Function constmuck::wrapper::wrap_ref[][src]

pub const fn wrap_ref<Outer, Inner>(
    reff: &Inner,
    _bound: IsTransparentWrapper<Outer, Inner>
) -> &Outer
Expand description

Casts &Inner to &Outer

Requires that Outer implements TransparentWrapper<Inner>

To cast references to ?Sized types, you need to use the wrap_ref macro instead of this function.

Example

use constmuck::{IsTW, wrapper};

#[derive(Debug, PartialEq)]
#[repr(transparent)]
pub struct Foo<T>(pub T);

unsafe impl<T> constmuck::TransparentWrapper<T> for Foo<T> {}

// Casting `&u32` to `&Foo<u32>`
//
// `IsTW!()` is a more concise way to write `IsTransparentWrapper::NEW`
const X: &Foo<u32> = wrapper::wrap_ref(&100, IsTW!());

assert_eq!(X, &Foo(100));

// `IsTW!(Foo<_>)` is required because any type can implement comparison with `Foo`.
assert_eq!(wrapper::wrap_ref(&100, IsTW!(Foo<_>)), &Foo(100));