diff options
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/alloc.rs | 4 | ||||
| -rw-r--r-- | src/liballoc/boxed.rs | 10 | ||||
| -rw-r--r-- | src/liballoc/boxed_test.rs | 8 | ||||
| -rw-r--r-- | src/liballoc/collections/btree/map.rs | 7 | ||||
| -rw-r--r-- | src/liballoc/string.rs | 41 |
5 files changed, 66 insertions, 4 deletions
diff --git a/src/liballoc/alloc.rs b/src/liballoc/alloc.rs index 3bd0c243b39..1a8a081e16f 100644 --- a/src/liballoc/alloc.rs +++ b/src/liballoc/alloc.rs @@ -21,6 +21,10 @@ use core::usize; pub use core::alloc::*; extern "Rust" { + // These are the magic symbols to call the global allocator. rustc generates + // them from the `#[global_allocator]` attribute if there is one, or uses the + // default implementations in libstd (`__rdl_alloc` etc in `src/libstd/alloc.rs`) + // otherwise. #[allocator] #[rustc_allocator_nounwind] fn __rust_alloc(size: usize, align: usize) -> *mut u8; diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 74354f605e5..63b262d1f3d 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -73,7 +73,7 @@ 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; @@ -81,6 +81,7 @@ use core::ops::{CoerceUnsized, DispatchFromDyn, Deref, DerefMut, Generator, Gene 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 { 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<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); +} diff --git a/src/liballoc/collections/btree/map.rs b/src/liballoc/collections/btree/map.rs index 24c8fd3a969..49e488702b4 100644 --- a/src/liballoc/collections/btree/map.rs +++ b/src/liballoc/collections/btree/map.rs @@ -853,9 +853,10 @@ impl<K: Ord, V> BTreeMap<K, V> { /// ``` /// use std::collections::BTreeMap; /// - /// let mut map: BTreeMap<&str, i32> = ["Alice", "Bob", "Carol", "Cheryl"].iter() - /// .map(|&s| (s, 0)) - /// .collect(); + /// let mut map: BTreeMap<&str, i32> = ["Alice", "Bob", "Carol", "Cheryl"] + /// .iter() + /// .map(|&s| (s, 0)) + /// .collect(); /// for (_, balance) in map.range_mut("B".."Cheryl") { /// *balance += 100; /// } diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index 5c776292f53..2beb3240aac 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -2206,6 +2206,20 @@ impl<'a> From<&'a str> for String { #[cfg(not(test))] #[stable(feature = "string_from_box", since = "1.18.0")] impl From<Box<str>> for String { + /// Converts the given boxed `str` slice to a `String`. + /// It is notable that the `str` slice is owned. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// let s1: String = String::from("hello world"); + /// let s2: Box<str> = s1.into_boxed_str(); + /// let s3: String = String::from(s2); + /// + /// assert_eq!("hello world", s3) + /// ``` fn from(s: Box<str>) -> String { s.into_string() } @@ -2213,6 +2227,19 @@ impl From<Box<str>> for String { #[stable(feature = "box_from_str", since = "1.20.0")] impl From<String> for Box<str> { + /// Converts the given `String` to a boxed `str` slice that is owned. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// let s1: String = String::from("hello world"); + /// let s2: Box<str> = Box::from(s1); + /// let s3: String = String::from(s2); + /// + /// assert_eq!("hello world", s3) + /// ``` fn from(s: String) -> Box<str> { s.into_boxed_str() } @@ -2272,6 +2299,20 @@ impl<'a> FromIterator<String> for Cow<'a, str> { #[stable(feature = "from_string_for_vec_u8", since = "1.14.0")] impl From<String> for Vec<u8> { + /// Converts the given `String` to a vector `Vec` that holds values of type `u8`. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// let s1 = String::from("hello world"); + /// let v1 = Vec::from(s1); + /// + /// for b in v1 { + /// println!("{}", b); + /// } + /// ``` fn from(string: String) -> Vec<u8> { string.into_bytes() } |
