diff options
| author | bors <bors@rust-lang.org> | 2024-05-19 02:13:06 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-05-19 02:13:06 +0000 |
| commit | bfa3635df920454cdf03f6d268dfaac769375df3 (patch) | |
| tree | fc8538014f461a0fb508c554738b513d734db18c /library/alloc/src | |
| parent | 7690f29bdb6460ab525a8e8919420a4675a93386 (diff) | |
| parent | c92c22826015168581f82e8b0f870a8cef9209b9 (diff) | |
| download | rust-bfa3635df920454cdf03f6d268dfaac769375df3.tar.gz rust-bfa3635df920454cdf03f6d268dfaac769375df3.zip | |
Auto merge of #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: https://github.com/rust-lang/libs-team/issues/196
Diffstat (limited to 'library/alloc/src')
| -rw-r--r-- | library/alloc/src/boxed.rs | 48 | ||||
| -rw-r--r-- | library/alloc/src/string.rs | 10 |
2 files changed, 54 insertions, 4 deletions
diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index e5d62447eb2..f1a6df94e11 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -2081,6 +2081,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> { fn clone(&self) -> Self { diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 335a1a8ffb3..36078da7c35 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -60,6 +60,8 @@ 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; use crate::collections::TryReserveError; @@ -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 @@ -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)); } } |
