diff options
| author | Ralf Jung <post@ralfj.de> | 2018-08-02 16:51:54 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2018-08-02 17:32:24 +0200 |
| commit | 6fb97a6c86e792552d330c7ab9db6f39ba28efb1 (patch) | |
| tree | ad838923aca82e197b1334616e5c221b03aeaee5 /src/libcore | |
| parent | 7b24d2bb351efeea9c0109d8a662b1038fe8f539 (diff) | |
| download | rust-6fb97a6c86e792552d330c7ab9db6f39ba28efb1.tar.gz rust-6fb97a6c86e792552d330c7ab9db6f39ba28efb1.zip | |
test that align_of handles alignment properly for the mid part
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/slice/mod.rs | 2 | ||||
| -rw-r--r-- | src/libcore/tests/slice.rs | 14 |
2 files changed, 16 insertions, 0 deletions
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index eeb171ebb9b..f9de78c6cdf 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -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, diff --git a/src/libcore/tests/slice.rs b/src/libcore/tests/slice.rs index 7968521f7b4..b087ec81f59 100644 --- a/src/libcore/tests/slice.rs +++ b/src/libcore/tests/slice.rs @@ -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); + } +} |
