pub unsafe trait Transparent {
type Wrapped: ?Sized;
// Provided methods
fn from_ref(wrapped: &Self::Wrapped) -> &Self { ... }
fn from_ref_mut(wrapped: &mut Self::Wrapped) -> &mut Self { ... }
}Expand description
Transparent is a trait that indicates that the implementor is a transparent wrapper around some type, and should be treated interoperably with wrapped type.
This allows for internal types to be treated as references of the wrapper type, and vice-versa.
§Note
This trait only allows for conversions between references of objects; this means that the formed references will become ephemeral references of a different type – but the lifetime of the object will still be tied to the lifetime of the source.
§Acknowledgements
The approach to use this trait is heavily inspired by the bytemuck crate,
which is licensed under ZLib OR MIT OR Apache-2. The idea has been adapted
for this library’s specific needs.
§Safety
The safety contract for this type requires two things:
- The wrapper type implementing this must be
#[repr(transparent)] - The implementation must not override the function definitions in the trait.
Required Associated Types§
Provided Methods§
sourcefn from_ref(wrapped: &Self::Wrapped) -> &Self
fn from_ref(wrapped: &Self::Wrapped) -> &Self
Converts a reference to a type into a reference of the wrapper type.
§Safety
The safety contract for this type requires two things:
- The wrapper type implementing this must be
#[repr(transparent)] - This function must not be implemented in
impls.
sourcefn from_ref_mut(wrapped: &mut Self::Wrapped) -> &mut Self
fn from_ref_mut(wrapped: &mut Self::Wrapped) -> &mut Self
Converts a mut reference to a wrapped type into a mut reference to the wrapper.
§Safety
The safety contract for this type requires two things:
- The wrapper type implementing this must be
#[repr(transparent)] - This function must not be implemented in
impls.
Object Safety§
Implementations on Foreign Types§
source§impl<T> Transparent for *const Twhere
T: Transparent,
impl<T> Transparent for *const Twhere
T: Transparent,
source§impl<T> Transparent for *mut Twhere
T: Transparent,
impl<T> Transparent for *mut Twhere
T: Transparent,
source§impl<T> Transparent for [T]
impl<T> Transparent for [T]
Slices which are Transparent are convertible to their wrapped types.
type Wrapped = [<T as Transparent>::Wrapped]
source§impl<T> Transparent for Box<T>where
T: Transparent,
impl<T> Transparent for Box<T>where
T: Transparent,
source§impl<T> Transparent for Rc<T>where
T: Transparent,
impl<T> Transparent for Rc<T>where
T: Transparent,
source§impl<T> Transparent for Arc<T>where
T: Transparent,
impl<T> Transparent for Arc<T>where
T: Transparent,
source§impl<T> Transparent for Vec<T>
impl<T> Transparent for Vec<T>
source§impl<const N: usize, T> Transparent for [T; N]
impl<const N: usize, T> Transparent for [T; N]
Arrays which are Transparent are convertible to their wrapped types.