mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-15 20:45:45 +03:00
test that align_of handles alignment properly for the mid part
This commit is contained in:
@@ -1785,6 +1785,7 @@ pub unsafe fn align_to<U>(&self) -> (&[T], &[U], &[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 @@ pub unsafe fn align_to_mut<U>(&mut self) -> (&mut [T], &mut [U], &mut [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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user