Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ pub const fn Aligned<A, T>(value: T) -> Aligned<A, T> {
}
}

impl<A, T> From<T> for Aligned<A, T>
where
A: Alignment,
{
fn from(value: T) -> Self {
Aligned(value)
}
}

impl<A, T> ops::Deref for Aligned<A, T>
where
A: Alignment,
Expand Down Expand Up @@ -297,6 +306,42 @@ where
}
}

impl<A, T, const N: usize> AsRef<Aligned<A, [T]>> for Aligned<A, [T; N]>
where
A: Alignment,
{
fn as_ref(&self) -> &Aligned<A, [T]> {
self
}
}

impl<A, T, const N: usize> AsMut<Aligned<A, [T]>> for Aligned<A, [T; N]>
where
A: Alignment,
{
fn as_mut(&mut self) -> &mut Aligned<A, [T]> {
self
}
}

impl<A, T, const N: usize> AsRef<[T]> for Aligned<A, [T; N]>
where
A: Alignment,
{
fn as_ref(&self) -> &[T] {
&self.value[..]
}
}

impl<A, T, const N: usize> AsMut<[T]> for Aligned<A, [T; N]>
where
A: Alignment,
{
fn as_mut(&mut self) -> &mut [T] {
&mut self.value[..]
}
}

impl<A, T> AsSlice for Aligned<A, T>
where
A: Alignment,
Expand Down Expand Up @@ -989,3 +1034,25 @@ fn test_range_from_out_of_bounds() {
let a: &Aligned<A2, [u8]> = &Aligned::<A2, _>([0u8; 4]);
let _ = &a[6..];
}

#[test]
fn test_from() {
let a: Aligned<A4, _> = [1, 2, 3].into();
assert_eq!(*a, [1, 2, 3]);
assert_eq!(core::mem::align_of_val(&a), 4);
}

#[test]
fn array_as_ref() {
let array = Aligned::<A4, _>([1, 2, 3]);
let slice: &[u8] = array.as_ref();
assert_eq!(slice.as_ptr(), array.as_ptr());
}

#[test]
fn array_as_mut() {
let mut array = Aligned::<A4, _>([1, 2, 3]);
let slice: &mut [u8] = array.as_mut();
slice[0] = 4;
assert_eq!(slice.as_ptr(), array.as_ptr());
}