Macro constmuck::wrapper::wrap_ref[][src]

macro_rules! wrap_ref {
    ($reff : expr, $is_tw : expr $(,) *) => { ... };
}
Expand description

Casts &Inner to &Outer, allows casting between ?Sized types.

This is equivalent to a function with this signature:

pub const fn wrap_ref<Inner: ?Sized, Outer: ?Sized>(
    reff: &Inner,
    is_tw: constmuck::IsTransparentWrapper<Outer, Inner>
) -> &Outer

Requires that Outer implements TransparentWrapper<Inner>

Note that, because of how this macro is implemented, infer cannot be passed as the is_tw argument. You must pass a type that’s known to be an IsTransparentWrapper beforehand, eg: IsTW!(), IsTransparentWrapper::NEW.

Example

use constmuck::{IsTW, wrapper};

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

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

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

assert_eq!(X.0, *"world");

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