smallmap/
primitive.rs

1//! Contains `Collapse` impls for primitive types through a newtype shim.
2//!
3//! # Why
4//! Such wrappers are a workaround for the lack of template specialisation available in Rust so far, as the generic `impl<T: Hash> Collapse<T> for T` still requires computing the hash of the internal types before reducing to the `u8` page index.
5//! For primitive types, this is unnessisary and causes a (very slight) performance loss.
6//!
7//! If/when Rust gets specialisation, this will be unneeded.
8use super::*;
9use core::num::*;
10
11/// Sealed trait allowing for wrapping primitive types with a more efficient implemntation for the `Collapse` trait.
12/// This should not be used for much directly, instead use the newtype shim `Primitive<T>`.
13pub trait PrimitiveCollapse: private::Sealed
14{
15    fn collapse(&self) -> u8;
16}
17
18/// Shim for primitive types to efficiently implement `Collapse`.
19///
20/// # Notes
21/// This newtype is transparent. It is safe to `mem::transmute`() from `Primitive<T>` to `T` and vice versa.
22/// However, if `T` does *not* implement `PrimitiveCollapse`, it is undefined behaviour.
23///
24/// Also, the `collapse()` output from this structure is not guaranteed to be the same as the `collapse()` output from the inner value, so the following code is very unsafe and such patterns should only be used if the programmer is absolutely sure there will be absolutely no difference between `T::collapse` and `Self::collapse`:
25/// ```
26/// # use smallmap::{Map, Primitive};
27/// # use std::mem;
28///
29///  let mut map: Map<u8, ()> = Map::new();
30///  map.insert(120, ());
31///
32///  let map: Map<Primitive<u8>, ()> = unsafe { mem::transmute(map) };
33///  assert_eq!(map.get(&120.into()).copied(), Some(()));
34/// ```
35/// This code pretty much only works with `u8`. and `i8`.
36///
37/// However unsafe, it is possible these values will line up in your use case. In which case, it is an acceptable pattern.
38#[derive(Debug, Clone, PartialEq, Eq, Copy, Default, Ord, PartialOrd)]
39#[repr(transparent)]
40pub struct Primitive<T>(T);
41
42impl<T: PrimitiveCollapse+ Eq> Collapse for Primitive<T>
43{
44    #[inline(always)] fn collapse(&self) -> u8 {
45	self.0.collapse()
46    }
47}
48
49impl<T: PrimitiveCollapse+ Eq> Primitive<T>
50{
51    /// Wrap this primitive 
52    #[cfg(nightly)] #[inline] pub const fn new(value: T) -> Self
53    {
54	Self(value)
55    }
56    /// Wrap this primitive 
57    #[cfg(not(nightly))] #[inline] pub fn new(value: T) -> Self
58    {
59	Self(value)
60    }
61    /// Consume into the inner primitive
62    #[inline] pub fn into_inner(self) -> T
63    {
64	self.0
65    }
66    /// Get the inner primitive
67    ///
68    /// # Notes
69    /// Only useful if the inner type does not implement `Copy`, which is extremely unlickely.
70    /// You should almost always use `into_inner` instead.
71    #[inline] pub fn inner(&self) -> &T
72    {
73	&self.0
74    }
75    /// Get a mutable reference to the inner ptimitive.
76    #[inline] pub fn inner_mut(&mut self) -> &mut T
77    {
78	&mut self.0
79    }
80    
81    /// Same as `into_inner`, except only for `Copy` types.
82    ///
83    /// # Notes
84    /// The only use of this function is that it is `const fn` on nightly.
85    /// If you're not using a version of rustc that supports generic `const fn`, this method is identical to `into_inner`.
86    #[cfg(nightly)] #[inline] pub const fn into_inner_copy(self) -> T
87    where T: Copy
88    {
89	self.0
90    }
91    #[cfg(not(nightly))] #[inline(always)] #[deprecated = "This function should only be used on Rust nightly. Please use `into_inner` instead"] pub fn into_inner_copy(self) -> T
92    where T: Copy
93    {
94	self.0
95    }
96}
97
98impl<T> From<T> for Primitive<T>
99    where T: PrimitiveCollapse + Eq
100{
101    #[inline] fn from(from: T) -> Self
102    {
103	Self::new(from)
104    }
105}
106
107macro_rules! prim {
108    ($name:ty) => {	
109	impl private::Sealed for $name{}
110	impl PrimitiveCollapse for $name
111	{
112	    #[inline(always)] fn collapse(&self) -> u8 {
113		(*self) as u8
114	    }
115	}
116    };
117    ($name:ty: +) => {	
118	impl private::Sealed for $name{}
119	impl PrimitiveCollapse for $name
120	{
121	    #[inline(always)] fn collapse(&self) -> u8 {
122		self.get() as u8
123	    }
124	}
125    };
126    ($name:ty: ^) => {	
127	impl private::Sealed for $name{}
128	impl PrimitiveCollapse for $name
129	{
130	    #[inline(always)] fn collapse(&self) -> u8 {
131		super::collapse(<$name>::to_ne_bytes(*self))
132	    }
133	}
134    };
135    ($name:ty: ^+) => {	
136	impl private::Sealed for $name{}
137	impl PrimitiveCollapse for $name
138	{
139	    #[inline(always)] fn collapse(&self) -> u8 {
140		super::collapse(self.get().to_ne_bytes())
141	    }
142	}
143    };
144    ($name:ty: fn {$($block:tt)*}) => {
145	impl private::Sealed for $name{}
146	impl PrimitiveCollapse for $name
147	{
148	    #[inline(always)] fn collapse(&self) -> u8 {
149		$($block)+
150	    }
151	}
152    };
153    ($name:ty: {$($block:tt)*}) => {
154	impl private::Sealed for $name{}
155	impl PrimitiveCollapse for $name
156	{
157	    $($block)+
158	}
159    };
160
161}
162
163prim!(u8);
164prim!(i8);
165prim!(u16: ^);
166prim!(i16: ^);
167prim!(u32: ^);
168prim!(i32: ^);
169prim!(u64: ^);
170prim!(i64: ^);
171prim!(u128: ^);
172prim!(i128: ^);
173prim!(isize: ^);
174prim!(usize: ^);
175
176prim!(NonZeroU8: +);
177prim!(NonZeroI8: +);
178prim!(NonZeroU16: ^+);
179prim!(NonZeroI16: ^+);
180prim!(NonZeroU32: ^+);
181prim!(NonZeroI32: ^+);
182prim!(NonZeroU64: ^+);
183prim!(NonZeroI64: ^+);
184prim!(NonZeroU128: ^+);
185prim!(NonZeroI128: ^+);
186prim!(NonZeroIsize: ^+);
187prim!(NonZeroUsize: ^+);
188
189prim!((): fn {
190    0
191});
192
193#[cfg(nightly)] 
194prim!(!: {
195    fn collapse(&self) -> u8
196    {
197	*self
198    }
199});