Function constmuck::wrapper::wrap [−][src]
pub const fn wrap<Outer, Inner>(
val: Inner,
_bound: IsTransparentWrapper<Outer, Inner>
) -> Outer
Expand description
Casts Inner
to Outer
Requires that Outer
implements
TransparentWrapper<Inner>
Example
use constmuck::{IsTW, wrapper};
#[derive(Debug, PartialEq)]
#[repr(transparent)]
pub struct Qux<T>(pub T);
unsafe impl<T> constmuck::TransparentWrapper<T> for Qux<T> {}
// Casting `&u32` to `Qux<u32>`
//
// `IsTW!()` is a more concise way to write `IsTransparentWrapper::NEW`
const VALUE: Qux<u32> = wrapper::wrap(3, IsTW!());
assert_eq!(VALUE, Qux(3));
// `IsTW!(Qux<_>)` is required because any type can implement comparison with `Qux`.
assert_eq!(wrapper::wrap(3, IsTW!(Qux<_>)), Qux(3));
// Casting `[u32; 3]` to `[Qux<u32>; 3]`
//
// The `.array()` is currently required to cast arrays of values into arrays of
// wrappers around those values.
const ARR: [Qux<u32>; 3] = wrapper::wrap([5, 8, 13], IsTW!(Qux<u32>, u32).array());
assert_eq!(ARR, [Qux(5), Qux(8), Qux(13)]);
assert_eq!(
wrapper::wrap([5, 8, 13], IsTW!(Qux<_>).array()),
[Qux(5), Qux(8), Qux(13)],
);