Function constmuck::wrapper::wrap_slice[][src]

pub const fn wrap_slice<Outer, Inner>(
    reff: &[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 Bar<T>(pub T);

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

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

assert_eq!(X, [Bar("hello"), Bar("world")]);

// `IsTW!(Bar<_>)` is required because any type can implement comparison with `Bar`.
assert_eq!(
    wrapper::wrap_slice(&["hello", "world"], IsTW!(Bar<_>)),
    [Bar("hello"), Bar("world")],
);