1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! Contains `Collapse` impls for primitive types through a newtime shim.
//!
//! # Why
//! 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.
//! For primitive types, this is unnessisary and causes a (very slight) performance loss.
//!
//! If/when Rust gets specialisation, this will be unneeded.
use super::*;
use std::num::*;

/// Sealed trait allowing for wrapping primitive types with a more efficient implemntation for the `Collapse` trait.
/// This should not be used for much directly, instead use the newtype shim `Primitive<T>`.
pub trait PrimitiveCollapse: private::Sealed
{
    fn collapse(&self) -> u8;
}

/// Shim for primitive types to efficiently implement `Collapse`.
///
/// # Notes
/// This newtype is transparent. It is safe to `mem::transmute`() from `Primitive<T>` to `T` and vice versa.
/// However, if `T` does *not* implement `PrimitiveCollapse`, it is undefined behaviour.
///
/// 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`:
/// ```
/// # use smallmap::{Map, Primitive};
/// # use std::mem;
///
///  let mut map: Map<u8, ()> = Map::new();
///  map.insert(120, ());
///
///  let map: Map<Primitive<u8>, ()> = unsafe { mem::transmute(map) };
///  assert_eq!(map.get(&120.into()).copied(), Some(()));
/// ```
/// This code pretty much only works with `u8`. and `i8`.
///
/// However unsafe, it is possible these values will line up in your use case. In which case, it is an acceptable pattern.
#[derive(Debug, Clone, PartialEq, Eq, Copy, Default, Ord, PartialOrd)]
#[repr(transparent)]
pub struct Primitive<T>(T);

impl<T: PrimitiveCollapse+ Eq> Collapse for Primitive<T>
{
    #[inline(always)] fn collapse(&self) -> u8 {
	self.0.collapse()
    }
}

impl<T: PrimitiveCollapse+ Eq> Primitive<T>
{
    /// Wrap this primitive 
    #[cfg(nightly)] #[inline] pub const fn new(value: T) -> Self
    {
	Self(value)
    }
    /// Wrap this primitive 
    #[cfg(not(nightly))] #[inline] pub fn new(value: T) -> Self
    {
	Self(value)
    }
    /// Consume into the inner primitive
    #[inline] pub fn into_inner(self) -> T
    {
	self.0
    }
    /// Get the inner primitive
    ///
    /// # Notes
    /// Only useful if the inner type does not implement `Copy`, which is extremely unlickely.
    /// You should almost always use `into_inner` instead.
    #[inline] pub fn inner(&self) -> &T
    {
	&self.0
    }
    /// Get a mutable reference to the inner ptimitive.
    #[inline] pub fn inner_mut(&mut self) -> &mut T
    {
	&mut self.0
    }
    
    /// Same as `into_inner`, except only for `Copy` types.
    ///
    /// # Notes
    /// The only use of this function is that it is `const fn` on nightly.
    /// If you're not using a version of rustc that supports generic `const fn`, this method is identical to `into_inner`.
    #[cfg(nightly)] #[inline] pub const fn into_inner_copy(self) -> T
    where T: Copy
    {
	self.0
    }
    #[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
    where T: Copy
    {
	self.0
    }
}

impl<T> From<T> for Primitive<T>
    where T: PrimitiveCollapse + Eq
{
    #[inline] fn from(from: T) -> Self
    {
	Self::new(from)
    }
}

macro_rules! prim {
    ($name:ty) => {	
	impl private::Sealed for $name{}
	impl PrimitiveCollapse for $name
	{
	    #[inline(always)] fn collapse(&self) -> u8 {
		(*self) as u8
	    }
	}
    };
    ($name:ty: +) => {	
	impl private::Sealed for $name{}
	impl PrimitiveCollapse for $name
	{
	    #[inline(always)] fn collapse(&self) -> u8 {
		self.get() as u8
	    }
	}
    };
    ($name:ty: ^) => {	
	impl private::Sealed for $name{}
	impl PrimitiveCollapse for $name
	{
	    #[inline(always)] fn collapse(&self) -> u8 {
		super::collapse(<$name>::to_ne_bytes(*self))
	    }
	}
    };
    ($name:ty: ^+) => {	
	impl private::Sealed for $name{}
	impl PrimitiveCollapse for $name
	{
	    #[inline(always)] fn collapse(&self) -> u8 {
		super::collapse(self.get().to_ne_bytes())
	    }
	}
    };
    ($name:ty: fn {$($block:tt)*}) => {
	impl private::Sealed for $name{}
	impl PrimitiveCollapse for $name
	{
	    #[inline(always)] fn collapse(&self) -> u8 {
		$($block)+
	    }
	}
    };
    ($name:ty: {$($block:tt)*}) => {
	impl private::Sealed for $name{}
	impl PrimitiveCollapse for $name
	{
	    $($block)+
	}
    };

}

prim!(u8);
prim!(i8);
prim!(u16: ^);
prim!(i16: ^);
prim!(u32: ^);
prim!(i32: ^);
prim!(u64: ^);
prim!(i64: ^);
prim!(u128: ^);
prim!(i128: ^);
prim!(isize: ^);
prim!(usize: ^);

prim!(NonZeroU8: +);
prim!(NonZeroI8: +);
prim!(NonZeroU16: ^+);
prim!(NonZeroI16: ^+);
prim!(NonZeroU32: ^+);
prim!(NonZeroI32: ^+);
prim!(NonZeroU64: ^+);
prim!(NonZeroI64: ^+);
prim!(NonZeroU128: ^+);
prim!(NonZeroI128: ^+);
prim!(NonZeroIsize: ^+);
prim!(NonZeroUsize: ^+);

prim!((): fn {
    0
});

#[cfg(nightly)] 
prim!(!: {
    fn collapse(&self) -> u8
    {
	*self
    }
});