Struct constmuck::ImplsPod [−][src]
pub struct ImplsPod<T> {
pub impls_copy: ImplsCopy<T>,
pub impls_zeroable: ImplsZeroable<T>,
// some fields omitted
}
Expand description
Encodes a T:
Pod
bound as a value,
avoids requiring (unstable as of 2021) trait bounds in const fn
s.
Example
use constmuck::{ImplsPod, cast, cast_ref_alt, cast_slice_alt, infer};
{
// transmuting `i16` to `u16`
const FOO1: u16 = cast(-1i16, (ImplsPod::NEW, ImplsPod::NEW));
// The same as the above constant
const FOO2: u16 = cast(-1i16, infer!());
assert_eq!(FOO1, u16::MAX);
assert_eq!(FOO2, u16::MAX);
}
{
// transmuting `&i8` to `&u8`
const REFF: &u8 = cast_ref_alt(&-2i8, infer!());
assert_eq!(REFF, &254);
}
{
// transmuting `&[u8]` to `&[i8]`
const REFF: &[i8] = cast_slice_alt(&[0u8, 127, 128, 255], infer!());
assert_eq!(REFF, &[0i8, 127, -128, -1]);
}
Fields
impls_copy: ImplsCopy<T>
impls_zeroable: ImplsZeroable<T>
Implementations
Constructs an ImplsPod<T>
without checking that T
implements Pod
.
Safety
You must ensure that T
follows the
safety requirements of Pod
use constmuck::{ImplsPod, cast, infer};
#[repr(transparent)]
struct Foo([u8; 4]);
unsafe{
let bounds = (ImplsPod::new_unchecked(), ImplsPod::new_unchecked());
assert_eq!(cast::<u32, Foo>(12345678, bounds).0, 12345678u32.to_ne_bytes());
}