Struct constmuck::IsPod [−][src]
pub struct IsPod<T> {
pub is_copy: IsCopy<T>,
pub is_zeroable: IsZeroable<T>,
// some fields omitted
}
Expand description
Encodes a T:
Pod
bound as a value.
Example
use constmuck::{IsPod, cast, cast_ref_alt, cast_slice_alt, infer};
{
// transmuting `i16` to `u16`
// The four lines below are equivalent
const FOO1: u16 = cast(-1i16, (IsPod::NEW, IsPod::NEW));
const FOO2: u16 = cast(-1i16, (IsPod!(), IsPod!()));
const FOO3: u16 = cast(-1, (IsPod!(i16), IsPod!(u16)));
const FOO4: u16 = cast(-1i16, infer!());
assert_eq!(FOO1, u16::MAX);
assert_eq!(FOO2, u16::MAX);
assert_eq!(FOO3, u16::MAX);
assert_eq!(FOO4, 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
is_copy: IsCopy<T>
is_zeroable: IsZeroable<T>
Implementations
Constructs an IsPod<T>
without checking that T
implements Pod
.
Safety
You must ensure that T
follows the
safety requirements of Pod
use constmuck::{IsPod, cast};
#[repr(transparent)]
struct Foo([u8; 4]);
unsafe{
let bounds = (IsPod::new_unchecked(), IsPod::new_unchecked());
assert_eq!(cast::<u32, Foo>(12345678, bounds).0, 12345678u32.to_ne_bytes());
}