Trait Aligned

Source
pub unsafe trait Aligned {
    const ALIGNMENT: usize;
}
Expand description

Types (may not be pointers) that can be used in EnumPtr.

§Safety

  • T must be exactly one-pointer wide.
  • T’s pointee must be aligned by ALIGNMENT (T’s low bits are zeros).

For example, raw pointers are not guaranteed to be aligned, so implementing this trait for them is unsound.

§Examples

use enum_ptr::{Aligned, Compact, EnumPtr};

// It's your responsibility to ensure `MyPtr` is always aligned.
struct MyPtr<T>(*const T);

unsafe impl<T> Aligned for MyPtr<T> {
    const ALIGNMENT: usize = std::mem::align_of::<T>();
}

#[derive(EnumPtr)]
#[repr(C, usize)]
enum Foo {
    A(MyPtr<i64>),
    B(MyPtr<u64>),
}

Required Associated Constants§

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> Aligned for Option<&T>

Source§

impl<T> Aligned for Option<&mut T>

Source§

impl<T> Aligned for Option<Box<T>>

Source§

impl<T> Aligned for Option<Rc<T>>

On top of the safety reasoning of impl Align for Rc<T>, implementing Aligned for Option<Rc<T>> is safe as well. Rc holds a pointer to its RcBox as NonNull<RcBox<T>> And the Option-ed type (i.e. Option<NonNull<RcBox<T>>>) is explicitly guaranteed to be same as the original type in terms of size and alignment.

Source§

impl<T> Aligned for Option<Arc<T>>

On top of the safety reasoning of impl Align for Arc<T>, implementing Aligned for Option<Arc<T>> is safe as well. Arc holds a pointer to its ArcInner as NonNull<ArcInner<T>> And the Option-ed type (i.e. Option<NonNull<ArcInner<T>>>) is explicitly guaranteed to be same as the original type in terms of size and alignment.

Source§

impl<T> Aligned for &T

Source§

impl<T> Aligned for &mut T

Source§

impl<T> Aligned for Box<T>

Source§

impl<T> Aligned for Rc<T>

Implementing Aligned for Rc relies on some undocumented assumptions on its internal representation. However, it’s very unlikely for these assumptions to be violated in practice. RcBox is marked as repr(C) and reference counters are typed as usize. Thus, its alignment is defined as the highest of its members (i.e. among max of usize and T). Lastly, it’s unconceivable for the type to be lifted with repr(C) as commented or to start to use shorter integers as counters.

Source§

impl<T> Aligned for Arc<T>

Implementing Aligned for Arc relies on some undocumented assumptions on its internal representation. However, it’s very unlikely for these assumptions to be violated in practice. ArcInner is marked as repr(C) and reference counters are typed as usize. Thus, its alignment is defined as the highest of its members (i.e. among max of usize and T). Lastly, it’s unconceivable for the type to be lifted with repr(C) as commented or to start to use shorter integers as counters.

Implementors§

Source§

impl Aligned for Unit

Source§

const ALIGNMENT: usize = 18_446_744_073_709_551_615usize

Source§

impl<const N: isize> Aligned for ShiftIsize<N>

Source§

impl<const N: usize> Aligned for ShiftUsize<N>