Skip to content

Commit c92c228

Browse files
committed
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.
1 parent 7c4ac06 commit c92c228

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

library/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> {

library/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;
@@ -2155,8 +2157,8 @@ impl FromIterator<String> for String {
21552157

21562158
#[cfg(not(no_global_oom_handling))]
21572159
#[stable(feature = "box_str2", since = "1.45.0")]
2158-
impl FromIterator<Box<str>> for String {
2159-
fn from_iter<I: IntoIterator<Item = Box<str>>>(iter: I) -> String {
2160+
impl<A: Allocator> FromIterator<Box<str, A>> for String {
2161+
fn from_iter<I: IntoIterator<Item = Box<str, A>>>(iter: I) -> String {
21602162
let mut buf = String::new();
21612163
buf.extend(iter);
21622164
buf
@@ -2237,8 +2239,8 @@ impl<'a> Extend<&'a str> for String {
22372239

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

0 commit comments

Comments
 (0)