Struct constmuck::TransmutableInto [−][src]
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 asTo
. - The alignment of
Fro
is less thanTo
(Foo
is allowed to be more aligned thanTo
).
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]);
}
pub const fn join<To2: ?Sized>(
self,
_other: TransmutableInto<To, To2>
) -> TransmutableInto<Fro, To2>
pub const fn join<To2: ?Sized>(
self,
_other: TransmutableInto<To, To2>
) -> TransmutableInto<Fro, To2>
Combines an TransmutableInto
with another to allow
casting from Fro
to To2
.
Without this you’d have to do Fro -> To -> To2
casts.
Example
use constmuck::{TransmutableInto, infer, infer_tw};
use constmuck::transmutable::{transmute_ref, transmute_slice};
use std::num::Wrapping;
const FOO: TransmutableInto<Wrapping<u8>, i8> = {
infer_tw!(Wrapping<u8>, u8)
.into_inner
.join(TransmutableInto::<_, i8>::pod(infer!()))
};
assert_eq!(transmute_ref(&Wrapping(255u8), FOO), &-1);
assert_eq!(
transmute_slice(&[Wrapping(255u8), Wrapping(0), Wrapping(1)], FOO),
&[-1, 0, 1],
);