Skip to content

Commit f481987

Browse files
committed
Auto merge of #53754 - RalfJung:slice_align_to, r=alexcrichton
stabilize slice_align_to This is very hard to implement correctly, and leads to [serious bugs](llogiq/bytecount#42) when done incorrectly. Moreover, this is needed to be able to run code that opportunistically exploits alignment on miri. So code using `align_to`/`align_to_mut` gets the benefit of a well-tested implementation *and* of being able to run in miri to test for (some kinds of) UB. This PR also clarifies the guarantee wrt. the middle part being as long as possible. Should the docs say under which circumstances the middle part could be shorter? Currently, that can only happen when running in miri.
2 parents e4ba1d4 + f4f1140 commit f481987

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/libcore/slice/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1739,8 +1739,9 @@ impl<T> [T] {
17391739
/// maintained.
17401740
///
17411741
/// This method splits the slice into three distinct slices: prefix, correctly aligned middle
1742-
/// slice of a new type, and the suffix slice. The middle slice will have the greatest length
1743-
/// possible for a given type and input slice.
1742+
/// slice of a new type, and the suffix slice. The method does a best effort to make the
1743+
/// middle slice the greatest length possible for a given type and input slice, but only
1744+
/// your algorithm's performance should depend on that, not its correctness.
17441745
///
17451746
/// This method has no purpose when either input element `T` or output element `U` are
17461747
/// zero-sized and will return the original slice without splitting anything.
@@ -1755,7 +1756,6 @@ impl<T> [T] {
17551756
/// Basic usage:
17561757
///
17571758
/// ```
1758-
/// # #![feature(slice_align_to)]
17591759
/// unsafe {
17601760
/// let bytes: [u8; 7] = [1, 2, 3, 4, 5, 6, 7];
17611761
/// let (prefix, shorts, suffix) = bytes.align_to::<u16>();
@@ -1764,7 +1764,7 @@ impl<T> [T] {
17641764
/// // less_efficient_algorithm_for_bytes(suffix);
17651765
/// }
17661766
/// ```
1767-
#[unstable(feature = "slice_align_to", issue = "44488")]
1767+
#[stable(feature = "slice_align_to", since = "1.30.0")]
17681768
pub unsafe fn align_to<U>(&self) -> (&[T], &[U], &[T]) {
17691769
// Note that most of this function will be constant-evaluated,
17701770
if ::mem::size_of::<U>() == 0 || ::mem::size_of::<T>() == 0 {
@@ -1792,8 +1792,9 @@ impl<T> [T] {
17921792
/// maintained.
17931793
///
17941794
/// This method splits the slice into three distinct slices: prefix, correctly aligned middle
1795-
/// slice of a new type, and the suffix slice. The middle slice will have the greatest length
1796-
/// possible for a given type and input slice.
1795+
/// slice of a new type, and the suffix slice. The method does a best effort to make the
1796+
/// middle slice the greatest length possible for a given type and input slice, but only
1797+
/// your algorithm's performance should depend on that, not its correctness.
17971798
///
17981799
/// This method has no purpose when either input element `T` or output element `U` are
17991800
/// zero-sized and will return the original slice without splitting anything.
@@ -1808,7 +1809,6 @@ impl<T> [T] {
18081809
/// Basic usage:
18091810
///
18101811
/// ```
1811-
/// # #![feature(slice_align_to)]
18121812
/// unsafe {
18131813
/// let mut bytes: [u8; 7] = [1, 2, 3, 4, 5, 6, 7];
18141814
/// let (prefix, shorts, suffix) = bytes.align_to_mut::<u16>();
@@ -1817,7 +1817,7 @@ impl<T> [T] {
18171817
/// // less_efficient_algorithm_for_bytes(suffix);
18181818
/// }
18191819
/// ```
1820-
#[unstable(feature = "slice_align_to", issue = "44488")]
1820+
#[stable(feature = "slice_align_to", since = "1.30.0")]
18211821
pub unsafe fn align_to_mut<U>(&mut self) -> (&mut [T], &mut [U], &mut [T]) {
18221822
// Note that most of this function will be constant-evaluated,
18231823
if ::mem::size_of::<U>() == 0 || ::mem::size_of::<T>() == 0 {

src/libcore/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#![feature(try_from)]
3535
#![feature(try_trait)]
3636
#![feature(exact_chunks)]
37-
#![feature(slice_align_to)]
3837
#![feature(align_offset)]
3938
#![feature(reverse_bits)]
4039
#![feature(inner_deref)]

0 commit comments

Comments
 (0)