Skip to content

Commit

Permalink
Auto merge of rust-lang#116607 - clarfonthey:box-into-iter, r=<try>
Browse files Browse the repository at this point in the history
IntoIterator for Box<[T]>

ACP: rust-lang/libs-team#263

Recommendation per ACP: this should receive a crater run to gauge impact. If there's no impact, it can be merged as-is, but otherwise it will need a similar edition-based workaround to the array implementation.

In addition to what was proposed by the ACP, this also adds `IntoIterator for &Box<[T]>` and `IntoIterator for &mut Box<[T]>` to ensure that those work as expected. I also already had to change at least one line in the compiler to account for this change, which isn't a good sign toward whether edition-specific mitigations may be needed, but we'll see.
  • Loading branch information
bors committed Oct 11, 2023
2 parents 6d05c43 + b4c7843 commit ec8d137
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl<'a, T> IntoIterator for &'a P<[T]> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.ptr.into_iter()
self.ptr.iter()
}
}

Expand Down
38 changes: 38 additions & 0 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ use core::ops::{
};
use core::pin::Pin;
use core::ptr::{self, NonNull, Unique};
use core::slice;
use core::task::{Context, Poll};

#[cfg(not(no_global_oom_handling))]
Expand All @@ -175,6 +176,7 @@ use crate::raw_vec::RawVec;
use crate::str::from_boxed_utf8_unchecked;
#[cfg(not(no_global_oom_handling))]
use crate::string::String;
use crate::vec;
#[cfg(not(no_global_oom_handling))]
use crate::vec::Vec;

Expand Down Expand Up @@ -2036,6 +2038,42 @@ impl<I> FromIterator<I> for Box<[I]> {
}
}

#[stable(feature = "boxed_slice_into_iter", since = "CURRENT_RUSTC_VERSION")]
impl<I, A: Allocator> !Iterator for Box<[I], A> {}

#[stable(feature = "boxed_slice_into_iter", since = "CURRENT_RUSTC_VERSION")]
impl<I, A: Allocator> IntoIterator for Box<[I], A> {
type IntoIter = vec::IntoIter<I, A>;
type Item = I;
fn into_iter(self) -> vec::IntoIter<I, A> {
self.into_vec().into_iter()
}
}

#[stable(feature = "boxed_slice_into_iter", since = "CURRENT_RUSTC_VERSION")]
impl<'a, I, A: Allocator> !Iterator for &'a Box<[I], A> {}

#[stable(feature = "boxed_slice_into_iter", since = "CURRENT_RUSTC_VERSION")]
impl<'a, I, A: Allocator> IntoIterator for &'a Box<[I], A> {
type IntoIter = slice::Iter<'a, I>;
type Item = &'a I;
fn into_iter(self) -> slice::Iter<'a, I> {
self.iter()
}
}

#[stable(feature = "boxed_slice_into_iter", since = "CURRENT_RUSTC_VERSION")]
impl<'a, I, A: Allocator> !Iterator for &'a mut Box<[I], A> {}

#[stable(feature = "boxed_slice_into_iter", since = "CURRENT_RUSTC_VERSION")]
impl<'a, I, A: Allocator> IntoIterator for &'a mut Box<[I], A> {
type IntoIter = slice::IterMut<'a, I>;
type Item = &'a mut I;
fn into_iter(self) -> slice::IterMut<'a, I> {
self.iter_mut()
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "box_slice_clone", since = "1.3.0")]
impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> {
Expand Down
3 changes: 3 additions & 0 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use crate::ptr::{self, invalid, invalid_mut, NonNull};

use super::{from_raw_parts, from_raw_parts_mut};

#[stable(feature = "boxed_slice_into_iter", since = "CURRENT_RUSTC_VERSION")]
impl<T> !Iterator for [T] {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> IntoIterator for &'a [T] {
type Item = &'a T;
Expand Down

0 comments on commit ec8d137

Please sign in to comment.