Skip to content

Commit

Permalink
Rollup merge of rust-lang#55843 - Axary:master, r=sfackler
Browse files Browse the repository at this point in the history
add FromIterator<A> to Box<[A]>
  • Loading branch information
pietroalbini authored Nov 12, 2018
2 parents ff06c9d + ab55d9b commit 345fbb0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ use core::convert::From;
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;
use core::ops::{CoerceUnsized, DispatchFromDyn, Deref, DerefMut, Generator, GeneratorState};
use core::ptr::{self, NonNull, Unique};
use core::task::{LocalWaker, Poll};

use vec::Vec;
use raw_vec::RawVec;
use str::from_boxed_utf8_unchecked;

Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions src/liballoc/boxed_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 345fbb0

Please sign in to comment.