Struct dungeon_cell::Aligned

source ·
#[repr(C)]
pub struct Aligned<T, A: ValidAlignment> { pub value: T, /* private fields */ }
Expand description

Force minimum alignment of a type.

Invariants

The wrapped value is guaranteed, for specifically unsafe code, to have a minimum alignment of A::VALUE bytes. The wrapped value is also guaranteed to have a zero offset from the start of the alignment block. This makes it sound to construct a reference the wrapped value for a different type with an alignment of maximum A::VALUE bytes, given the other unsafe rules are followed.

Additionally the layout of this struct is #[repr(C)] with a single field value of type T and a private zero sized type with an alignment of A::VALUE bytes.

Examples

use std::mem::*;
use dungeon_cell::{Aligned, Alignment};

// 1 byte aligned buffer
let x: [u8; 4] = 1234567890u32.to_ne_bytes();
assert_eq!(size_of_val(&x), 4);
assert_eq!(align_of_val(&x), 1);

// 4 byte aligned buffer
let y = Aligned::<_, Alignment<4>>::new(x);
assert_eq!(size_of_val(&y), 4);
assert_eq!(align_of_val(&y), 4);

// this cast back to a u32 is sound because Aligned
// makes the value field aligned to 4 bytes.
let z = &y.value as *const [u8; 4] as *const u32;
let z = unsafe { &*z };
assert_eq!(z, &1234567890);

Fields§

§value: T

Stored value.

This field is guaranteed to be aligned to A bytes.

Implementations§

source§

impl<T, A: ValidAlignment> Aligned<T, A>

source

pub const fn new(value: T) -> Self

Construct a new aligned value.

Examples
use dungeon_cell::{Aligned, Alignment};

let a = Aligned::<_, Alignment<16>>::new(4);

Trait Implementations§

source§

impl<T, A: ValidAlignment> Deref for Aligned<T, A>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T, A: ValidAlignment> DerefMut for Aligned<T, A>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<T, A> RefUnwindSafe for Aligned<T, A>where T: RefUnwindSafe,

§

impl<T, A> Send for Aligned<T, A>where T: Send,

§

impl<T, A> Sync for Aligned<T, A>where T: Sync,

§

impl<T, A> Unpin for Aligned<T, A>where T: Unpin,

§

impl<T, A> UnwindSafe for Aligned<T, A>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, W> HasTypeWitness<W> for Twhere W: MakeTypeWitness<Arg = T>, T: ?Sized,

§

const WITNESS: W = W::MAKE

A constant of the type witness
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.