pub struct Primitive<T>(/* private fields */);
Expand description
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
:
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.
Implementations§
source§impl<T: PrimitiveCollapse + Eq> Primitive<T>
impl<T: PrimitiveCollapse + Eq> Primitive<T>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consume into the inner primitive
sourcepub fn inner(&self) -> &T
pub fn inner(&self) -> &T
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.
sourcepub const fn into_inner_copy(self) -> Twhere
T: Copy,
pub const fn into_inner_copy(self) -> Twhere T: Copy,
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
.
Trait Implementations§
source§impl<T: Ord> Ord for Primitive<T>
impl<T: Ord> Ord for Primitive<T>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl<T: PartialEq> PartialEq for Primitive<T>
impl<T: PartialEq> PartialEq for Primitive<T>
source§impl<T: PartialOrd> PartialOrd for Primitive<T>
impl<T: PartialOrd> PartialOrd for Primitive<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more