Struct constmuck::IsTransparentWrapper[][src]

#[non_exhaustive]
pub struct IsTransparentWrapper<Outer: ?Sized, Inner: ?Sized> { /* fields omitted */ }
Expand description

Encodes a Outer:TransparentWrapper<Inner> bound as a value.

Related: IsTW macro, wrapper module.

You can also construct an IsTransparentWrapper<T, T>, which allows using a type wherever transparent wrappers to it are expected.

Example

use constmuck::{IsTW, wrapper};

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

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

{
    // Casting from `&&str` to `&This<&str>`
    //
    // `IsTW!()` is a more concise way to write `IsTransparentWrapper::NEW`
    const WRAPPED: &This<&str> = wrapper::wrap_ref(&"hi", IsTW!());
    assert_eq!(*WRAPPED, This("hi"));
}

{
    // Casting from `&This<&str>` to `&&str`
    const UNWRAPPED: &&str = wrapper::peel_ref(&This("hello"), IsTW!());
    assert_eq!(*UNWRAPPED, "hello");
}

{
    // Casting from `[u64; 2]` to `[This<u64>; 2]`
    //
    // The `.array()` is currently required to cast arrays of values into arrays of
    // wrappers around those values.
    const WRAPPED_ARR: [This<u64>; 2] =
        wrapper::wrap([9, 99], IsTW!(This<u64>, u64).array());

    assert_eq!(WRAPPED_ARR, [This(9), This(99)]);
}

{
    // Casting from `[This<i8>; 2]` to `[i8; 2]`
    //
    // `.array()` allows casting arrays of wrappers into
    // arrays of the values inside those wrappers, using the `peel*` functions.
    const UNWRAPPED_ARR: [i8; 2] =
        wrapper::peel([This(2), This(22)], IsTW!(This<i8>, i8).array());

    assert_eq!(UNWRAPPED_ARR, [2, 22]);
}

Implementations

Constructs an IsTransparentWrapper

You can also use the IsTW macro to construct IsTransparentWrapper arguments.

Constructs an IsTransparentWrapper<T, T>.

Example
use constmuck::{IsTransparentWrapper as IsTW, wrapper};

use std::num::Wrapping;

const fn add_u32<W>(left: u32, right: &W, is_u32: IsTW<W, u32>) -> u32 {
    left + *wrapper::peel_ref(right, is_u32)
}

assert_eq!(add_u32(3, &Wrapping(5), IsTW::NEW), 8);

assert_eq!(add_u32(8, &13, IsTW::IDENTITY), 21);

Constructs an IsTransparentWrapper without checking that Outer implements TransparentWrapper<Inner>.

Safety

You must ensure that Outer follows the safety requirements of TransparentWrapper<Inner>

Combines an IsTransparentWrapper with another to allow casting between Outer and Nested.

Without this you’d have to do Outer -> Inner -> Nested casts.

Example
use constmuck::{IsTW, IsTransparentWrapper, wrapper};

use std::num::Wrapping;

const FOO: IsTransparentWrapper<Bar<u32>, u32> = IsTW!().join(IsTW!());

// Equivalent to FOO, but passing the types explicitly to `IsTW`.
// Only the last joined `IsTW!(` needs to be passed the `$Inner` argument.
let foo = IsTW!(Bar<_>).join(IsTW!(Wrapping<u32>, u32));

assert_eq!(wrapper::wrap_ref(&5, FOO), &Bar(Wrapping(5)));

assert_eq!(wrapper::wrap_ref(&8, foo), &Bar(Wrapping(8)));

assert_eq!(
    wrapper::wrap_slice(&[13, 21], FOO),
    &[Bar(Wrapping(13)), Bar(Wrapping(21))]
);


#[derive(Debug, PartialEq)]
struct Bar<T>(Wrapping<T>);

unsafe impl<T> constmuck::TransparentWrapper<Wrapping<T>> for Bar<T> {}

Turns a IsTransparentWrapper<Outer, Inner> into a IsTransparentWrapper<[Outer; LEN], [Inner; LEN]>.

Example
use constmuck::{IsTW, wrapper};

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

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

{
    // Casting from `[u32; 5]` to `[Xyz<u32>; 5]`
    const ARR: [Xyz<u32>; 5] = wrapper::wrap(
        [3, 5, 13, 34, 89],
        IsTW!(Xyz<u32>, u32).array(),
    );
    
    assert_eq!(ARR, [Xyz(3), Xyz(5), Xyz(13), Xyz(34), Xyz(89)]);
}

{
    // Casting from `[Xyz<u32>; 5]` to `[u32; 5]`
    const ARR: [u32; 5] = wrapper::peel(
        [Xyz(3), Xyz(5), Xyz(13), Xyz(34), Xyz(89)],
        IsTW!(Xyz<u32>, u32).array::<5>(),
    );
    
    assert_eq!(ARR, [3, 5, 13, 34, 89]);
}

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Constructs this type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.