Rollup merge of #55843 - Axary:master, r=sfackler

add FromIterator<A> to Box<[A]>
This commit is contained in:
kennytm 2018-11-13 13:03:12 +08:00
commit 196db96d8b
No known key found for this signature in database
GPG Key ID: FEF6C8051D0E013C
2 changed files with 17 additions and 1 deletions

View File

@ -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<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T> {}
#[stable(feature = "boxed_slice_from_iter", since = "1.32.0")]
impl<A> FromIterator<A> for Box<[A]> {
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
iter.into_iter().collect::<Vec<_>>().into_boxed_slice()
}
}
#[stable(feature = "box_slice_clone", since = "1.3.0")]
impl<T: Clone> Clone for Box<[T]> {
fn clone(&self) -> Self {

View File

@ -140,3 +140,11 @@ fn str_slice() {
let boxed: Box<str> = 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);
}