From c92c22826015168581f82e8b0f870a8cef9209b9 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 | 48 +++++++++++++++++++++++++++++++++++++ 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 e5d62447eb20e..f1a6df94e1179 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -2080,6 +2080,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 2a859ad55eed2..d9a03d300530c 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -59,6 +59,8 @@ use core::ptr; 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; @@ -2155,8 +2157,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 @@ -2237,8 +2239,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)); } }