Skip to content

Commit c226e8f

Browse files
authored
Rollup 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 72d7897 + 55ba9e7 commit c226e8f

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

library/alloc/src/boxed.rs

Lines changed: 48 additions & 0 deletions
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> {

library/alloc/src/string.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ use core::str::pattern::Pattern;
6161
#[cfg(not(no_global_oom_handling))]
6262
use core::str::Utf8Chunks;
6363

64+
#[cfg(not(no_global_oom_handling))]
65+
use crate::alloc::Allocator;
6466
#[cfg(not(no_global_oom_handling))]
6567
use crate::borrow::{Cow, ToOwned};
6668
use crate::boxed::Box;
@@ -2153,8 +2155,8 @@ impl FromIterator<String> for String {
21532155

21542156
#[cfg(not(no_global_oom_handling))]
21552157
#[stable(feature = "box_str2", since = "1.45.0")]
2156-
impl FromIterator<Box<str>> for String {
2157-
fn from_iter<I: IntoIterator<Item = Box<str>>>(iter: I) -> String {
2158+
impl<A: Allocator> FromIterator<Box<str, A>> for String {
2159+
fn from_iter<I: IntoIterator<Item = Box<str, A>>>(iter: I) -> String {
21582160
let mut buf = String::new();
21592161
buf.extend(iter);
21602162
buf
@@ -2235,8 +2237,8 @@ impl<'a> Extend<&'a str> for String {
22352237

22362238
#[cfg(not(no_global_oom_handling))]
22372239
#[stable(feature = "box_str2", since = "1.45.0")]
2238-
impl Extend<Box<str>> for String {
2239-
fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iter: I) {
2240+
impl<A: Allocator> Extend<Box<str, A>> for String {
2241+
fn extend<I: IntoIterator<Item = Box<str, A>>>(&mut self, iter: I) {
22402242
iter.into_iter().for_each(move |s| self.push_str(&s));
22412243
}
22422244
}

0 commit comments

Comments
 (0)