diff --git a/components/nonzero/lib.rs b/components/nonzero/lib.rs index a787e647165..4ba341aee5f 100644 --- a/components/nonzero/lib.rs +++ b/components/nonzero/lib.rs @@ -19,10 +19,14 @@ mod imp { use self::core::nonzero::NonZero; pub type NonZeroU32 = NonZero; + pub type NonZeroUsize = NonZero; } #[cfg(not(feature = "unstable"))] mod imp { + use std::cmp; + use std::hash; + #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] pub struct NonZeroU32(u32); @@ -39,4 +43,45 @@ mod imp { self.0 } } + + #[derive(Clone, Copy, Debug, Eq)] + pub struct NonZeroUsize(&'static ()); + + impl NonZeroUsize { + pub fn new(x: usize) -> Option { + if x != 0 { + Some(NonZeroUsize(unsafe { &*(x as *const ()) })) + } else { + None + } + } + + pub fn get(self) -> usize { + self.0 as *const () as usize + } + } + + impl PartialEq for NonZeroUsize { + fn eq(&self, other: &Self) -> bool { + self.get() == other.get() + } + } + + impl PartialOrd for NonZeroUsize { + fn partial_cmp(&self, other: &Self) -> Option { + self.get().partial_cmp(&other.get()) + } + } + + impl Ord for NonZeroUsize { + fn cmp(&self, other: &Self) -> cmp::Ordering { + self.get().cmp(&other.get()) + } + } + + impl hash::Hash for NonZeroUsize { + fn hash(&self, hasher: &mut H) { + self.get().hash(hasher) + } + } }