test that align_of handles alignment properly for the mid part
This commit is contained in:
parent
7b24d2bb35
commit
6fb97a6c86
2 changed files with 16 additions and 0 deletions
|
@ -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),
|
||||
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue