Skip to main content

core/intrinsics/
bounds.rs

1//! Various traits used to restrict intrinsics to not-completely-wrong types.
2
3use crate::marker::PointeeSized;
4
5/// Types with a built-in dereference operator in runtime MIR,
6/// aka references and raw pointers.
7///
8/// # Safety
9/// Must actually *be* such a type.
10pub unsafe trait BuiltinDeref: Sized {
11    type Pointee: PointeeSized;
12}
13
14unsafe impl<T: PointeeSized> BuiltinDeref for &mut T {
15    type Pointee = T;
16}
17unsafe impl<T: PointeeSized> BuiltinDeref for &T {
18    type Pointee = T;
19}
20unsafe impl<T: PointeeSized> BuiltinDeref for *mut T {
21    type Pointee = T;
22}
23unsafe impl<T: PointeeSized> BuiltinDeref for *const T {
24    type Pointee = T;
25}
26
27pub trait ChangePointee<U: PointeeSized>: BuiltinDeref {
28    type Output;
29}
30impl<'a, T: PointeeSized + 'a, U: PointeeSized + 'a> ChangePointee<U> for &'a mut T {
31    type Output = &'a mut U;
32}
33impl<'a, T: PointeeSized + 'a, U: PointeeSized + 'a> ChangePointee<U> for &'a T {
34    type Output = &'a U;
35}
36impl<T: PointeeSized, U: PointeeSized> ChangePointee<U> for *mut T {
37    type Output = *mut U;
38}
39impl<T: PointeeSized, U: PointeeSized> ChangePointee<U> for *const T {
40    type Output = *const U;
41}
42
43/// Built-in float types (f16, f32, f64 and f128).
44///
45/// # Safety
46/// Must actually *be* such a type.
47#[rustc_const_unstable(feature = "core_intrinsics", issue = "none")]
48pub const unsafe trait FloatPrimitive: Sized + Copy {
49    type UInt: const core::ops::BitOr<Output = Self::UInt>
50        + const core::ops::BitAnd<Output = Self::UInt>
51        + const core::ops::Not<Output = Self::UInt>;
52    const SIGN_MASK: Self::UInt;
53    fn to_bits(self) -> Self::UInt;
54    fn from_bits(bits: Self::UInt) -> Self;
55}
56
57#[rustc_const_unstable(feature = "core_intrinsics", issue = "none")]
58const unsafe impl FloatPrimitive for f16 {
59    type UInt = u16;
60    const SIGN_MASK: Self::UInt = f16::SIGN_MASK;
61    #[inline]
62    fn to_bits(self) -> Self::UInt {
63        f16::to_bits(self)
64    }
65    #[inline]
66    fn from_bits(bits: Self::UInt) -> Self {
67        f16::from_bits(bits)
68    }
69}
70
71#[rustc_const_unstable(feature = "core_intrinsics", issue = "none")]
72const unsafe impl FloatPrimitive for f32 {
73    type UInt = u32;
74    const SIGN_MASK: Self::UInt = f32::SIGN_MASK;
75    #[inline]
76    fn to_bits(self) -> Self::UInt {
77        f32::to_bits(self)
78    }
79    #[inline]
80    fn from_bits(bits: Self::UInt) -> Self {
81        f32::from_bits(bits)
82    }
83}
84
85#[rustc_const_unstable(feature = "core_intrinsics", issue = "none")]
86const unsafe impl FloatPrimitive for f64 {
87    type UInt = u64;
88    const SIGN_MASK: Self::UInt = f64::SIGN_MASK;
89    #[inline]
90    fn to_bits(self) -> Self::UInt {
91        f64::to_bits(self)
92    }
93    #[inline]
94    fn from_bits(bits: Self::UInt) -> Self {
95        f64::from_bits(bits)
96    }
97}
98
99#[rustc_const_unstable(feature = "core_intrinsics", issue = "none")]
100const unsafe impl FloatPrimitive for f128 {
101    type UInt = u128;
102    const SIGN_MASK: Self::UInt = f128::SIGN_MASK;
103    #[inline]
104    fn to_bits(self) -> Self::UInt {
105        f128::to_bits(self)
106    }
107    #[inline]
108    fn from_bits(bits: Self::UInt) -> Self {
109        f128::from_bits(bits)
110    }
111}