Skip to content

Commit

Permalink
Merge pull request #30 from V0ldek/v0ldek/alignment-size-function
Browse files Browse the repository at this point in the history
feat: `alignment_size` function for aligned types
  • Loading branch information
V0ldek authored May 31, 2022
2 parents 3d9af84 + 9e21b3a commit 8e14a25
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,14 @@ impl<A: Alignment> AlignedBytes<A> {
}

/// Return the size of the alignment in bytes.
///
/// ## Note
/// This does not reflect the actual maximal alignment,
/// only the guarantee provided by `A`, which may be lower than
/// the actual alignment.
#[must_use]
#[inline(always)]
pub fn alignment_size() -> usize {
pub fn alignment_size(&self) -> usize {
A::size()
}

Expand Down Expand Up @@ -282,4 +287,11 @@ mod tests {
let bytes: AlignedBytes<alignment::TwoTo<15>> = AlignedBytes::new_initialize(2137, |_| 1);
assert_aligned(bytes.as_ptr(), 2usize.pow(15));
}

#[test]
fn alignment_size_equal_to_alignment_type() {
let bytes: AlignedBytes<alignment::TwoTo<7>> = AlignedBytes::new_zeroed(1024);

assert_eq!(128, bytes.alignment_size());
}
}
26 changes: 26 additions & 0 deletions src/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ impl<A: Alignment> AlignedBlock<A> {
pub fn is_empty(&self) -> bool {
self.slice.is_empty()
}

/// Return the size of the alignment in bytes. Equal to [`A::size()`](`Alignment::size`).
///
/// ## Note
/// This does not reflect the actual maximal alignment,
/// only the guarantee provided by `A`, which may be lower than
/// the actual alignment.
#[must_use]
#[inline(always)]
pub fn alignment_size(&self) -> usize {
A::size()
}
}

impl<'a, A: Alignment> Iterator for AlignedBlockIterator<'a, A> {
Expand Down Expand Up @@ -90,3 +102,17 @@ impl<'a, A: Alignment> Iterator for AlignedBlockIterator<'a, A> {
impl<A: Alignment> ExactSizeIterator for AlignedBlockIterator<'_, A> {}

impl<A: Alignment> FusedIterator for AlignedBlockIterator<'_, A> {}

#[cfg(test)]
mod tests {
use crate::{alignment, AlignedBytes};

#[test]
fn alignment_size_equal_to_alignment_type() {
let bytes: AlignedBytes<alignment::TwoTo<7>> = AlignedBytes::new_zeroed(1024);
let mut iter = bytes.iter_blocks();
let block = iter.next().unwrap();

assert_eq!(128, block.alignment_size());
}
}
22 changes: 21 additions & 1 deletion src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ impl<A: Alignment> AlignedSlice<A> {
unsafe { std::mem::transmute(&self[offset_in_bytes..]) }
}

/// Return the size of the alignment in bytes.
///
/// ## Note
/// This does not reflect the actual maximal alignment,
/// only the guarantee provided by `A`, which may be lower than
/// the actual alignment.
#[must_use]
#[inline(always)]
pub fn alignment_size(&self) -> usize {
A::size()
}

/// Return an iterator over consecutive aligned blocks of the slice.
#[must_use]
#[inline]
Expand Down Expand Up @@ -232,7 +244,7 @@ impl<A: Alignment> Default for &mut AlignedSlice<A> {
#[cfg(test)]
mod tests {
use crate::test::assert_aligned;
use crate::{alignment, AlignedSlice};
use crate::{alignment, AlignedBytes, AlignedSlice};

#[test]
fn empty_slice_is_aligned() {
Expand All @@ -245,4 +257,12 @@ mod tests {
let empty: &mut AlignedSlice<alignment::Eight> = Default::default();
assert_aligned(empty.as_ptr(), 8);
}

#[test]
fn alignment_size_equal_to_alignment_type() {
let bytes: AlignedBytes<alignment::TwoTo<7>> = AlignedBytes::new_zeroed(1024);
let slice: &AlignedSlice<alignment::TwoTo<7>> = &bytes;

assert_eq!(128, slice.alignment_size());
}
}

0 comments on commit 8e14a25

Please sign in to comment.