Struct constmuck::TransmutableInto[][src]

pub struct TransmutableInto<Fro: ?Sized, To: ?Sized> { /* fields omitted */ }
Expand description

Marker type which guarantees that Fro is safely transmutable into To, both by value and by (mutable) reference.

Related: transmutable module.

Functions for transmuting that require align_of::<Fro>() == align_of::<To>() (eg: a function that transmutes from Arc<Fro> to Arc<To>) have to contain that equality check as an assertion, because TransmutableInto’s constructors only require align_of::<Fro>() >= align_of::<To>().

Example

use constmuck::{
    transmutable::{TransmutableInto, transmute_into, transmute_slice},
    infer,
};

use std::num::Wrapping;

// Transmuting from `&[u8]` to `&[i8]`
const POD: &[i8] =
    transmute_slice(&[5u8, 25, 125, 250], TransmutableInto::pod(infer!()));
assert_eq!(*POD, [5, 25, 125, -6]);


Implementations

Constructs a TransmutableInto

Safety

Fro must be soundly transmutable to To.

References (& and &mut) to Fro must be soundly transmutable to point to To.

size_of::<Fro>() must be equal to size_of::<To>().

align_of::<Fro>() must be greater than or equal to align_of::<To>().

Constructs a TransmutableInto

Panics

Panics if either:

  • The size of Fro isn’t the same as To.
  • The alignment of Fro is less than To (Foo is allowed to be more aligned than To).
Example
use constmuck::{
    transmutable::{TransmutableInto, transmute_into},
    infer,
};

{
    // Transmuting from `[u8; 5]` to `[i8; 5]`
    const POD: [i8; 5] = transmute_into(
        [0u8, 127, 128, 129, 130],
        TransmutableInto::pod(infer!()),
    );
    
    assert_eq!(POD, [0, 127, -128, -127, -126]);
}

Turns a TransmutableInto<Fro, To> into a TransmutableInto<[Fro; LEN], [To; LEN]>.

Example
use constmuck::{
    transmutable::{TransmutableInto, transmute_ref},
    infer_tw,
};

#[derive(Debug, PartialEq)]
#[repr(transparent)]
pub struct Other<T>(pub T);

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

{
    // Transmuting from `&[Other<u32>; 5]` to `&[u32; 5]`
    const ARR: &[u32; 5] = transmute_ref(
        &[Other(0), Other(127), Other(128), Other(129), Other(130)],
        // `infer_tw!().into_inner.array()` allows transmuting an arrays of wrappers
        // into an arraw of the values that are inside those wrappers.
        infer_tw!().into_inner.array(),
    );
    
    assert_eq!(*ARR, [0, 127, 128, 129, 130]);
}

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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.