Skip to content

Commit b3ed7df

Browse files
committed
Auto merge of rust-lang#99969 - calebsander:feature/collect-box-str, 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
2 parents f5c17c8 + 8e59cbf commit b3ed7df

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

alloc/src/boxed.rs

+48
Original file line numberDiff line numberDiff line change
@@ -2080,6 +2080,54 @@ impl<I> FromIterator<I> for Box<[I]> {
20802080
}
20812081
}
20822082

2083+
#[cfg(not(no_global_oom_handling))]
2084+
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
2085+
impl FromIterator<char> for Box<str> {
2086+
fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self {
2087+
String::from_iter(iter).into_boxed_str()
2088+
}
2089+
}
2090+
2091+
#[cfg(not(no_global_oom_handling))]
2092+
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
2093+
impl<'a> FromIterator<&'a char> for Box<str> {
2094+
fn from_iter<T: IntoIterator<Item = &'a char>>(iter: T) -> Self {
2095+
String::from_iter(iter).into_boxed_str()
2096+
}
2097+
}
2098+
2099+
#[cfg(not(no_global_oom_handling))]
2100+
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
2101+
impl<'a> FromIterator<&'a str> for Box<str> {
2102+
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
2103+
String::from_iter(iter).into_boxed_str()
2104+
}
2105+
}
2106+
2107+
#[cfg(not(no_global_oom_handling))]
2108+
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
2109+
impl FromIterator<String> for Box<str> {
2110+
fn from_iter<T: IntoIterator<Item = String>>(iter: T) -> Self {
2111+
String::from_iter(iter).into_boxed_str()
2112+
}
2113+
}
2114+
2115+
#[cfg(not(no_global_oom_handling))]
2116+
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
2117+
impl<A: Allocator> FromIterator<Box<str, A>> for Box<str> {
2118+
fn from_iter<T: IntoIterator<Item = Box<str, A>>>(iter: T) -> Self {
2119+
String::from_iter(iter).into_boxed_str()
2120+
}
2121+
}
2122+
2123+
#[cfg(not(no_global_oom_handling))]
2124+
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
2125+
impl<'a> FromIterator<Cow<'a, str>> for Box<str> {
2126+
fn from_iter<T: IntoIterator<Item = Cow<'a, str>>>(iter: T) -> Self {
2127+
String::from_iter(iter).into_boxed_str()
2128+
}
2129+
}
2130+
20832131
#[cfg(not(no_global_oom_handling))]
20842132
#[stable(feature = "box_slice_clone", since = "1.3.0")]
20852133
impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> {

alloc/src/string.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ use core::ptr;
5959
use core::slice;
6060
use core::str::pattern::Pattern;
6161

62+
#[cfg(not(no_global_oom_handling))]
63+
use crate::alloc::Allocator;
6264
#[cfg(not(no_global_oom_handling))]
6365
use crate::borrow::{Cow, ToOwned};
6466
use crate::boxed::Box;
@@ -2157,8 +2159,8 @@ impl FromIterator<String> for String {
21572159

21582160
#[cfg(not(no_global_oom_handling))]
21592161
#[stable(feature = "box_str2", since = "1.45.0")]
2160-
impl FromIterator<Box<str>> for String {
2161-
fn from_iter<I: IntoIterator<Item = Box<str>>>(iter: I) -> String {
2162+
impl<A: Allocator> FromIterator<Box<str, A>> for String {
2163+
fn from_iter<I: IntoIterator<Item = Box<str, A>>>(iter: I) -> String {
21622164
let mut buf = String::new();
21632165
buf.extend(iter);
21642166
buf
@@ -2239,8 +2241,8 @@ impl<'a> Extend<&'a str> for String {
22392241

22402242
#[cfg(not(no_global_oom_handling))]
22412243
#[stable(feature = "box_str2", since = "1.45.0")]
2242-
impl Extend<Box<str>> for String {
2243-
fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iter: I) {
2244+
impl<A: Allocator> Extend<Box<str, A>> for String {
2245+
fn extend<I: IntoIterator<Item = Box<str, A>>>(&mut self, iter: I) {
22442246
iter.into_iter().for_each(move |s| self.push_str(&s));
22452247
}
22462248
}

0 commit comments

Comments
 (0)