Skip to content

Commit

Permalink
Auto merge of rust-lang#99969 - calebsander:feature/collect-box-str, …
Browse files Browse the repository at this point in the history
…r=dtolnay

alloc: implement FromIterator for Box<str>

`Box<[T]>` implements `FromIterator<T>` using `Vec<T>` + `into_boxed_slice()`.
Add analogous `FromIterator` implementations for `Box<str>`
matching the current implementations for `String`.
Remove the `Global` allocator requirement for `FromIterator<Box<str>>` too.

ACP: rust-lang/libs-team#196
  • Loading branch information
bors committed May 18, 2024
2 parents b1ec1bd + c92c228 commit ce8c2dc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
48 changes: 48 additions & 0 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2080,6 +2080,54 @@ impl<I> FromIterator<I> for Box<[I]> {
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
impl FromIterator<char> for Box<str> {
fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self {
String::from_iter(iter).into_boxed_str()
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
impl<'a> FromIterator<&'a char> for Box<str> {
fn from_iter<T: IntoIterator<Item = &'a char>>(iter: T) -> Self {
String::from_iter(iter).into_boxed_str()
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
impl<'a> FromIterator<&'a str> for Box<str> {
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
String::from_iter(iter).into_boxed_str()
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
impl FromIterator<String> for Box<str> {
fn from_iter<T: IntoIterator<Item = String>>(iter: T) -> Self {
String::from_iter(iter).into_boxed_str()
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
impl<A: Allocator> FromIterator<Box<str, A>> for Box<str> {
fn from_iter<T: IntoIterator<Item = Box<str, A>>>(iter: T) -> Self {
String::from_iter(iter).into_boxed_str()
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
impl<'a> FromIterator<Cow<'a, str>> for Box<str> {
fn from_iter<T: IntoIterator<Item = Cow<'a, str>>>(iter: T) -> Self {
String::from_iter(iter).into_boxed_str()
}
}

#[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
10 changes: 6 additions & 4 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ use core::ptr;
use core::slice;
use core::str::pattern::Pattern;

#[cfg(not(no_global_oom_handling))]
use crate::alloc::Allocator;
#[cfg(not(no_global_oom_handling))]
use crate::borrow::{Cow, ToOwned};
use crate::boxed::Box;
Expand Down Expand Up @@ -2157,8 +2159,8 @@ impl FromIterator<String> for String {

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "box_str2", since = "1.45.0")]
impl FromIterator<Box<str>> for String {
fn from_iter<I: IntoIterator<Item = Box<str>>>(iter: I) -> String {
impl<A: Allocator> FromIterator<Box<str, A>> for String {
fn from_iter<I: IntoIterator<Item = Box<str, A>>>(iter: I) -> String {
let mut buf = String::new();
buf.extend(iter);
buf
Expand Down Expand Up @@ -2239,8 +2241,8 @@ impl<'a> Extend<&'a str> for String {

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "box_str2", since = "1.45.0")]
impl Extend<Box<str>> for String {
fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iter: I) {
impl<A: Allocator> Extend<Box<str, A>> for String {
fn extend<I: IntoIterator<Item = Box<str, A>>>(&mut self, iter: I) {
iter.into_iter().for_each(move |s| self.push_str(&s));
}
}
Expand Down

0 comments on commit ce8c2dc

Please sign in to comment.