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>

All types that are Pod are Copy

is_zeroable: IsZeroable<T>

All types that are Pod are Zeroable

Implementations

Constructs an IsPod.

You can also use the IsPod or ìnfer macros to construct IsPod arguments.

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());
}

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Constructs this type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.