Macro constmuck::wrapper::peel_ref[][src]

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

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

This is equivalent to a function with this signature:

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

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> {}

const X: &[u8] = {
    let x: &'static Foo<[u8]> = &Foo([3, 5, 8, 13]);

    // Casting `&Foo<[u8]>` to `&[u8]`
    //
    // `IsTW!()` is a more concise way to write `IsTransparentWrapper::NEW`
    wrapper::peel_ref!(x, IsTW!())
};

assert_eq!(X, [3, 5, 8, 13]);