pub unsafe trait Aligned {
const ALIGNMENT: usize;
}Expand description
Types (may not be pointers) that can be used in EnumPtr.
§Safety
Tmust be exactly one-pointer wide.T’s pointee must be aligned byALIGNMENT(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<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.
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.
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 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.
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.
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.