macro_rules! impl_transparent_newtype {
($S:ty) => { ... };
}
Available on crate feature
transparent_newtype
only.Expand description
For implementing the TransparentNewtype
trait,
to cast between a field and Self
.
Example
use core_extensions::{TransparentNewtype, TransparentNewtypeExt, impl_transparent_newtype};
use std::cmp::{Ordering, Ord, PartialOrd};
#[repr(transparent)]
#[derive(PartialEq, Eq)]
struct Reverse<T: ?Sized>(T);
unsafe impl<T: ?Sized> TransparentNewtype for Reverse<T> {
type Inner = T;
// The argument must be `Self`
// (it could also be Reverse<T> in here, it's just easier to always write Self)
impl_transparent_newtype!{Self}
}
/* PartialOrd an Ord impls for Reverse */
let mut list = vec![3, 13, 21, 5, 8, 34];
<[Reverse<u64>]>::from_inner_mut(&mut list).sort();
assert_eq!(list, vec![34, 21, 13, 8, 5, 3]);