|  | 
|  | 1 | +#![unstable( | 
|  | 2 | +    feature = "temporary_niche_types", | 
|  | 3 | +    issue = "none", | 
|  | 4 | +    reason = "for core, alloc, and std internals until pattern types are further along" | 
|  | 5 | +)] | 
|  | 6 | + | 
|  | 7 | +use crate::cmp::Ordering; | 
|  | 8 | +use crate::fmt; | 
|  | 9 | +use crate::hash::{Hash, Hasher}; | 
|  | 10 | +use crate::marker::StructuralPartialEq; | 
|  | 11 | + | 
|  | 12 | +macro_rules! define_valid_range_type { | 
|  | 13 | +    ($( | 
|  | 14 | +        $(#[$m:meta])* | 
|  | 15 | +        $vis:vis struct $name:ident($int:ident as $uint:ident in $low:literal..=$high:literal); | 
|  | 16 | +    )+) => {$( | 
|  | 17 | +        #[derive(Clone, Copy, Eq)] | 
|  | 18 | +        #[repr(transparent)] | 
|  | 19 | +        #[rustc_layout_scalar_valid_range_start($low)] | 
|  | 20 | +        #[rustc_layout_scalar_valid_range_end($high)] | 
|  | 21 | +        $(#[$m])* | 
|  | 22 | +        $vis struct $name($int); | 
|  | 23 | + | 
|  | 24 | +        const _: () = { | 
|  | 25 | +            // With the `valid_range` attributes, it's always specified as unsigned | 
|  | 26 | +            assert!(<$uint>::MIN == 0); | 
|  | 27 | +            let ulow: $uint = $low; | 
|  | 28 | +            let uhigh: $uint = $high; | 
|  | 29 | +            assert!(ulow <= uhigh); | 
|  | 30 | + | 
|  | 31 | +            assert!(size_of::<$int>() == size_of::<$uint>()); | 
|  | 32 | +        }; | 
|  | 33 | + | 
|  | 34 | +        impl $name { | 
|  | 35 | +            #[inline] | 
|  | 36 | +            pub const unsafe fn new_unchecked(val: $int) -> Self { | 
|  | 37 | +                // SAFETY: same precondition | 
|  | 38 | +                unsafe { $name(val) } | 
|  | 39 | +            } | 
|  | 40 | + | 
|  | 41 | +            #[inline] | 
|  | 42 | +            pub const fn as_inner(self) -> $int { | 
|  | 43 | +                // SAFETY: This is a transparent wrapper, so unwrapping it is sound | 
|  | 44 | +                // (Not using `.0` due to MCP#807.) | 
|  | 45 | +                unsafe { crate::mem::transmute(self) } | 
|  | 46 | +            } | 
|  | 47 | +        } | 
|  | 48 | + | 
|  | 49 | +        // This is required to allow matching a constant.  We don't get it from a derive | 
|  | 50 | +        // because the derived `PartialEq` would do a field projection, which is banned | 
|  | 51 | +        // by <https://github.com/rust-lang/compiler-team/issues/807>. | 
|  | 52 | +        impl StructuralPartialEq for $name {} | 
|  | 53 | + | 
|  | 54 | +        impl PartialEq for $name { | 
|  | 55 | +            #[inline] | 
|  | 56 | +            fn eq(&self, other: &Self) -> bool { | 
|  | 57 | +                self.as_inner() == other.as_inner() | 
|  | 58 | +            } | 
|  | 59 | +        } | 
|  | 60 | + | 
|  | 61 | +        impl Ord for $name { | 
|  | 62 | +            #[inline] | 
|  | 63 | +            fn cmp(&self, other: &Self) -> Ordering { | 
|  | 64 | +                Ord::cmp(&self.as_inner(), &other.as_inner()) | 
|  | 65 | +            } | 
|  | 66 | +        } | 
|  | 67 | + | 
|  | 68 | +        impl PartialOrd for $name { | 
|  | 69 | +            #[inline] | 
|  | 70 | +            fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | 
|  | 71 | +                Some(Ord::cmp(self, other)) | 
|  | 72 | +            } | 
|  | 73 | +        } | 
|  | 74 | + | 
|  | 75 | +        impl Hash for $name { | 
|  | 76 | +            // Required method | 
|  | 77 | +            fn hash<H: Hasher>(&self, state: &mut H) { | 
|  | 78 | +                Hash::hash(&self.as_inner(), state); | 
|  | 79 | +            } | 
|  | 80 | +        } | 
|  | 81 | + | 
|  | 82 | +        impl fmt::Debug for $name { | 
|  | 83 | +            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 
|  | 84 | +                <$int as fmt::Debug>::fmt(&self.as_inner(), f) | 
|  | 85 | +            } | 
|  | 86 | +        } | 
|  | 87 | +    )+}; | 
|  | 88 | +} | 
|  | 89 | + | 
|  | 90 | +define_valid_range_type! { | 
|  | 91 | +    pub struct Nanoseconds(u32 as u32 in 0..=999_999_999); | 
|  | 92 | +} | 
|  | 93 | + | 
|  | 94 | +impl Nanoseconds { | 
|  | 95 | +    // SAFETY: 0 is within the valid range | 
|  | 96 | +    pub const ZERO: Self = unsafe { Nanoseconds::new_unchecked(0) }; | 
|  | 97 | +} | 
|  | 98 | + | 
|  | 99 | +impl Default for Nanoseconds { | 
|  | 100 | +    #[inline] | 
|  | 101 | +    fn default() -> Self { | 
|  | 102 | +        Self::ZERO | 
|  | 103 | +    } | 
|  | 104 | +} | 
|  | 105 | + | 
|  | 106 | +define_valid_range_type! { | 
|  | 107 | +    pub struct NonZeroU8Inner(u8 as u8 in 1..=0xff); | 
|  | 108 | +    pub struct NonZeroU16Inner(u16 as u16 in 1..=0xff_ff); | 
|  | 109 | +    pub struct NonZeroU32Inner(u32 as u32 in 1..=0xffff_ffff); | 
|  | 110 | +    pub struct NonZeroU64Inner(u64 as u64 in 1..=0xffffffff_ffffffff); | 
|  | 111 | +    pub struct NonZeroU128Inner(u128 as u128 in 1..=0xffffffffffffffff_ffffffffffffffff); | 
|  | 112 | + | 
|  | 113 | +    pub struct NonZeroI8Inner(i8 as u8 in 1..=0xff); | 
|  | 114 | +    pub struct NonZeroI16Inner(i16 as u16 in 1..=0xff_ff); | 
|  | 115 | +    pub struct NonZeroI32Inner(i32 as u32 in 1..=0xffff_ffff); | 
|  | 116 | +    pub struct NonZeroI64Inner(i64 as u64 in 1..=0xffffffff_ffffffff); | 
|  | 117 | +    pub struct NonZeroI128Inner(i128 as u128 in 1..=0xffffffffffffffff_ffffffffffffffff); | 
|  | 118 | +} | 
|  | 119 | + | 
|  | 120 | +#[cfg(target_pointer_width = "16")] | 
|  | 121 | +define_valid_range_type! { | 
|  | 122 | +    pub struct UsizeNoHighBit(usize as usize in 0..=0x7fff); | 
|  | 123 | +    pub struct NonZeroUsizeInner(usize as usize in 1..=0xffff); | 
|  | 124 | +    pub struct NonZeroIsizeInner(isize as usize in 1..=0xffff); | 
|  | 125 | +} | 
|  | 126 | +#[cfg(target_pointer_width = "32")] | 
|  | 127 | +define_valid_range_type! { | 
|  | 128 | +    pub struct UsizeNoHighBit(usize as usize in 0..=0x7fff_ffff); | 
|  | 129 | +    pub struct NonZeroUsizeInner(usize as usize in 1..=0xffff_ffff); | 
|  | 130 | +    pub struct NonZeroIsizeInner(isize as usize in 1..=0xffff_ffff); | 
|  | 131 | +} | 
|  | 132 | +#[cfg(target_pointer_width = "64")] | 
|  | 133 | +define_valid_range_type! { | 
|  | 134 | +    pub struct UsizeNoHighBit(usize as usize in 0..=0x7fff_ffff_ffff_ffff); | 
|  | 135 | +    pub struct NonZeroUsizeInner(usize as usize in 1..=0xffff_ffff_ffff_ffff); | 
|  | 136 | +    pub struct NonZeroIsizeInner(isize as usize in 1..=0xffff_ffff_ffff_ffff); | 
|  | 137 | +} | 
|  | 138 | + | 
|  | 139 | +define_valid_range_type! { | 
|  | 140 | +    pub struct U32NotAllOnes(u32 as u32 in 0..=0xffff_fffe); | 
|  | 141 | +    pub struct I32NotAllOnes(i32 as u32 in 0..=0xffff_fffe); | 
|  | 142 | + | 
|  | 143 | +    pub struct U64NotAllOnes(u64 as u64 in 0..=0xffff_ffff_ffff_fffe); | 
|  | 144 | +    pub struct I64NotAllOnes(i64 as u64 in 0..=0xffff_ffff_ffff_fffe); | 
|  | 145 | +} | 
|  | 146 | + | 
|  | 147 | +pub trait NotAllOnesHelper { | 
|  | 148 | +    type Type; | 
|  | 149 | +} | 
|  | 150 | +pub type NotAllOnes<T> = <T as NotAllOnesHelper>::Type; | 
|  | 151 | +impl NotAllOnesHelper for u32 { | 
|  | 152 | +    type Type = U32NotAllOnes; | 
|  | 153 | +} | 
|  | 154 | +impl NotAllOnesHelper for i32 { | 
|  | 155 | +    type Type = I32NotAllOnes; | 
|  | 156 | +} | 
|  | 157 | +impl NotAllOnesHelper for u64 { | 
|  | 158 | +    type Type = U64NotAllOnes; | 
|  | 159 | +} | 
|  | 160 | +impl NotAllOnesHelper for i64 { | 
|  | 161 | +    type Type = I64NotAllOnes; | 
|  | 162 | +} | 
0 commit comments