From 55ba9e72a5929f6e795dc8e2b2215bf6fc176933 Mon Sep 17 00:00:00 2001 From: Caleb Sander Date: Sat, 30 Jul 2022 16:20:25 -0700 Subject: [PATCH] alloc: implement FromIterator for Box Box<[T]> implements FromIterator using Vec + into_boxed_slice(). Add analogous FromIterator implementations for Box matching the current implementations for String. Remove the Global allocator requirement for FromIterator> too. --- library/alloc/src/boxed.rs | 50 +++++++++++++++++++++++++++++++++++++ library/alloc/src/string.rs | 10 +++++--- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index c1ceeb0deb837..9a46a05709f83 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -175,6 +175,8 @@ use crate::raw_vec::RawVec; #[cfg(not(no_global_oom_handling))] use crate::str::from_boxed_utf8_unchecked; #[cfg(not(no_global_oom_handling))] +use crate::string::String; +#[cfg(not(no_global_oom_handling))] use crate::vec::Vec; #[unstable(feature = "thin_box", issue = "92791")] @@ -1964,6 +1966,54 @@ impl FromIterator for Box<[I]> { } } +#[cfg(not(no_global_oom_handling))] +#[stable(feature = "boxed_str_from_iter", since = "CURRENT_RUSTC_VERSION")] +impl FromIterator for Box { + fn from_iter>(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 { + fn from_iter>(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 { + fn from_iter>(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 for Box { + fn from_iter>(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> for Box { + fn from_iter>>(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> for Box { + fn from_iter>>(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 Clone for Box<[T], A> { diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index a5118e5333b82..e9096d4a322a2 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -62,6 +62,8 @@ use core::slice; use core::str::lossy; 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; @@ -2001,8 +2003,8 @@ impl FromIterator for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "box_str2", since = "1.45.0")] -impl FromIterator> for String { - fn from_iter>>(iter: I) -> String { +impl FromIterator> for String { + fn from_iter>>(iter: I) -> String { let mut buf = String::new(); buf.extend(iter); buf @@ -2083,8 +2085,8 @@ impl<'a> Extend<&'a str> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "box_str2", since = "1.45.0")] -impl Extend> for String { - fn extend>>(&mut self, iter: I) { +impl Extend> for String { + fn extend>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(&s)); } }