Skip to content

Commit

Permalink
test that align_of handles alignment properly for the mid part
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Aug 2, 2018
1 parent 7b24d2b commit 6fb97a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,7 @@ impl<T> [T] {
return (self, &[], &[]);
} else {
let (left, rest) = self.split_at(offset);
// now `rest` is definitely aligned, so `from_raw_parts_mut` below is okay
let (us_len, ts_len) = rest.align_to_offsets::<U>();
return (left,
from_raw_parts(rest.as_ptr() as *const U, us_len),
Expand Down Expand Up @@ -1837,6 +1838,7 @@ impl<T> [T] {
return (self, &mut [], &mut []);
} else {
let (left, rest) = self.split_at_mut(offset);
// now `rest` is definitely aligned, so `from_raw_parts_mut` below is okay
let (us_len, ts_len) = rest.align_to_offsets::<U>();
let mut_ptr = rest.as_mut_ptr();
return (left,
Expand Down
14 changes: 14 additions & 0 deletions src/libcore/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,3 +986,17 @@ fn test_align_to_non_trivial() {
assert_eq!(aligned.len(), 4);
assert_eq!(prefix.len() + suffix.len(), 2);
}

#[test]
fn test_align_to_empty_mid() {
use core::mem;

// Make sure that we do not create empty unaligned slices for the mid part, even when the
// overall slice is too short to contain an aligned address.
let bytes = [1, 2, 3, 4, 5, 6, 7];
type Chunk = u32;
for offset in 0..4 {
let (_, mid, _) = unsafe { bytes[offset..offset+1].align_to::<Chunk>() };
assert_eq!(mid.as_ptr() as usize % mem::align_of::<Chunk>(), 0);
}
}

0 comments on commit 6fb97a6

Please sign in to comment.