Macro constmuck::IsTW [−][src]
macro_rules! IsTW {
() => { ... };
($Outer : ty $(,) *) => { ... };
($Outer : ty, $Inner : ty $(,) *) => { ... };
}
Expand description
Constructs an IsTransparentWrapper<$Outer, $Inner>
,
requiring that $Outer
implements TransparentWrapper
<$Inner>
.
This has two optional type arguments ($Outer
and $Inner
) that default to
infering the type if not passed.
Example
constmuck::wrapper
functions
use constmuck::{IsTW, wrapper};
#[derive(Debug, PartialEq)]
#[repr(transparent)]
pub struct Foo<T>(pub T);
unsafe impl<T> constmuck::TransparentWrapper<T> for Foo<T> {}
{
// Transmute `ì8` to `Foo<i8>`
const WRAP_VAL_ONE: Foo<i8> = wrapper::wrap(3, IsTW!());
assert_eq!(WRAP_VAL_ONE, Foo(3));
assert_eq!(wrapper::wrap(3, IsTW!(Foo<i8>)), Foo(3));
assert_eq!(wrapper::wrap(3, IsTW!(Foo<i8>, i8)), Foo(3));
// Transmute `[u8; 3]` to `[Foo<u8>; 3]`
//
// The `.array()` is currently required to cast arrays of values into arrays of
// wrappers around those values.
const WRAP_VAL_ARR: [Foo<u8>; 3] = wrapper::wrap([5, 8, 13], IsTW!(Foo<u8>, u8).array());
assert_eq!(WRAP_VAL_ARR, [Foo(5), Foo(8), Foo(13)]);
assert_eq!(
wrapper::wrap([5, 8, 13], IsTW!(Foo<u8>, u8).array()),
[Foo(5), Foo(8), Foo(13)],
);
assert_eq!(
wrapper::wrap([5, 8, 13], IsTW!(Foo<u8>, u8).array()),
[Foo(5), Foo(8), Foo(13)],
);
}
{
// Transmute `&i8` to `&Foo<i8>`
const WRAP_REF_ONE: &Foo<i8> = wrapper::wrap_ref(&3, IsTW!());
assert_eq!(WRAP_REF_ONE, &Foo(3));
// Transmute `&[u8; 3]` to `&[Foo<u8>; 3]`
const WRAP_REF_ARR: &[Foo<u8>; 3] = wrapper::wrap_ref(&[5, 8, 13], IsTW!().array());
assert_eq!(WRAP_REF_ARR, &[Foo(5), Foo(8), Foo(13)]);
}
{
// Transmute `&[i8]` to `&[Foo<i8>]`
const WRAP_SLICE: &[Foo<i8>] = wrapper::wrap_slice(&[21, 34, 55], IsTW!());
assert_eq!(WRAP_SLICE, &[Foo(21), Foo(34), Foo(55)]);
}