pub unsafe trait TransparentNewtype {
type Inner: ?Sized;
fn from_inner_raw(from: *const Self::Inner) -> *const Self;
fn from_inner_raw_mut(v: *mut Self::Inner) -> *mut Self;
fn as_inner_raw(from: *const Self) -> *const Self::Inner;
fn as_inner_raw_mut(this: *mut Self) -> *mut Self::Inner;
}
Available on crate feature
transparent_newtype
only.Expand description
Trait for #[repr(transparent)]
newtypes,
which are safe to transmute to/from their contents.
For additional conversion methods, you can use the TransparentNewtypeExt
extension trait.
Safety
Implementors must only implement this trait for #[repr(transparent)]
wrappers,
with the same alignment as its only non-zero-sized field,
and the type of that field must be used as the TransparentNewtype::Inner
associated type.
This trait can be implemented with any of these macros:
- The
TransparentNewtype
derive macro (requires the “derive” feature). - The
impl_transparent_newtype
macro - The
delegate_transparent_newtype_impl
macro.
Example
A totally ordered 32 bit float.
use core_extensions::{TransparentNewtype, TransparentNewtypeExt};
pub struct TotalF32(pub f32);
// Eq, Ord, PartialEq, PartialOrd impls not shown
unsafe impl TransparentNewtype for TotalF32{
type Inner = f32;
core_extensions::impl_transparent_newtype!{Self}
}
let mut list = vec![1.0, 0.0, 2.0];
<[TotalF32]>::from_inner_mut(&mut list).sort();
assert_eq!(list, vec![0.0, 1.0, 2.0]);
Required Associated Types
Required Methods
sourcefn from_inner_raw(from: *const Self::Inner) -> *const Self
fn from_inner_raw(from: *const Self::Inner) -> *const Self
Converts *const Self::Inner
to *const Self
.
sourcefn from_inner_raw_mut(v: *mut Self::Inner) -> *mut Self
fn from_inner_raw_mut(v: *mut Self::Inner) -> *mut Self
Converts *mut Self::Inner
to *mut Self
.
sourcefn as_inner_raw(from: *const Self) -> *const Self::Inner
fn as_inner_raw(from: *const Self) -> *const Self::Inner
Converts *const Self
to *const Self::Inner
.
sourcefn as_inner_raw_mut(this: *mut Self) -> *mut Self::Inner
fn as_inner_raw_mut(this: *mut Self) -> *mut Self::Inner
Converts *mut Self
to *mut Self::Inner
.