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
22 changes: 15 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,13 @@ impl<A, T> Aligned<A, [T]>
where
A: Alignment,
{
#[inline(always)]
fn is_index_aligned(index: usize) -> bool {
use core::mem::size_of;

(index * size_of::<T>()) % A::ALIGN == 0
}
#[inline(always)]
fn check_start_index(index: usize) {
if !Self::is_index_aligned(index) {
panic!("Unaligned start index");
Expand All @@ -177,6 +179,7 @@ where
{
type Output = Aligned<A, [T]>;

#[inline(always)]
fn index(&self, range: ops::RangeFrom<usize>) -> &Aligned<A, [T]> {
Self::check_start_index(range.start);
unsafe { &*(&self.value[range] as *const [T] as *const Aligned<A, [T]>) }
Expand Down Expand Up @@ -211,6 +214,7 @@ where
{
type Output = Aligned<A, [T]>;

#[inline(always)]
fn index(&self, range: ops::RangeInclusive<usize>) -> &Aligned<A, [T]> {
Self::check_start_index(*range.start());
unsafe { &*(&self.value[range] as *const [T] as *const Aligned<A, [T]>) }
Expand All @@ -223,6 +227,7 @@ where
{
type Output = Aligned<A, [T]>;

#[inline(always)]
fn index(&self, range: ops::Range<usize>) -> &Aligned<A, [T]> {
Self::check_start_index(range.start);
unsafe { &*(&self.value[range] as *const [T] as *const Aligned<A, [T]>) }
Expand All @@ -244,6 +249,7 @@ impl<A, T> ops::IndexMut<ops::RangeFrom<usize>> for Aligned<A, [T]>
where
A: Alignment,
{
#[inline(always)]
fn index_mut(&mut self, range: ops::RangeFrom<usize>) -> &mut Aligned<A, [T]> {
Self::check_start_index(range.start);
unsafe { &mut *(&mut self.value[range] as *mut [T] as *mut Aligned<A, [T]>) }
Expand Down Expand Up @@ -272,6 +278,7 @@ impl<A, T> ops::IndexMut<ops::RangeInclusive<usize>> for Aligned<A, [T]>
where
A: Alignment,
{
#[inline(always)]
fn index_mut(&mut self, range: ops::RangeInclusive<usize>) -> &mut Aligned<A, [T]> {
Self::check_start_index(*range.start());
unsafe { &mut *(&mut self.value[range] as *mut [T] as *mut Aligned<A, [T]>) }
Expand All @@ -282,6 +289,7 @@ impl<A, T> ops::IndexMut<ops::Range<usize>> for Aligned<A, [T]>
where
A: Alignment,
{
#[inline(always)]
fn index_mut(&mut self, range: ops::Range<usize>) -> &mut Aligned<A, [T]> {
Self::check_start_index(range.start);
unsafe { &mut *(&mut self.value[range] as *mut [T] as *mut Aligned<A, [T]>) }
Expand Down Expand Up @@ -390,7 +398,7 @@ where
}
}

impl<A, T> Debug for Aligned<A, T>
impl<A, T: ?Sized> Debug for Aligned<A, T>
where
A: Alignment,
T: Debug,
Expand All @@ -400,7 +408,7 @@ where
}
}

impl<A, T> Display for Aligned<A, T>
impl<A, T: ?Sized> Display for Aligned<A, T>
where
A: Alignment,
T: Display,
Expand All @@ -410,7 +418,7 @@ where
}
}

impl<A, T> PartialEq for Aligned<A, T>
impl<A, T: ?Sized> PartialEq for Aligned<A, T>
where
A: Alignment,
T: PartialEq,
Expand All @@ -420,14 +428,14 @@ where
}
}

impl<A, T> Eq for Aligned<A, T>
impl<A, T: ?Sized> Eq for Aligned<A, T>
where
A: Alignment,
T: Eq,
{
}

impl<A, T> Hash for Aligned<A, T>
impl<A, T: ?Sized> Hash for Aligned<A, T>
where
A: Alignment,
T: Hash,
Expand All @@ -437,7 +445,7 @@ where
}
}

impl<A, T> Ord for Aligned<A, T>
impl<A, T: ?Sized> Ord for Aligned<A, T>
where
A: Alignment,
T: Ord,
Expand All @@ -447,7 +455,7 @@ where
}
}

impl<A, T> PartialOrd for Aligned<A, T>
impl<A, T: ?Sized> PartialOrd for Aligned<A, T>
where
A: Alignment,
T: PartialOrd,
Expand Down