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.
47pub unsafe trait FloatPrimitive:
48    Sized + Copy + PartialOrd + core::ops::Mul<Output = Self> + core::ops::Add<Output = Self>
49{
50    type UInt: core::ops::BitOr<Output = Self::UInt>
51        + core::ops::BitAnd<Output = Self::UInt>
52        + core::ops::Not<Output = Self::UInt>;
53    const SIGN_MASK: Self::UInt;
54    fn to_bits(self) -> Self::UInt;
55    fn from_bits(bits: Self::UInt) -> Self;
56    fn is_nan(self) -> bool;
57    fn is_sign_positive(self) -> bool;
58    fn is_sign_negative(self) -> bool;
59}
60
61macro_rules! impl_float_primitive {
62    ($($float:ident => $bits:ident),+) => {$(
63        unsafe impl FloatPrimitive for $float {
64            type UInt = $bits;
65            const SIGN_MASK: Self::UInt = $float::SIGN_MASK;
66            #[ferrocene::prevalidated]
67            #[ferrocene::annotation(
68                "This function directly calls `core::$float::<impl $float>::to_bits` with no other conversions. \
69                Since `core::$float::<impl $float>::to_bits` is covered, this function can be considered validated."
70            )]
71            #[inline]
72            fn to_bits(self) -> Self::UInt {
73                $float::to_bits(self)
74            }
75            #[ferrocene::prevalidated]
76            #[ferrocene::annotation(
77                "This function directly calls `core::$float::<impl $float>::from_bits` with no other conversions. \
78                Since `core::$float::<impl $float>::from_bits` is covered, this function can be considered validated."
79            )]
80            #[inline]
81            fn from_bits(bits: Self::UInt) -> Self {
82                $float::from_bits(bits)
83            }
84            #[ferrocene::prevalidated]
85            #[inline]
86            fn is_nan(self) -> bool {
87                $float::is_nan(self)
88            }
89            #[inline]
90            fn is_sign_positive(self) -> bool {
91                $float::is_sign_positive(self)
92            }
93            #[inline]
94            fn is_sign_negative(self) -> bool {
95                $float::is_sign_negative(self)
96            }
97        }
98    )+};
99}
100impl_float_primitive!(f16 => u16, f32 => u32, f64 => u64, f128 => u128);
101
102/// Built-in integer types (i8, i16, .., i128, isize, u8, u16, .., u128, usize).
103///
104/// Intentionally does not include other integer-repr types like `bool` or `char`.
105///
106/// # Safety
107/// Must actually *be* such a type.
108#[rustc_const_unstable(feature = "core_intrinsics", issue = "none")]
109pub const unsafe trait IntegerPrimitive: Copy + [const] Ord {}
110
111macro_rules! impl_integer_primitive {
112    ($($t:ty),*) => {$(
113        #[rustc_const_unstable(feature = "core_intrinsics", issue = "none")]
114        const unsafe impl IntegerPrimitive for $t {}
115    )*};
116}
117impl_integer_primitive!(i8, i16, i32, i64, i128, isize);
118impl_integer_primitive!(u8, u16, u32, u64, u128, usize);