Skip to content

Commit 55ba9e7

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 038f9e6 commit 55ba9e7

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

library/alloc/src/boxed.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ use crate::raw_vec::RawVec;
175175
#[cfg(not(no_global_oom_handling))]
176176
use crate::str::from_boxed_utf8_unchecked;
177177
#[cfg(not(no_global_oom_handling))]
178+
use crate::string::String;
179+
#[cfg(not(no_global_oom_handling))]
178180
use crate::vec::Vec;
179181

180182
#[unstable(feature = "thin_box", issue = "92791")]
@@ -1964,6 +1966,54 @@ impl<I> FromIterator<I> for Box<[I]> {
19641966
}
19651967
}
19661968

1969+
#[cfg(not(no_global_oom_handling))]
1970+
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
1971+
impl FromIterator<char> for Box<str> {
1972+
fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self {
1973+
String::from_iter(iter).into_boxed_str()
1974+
}
1975+
}
1976+
1977+
#[cfg(not(no_global_oom_handling))]
1978+
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
1979+
impl<'a> FromIterator<&'a char> for Box<str> {
1980+
fn from_iter<T: IntoIterator<Item = &'a char>>(iter: T) -> Self {
1981+
String::from_iter(iter).into_boxed_str()
1982+
}
1983+
}
1984+
1985+
#[cfg(not(no_global_oom_handling))]
1986+
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
1987+
impl<'a> FromIterator<&'a str> for Box<str> {
1988+
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
1989+
String::from_iter(iter).into_boxed_str()
1990+
}
1991+
}
1992+
1993+
#[cfg(not(no_global_oom_handling))]
1994+
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
1995+
impl FromIterator<String> for Box<str> {
1996+
fn from_iter<T: IntoIterator<Item = String>>(iter: T) -> Self {
1997+
String::from_iter(iter).into_boxed_str()
1998+
}
1999+
}
2000+
2001+
#[cfg(not(no_global_oom_handling))]
2002+
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
2003+
impl<A: Allocator> FromIterator<Box<str, A>> for Box<str> {
2004+
fn from_iter<T: IntoIterator<Item = Box<str, A>>>(iter: T) -> Self {
2005+
String::from_iter(iter).into_boxed_str()
2006+
}
2007+
}
2008+
2009+
#[cfg(not(no_global_oom_handling))]
2010+
#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")]
2011+
impl<'a> FromIterator<Cow<'a, str>> for Box<str> {
2012+
fn from_iter<T: IntoIterator<Item = Cow<'a, str>>>(iter: T) -> Self {
2013+
String::from_iter(iter).into_boxed_str()
2014+
}
2015+
}
2016+
19672017
#[cfg(not(no_global_oom_handling))]
19682018
#[stable(feature = "box_slice_clone", since = "1.3.0")]
19692019
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
@@ -62,6 +62,8 @@ use core::slice;
6262
use core::str::lossy;
6363
use core::str::pattern::Pattern;
6464

65+
#[cfg(not(no_global_oom_handling))]
66+
use crate::alloc::Allocator;
6567
#[cfg(not(no_global_oom_handling))]
6668
use crate::borrow::{Cow, ToOwned};
6769
use crate::boxed::Box;
@@ -2001,8 +2003,8 @@ impl FromIterator<String> for String {
20012003

20022004
#[cfg(not(no_global_oom_handling))]
20032005
#[stable(feature = "box_str2", since = "1.45.0")]
2004-
impl FromIterator<Box<str>> for String {
2005-
fn from_iter<I: IntoIterator<Item = Box<str>>>(iter: I) -> String {
2006+
impl<A: Allocator> FromIterator<Box<str, A>> for String {
2007+
fn from_iter<I: IntoIterator<Item = Box<str, A>>>(iter: I) -> String {
20062008
let mut buf = String::new();
20072009
buf.extend(iter);
20082010
buf
@@ -2083,8 +2085,8 @@ impl<'a> Extend<&'a str> for String {
20832085

20842086
#[cfg(not(no_global_oom_handling))]
20852087
#[stable(feature = "box_str2", since = "1.45.0")]
2086-
impl Extend<Box<str>> for String {
2087-
fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iter: I) {
2088+
impl<A: Allocator> Extend<Box<str, A>> for String {
2089+
fn extend<I: IntoIterator<Item = Box<str, A>>>(&mut self, iter: I) {
20882090
iter.into_iter().for_each(move |s| self.push_str(&s));
20892091
}
20902092
}

0 commit comments

Comments
 (0)