Struct constmuck::TransmutableInto[][src]

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

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

Related: transmutable module.

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 convertible to To.

Pointers to Fro must be soundly convertible to point to To, eg: transmuting &Fro to &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.

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.