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            #[inline]
67            fn to_bits(self) -> Self::UInt {
68                $float::to_bits(self)
69            }
70            #[inline]
71            fn from_bits(bits: Self::UInt) -> Self {
72                $float::from_bits(bits)
73            }
74            #[inline]
75            fn is_nan(self) -> bool {
76                $float::is_nan(self)
77            }
78            #[inline]
79            fn is_sign_positive(self) -> bool {
80                $float::is_sign_positive(self)
81            }
82            #[inline]
83            fn is_sign_negative(self) -> bool {
84                $float::is_sign_negative(self)
85            }
86        }
87    )+};
88}
89impl_float_primitive!(f16 => u16, f32 => u32, f64 => u64, f128 => u128);
90
91/// Built-in integer types (i8, i16, .., i128, isize, u8, u16, .., u128, usize).
92///
93/// Intentionally does not include other integer-repr types like `bool` or `char`.
94///
95/// # Safety
96/// Must actually *be* such a type.
97#[rustc_const_unstable(feature = "core_intrinsics", issue = "none")]
98pub const unsafe trait IntegerPrimitive: Copy + [const] Ord {}
99
100macro_rules! impl_integer_primitive {
101    ($($t:ty),*) => {$(
102        #[rustc_const_unstable(feature = "core_intrinsics", issue = "none")]
103        const unsafe impl IntegerPrimitive for $t {}
104    )*};
105}
106impl_integer_primitive!(i8, i16, i32, i64, i128, isize);
107impl_integer_primitive!(u8, u16, u32, u64, u128, usize);