From 852ff1fc7d74a7668441e0f354f8c477c95b676d Mon Sep 17 00:00:00 2001 From: Axary Date: Sat, 10 Nov 2018 11:43:39 +0100 Subject: [PATCH 1/2] add FromIterator to Box<[A]> --- src/liballoc/boxed.rs | 10 +++++++++- src/liballoc/boxed_test.rs | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 74354f605e5..e1e9617b491 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -73,7 +73,7 @@ use core::fmt; use core::future::Future; use core::hash::{Hash, Hasher}; -use core::iter::FusedIterator; +use core::iter::{Iterator, FromIterator, FusedIterator}; use core::marker::{Unpin, Unsize}; use core::mem; use core::pin::Pin; @@ -81,6 +81,7 @@ use core::ptr::{self, NonNull, Unique}; use core::task::{LocalWaker, Poll}; +use vec::Vec; use raw_vec::RawVec; use str::from_boxed_utf8_unchecked; @@ -699,6 +700,13 @@ impl, U: ?Sized> CoerceUnsized> for Box {} #[unstable(feature = "dispatch_from_dyn", issue = "0")] impl, U: ?Sized> DispatchFromDyn> for Box {} +#[unstable(feature = "boxed_slice_from_iter", issue = "0")] +impl FromIterator for Box<[A]> { + fn from_iter>(iter: T) -> Self { + iter.into_iter().collect::>().into_boxed_slice() + } +} + #[stable(feature = "box_slice_clone", since = "1.3.0")] impl Clone for Box<[T]> { fn clone(&self) -> Self { diff --git a/src/liballoc/boxed_test.rs b/src/liballoc/boxed_test.rs index 55995742a4a..f340ea01c5f 100644 --- a/src/liballoc/boxed_test.rs +++ b/src/liballoc/boxed_test.rs @@ -140,3 +140,11 @@ fn str_slice() { let boxed: Box = Box::from(s); assert_eq!(&*boxed, s) } + +#[test] +fn boxed_slice_from_iter() { + let iter = 0..100; + let boxed: Box<[u32]> = iter.collect(); + assert_eq!(boxed.len(), 100); + assert_eq!(boxed[7], 7); +} From ab55d9b5d33f324495509ab45c8386b97b74d04f Mon Sep 17 00:00:00 2001 From: Axary Date: Sun, 11 Nov 2018 10:45:16 +0100 Subject: [PATCH 2/2] change attribute to stable --- src/liballoc/boxed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index e1e9617b491..63b262d1f3d 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -700,7 +700,7 @@ impl, U: ?Sized> CoerceUnsized> for Box {} #[unstable(feature = "dispatch_from_dyn", issue = "0")] impl, U: ?Sized> DispatchFromDyn> for Box {} -#[unstable(feature = "boxed_slice_from_iter", issue = "0")] +#[stable(feature = "boxed_slice_from_iter", since = "1.32.0")] impl FromIterator for Box<[A]> { fn from_iter>(iter: T) -> Self { iter.into_iter().collect::>().into_boxed_slice()