From b3321e286013f14e20241706ef738d868fe7e91d Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Wed, 24 Mar 2021 18:43:18 -0700 Subject: [PATCH 1/2] Add docs for Vec::from functions Part of #51430 --- library/alloc/src/vec/mod.rs | 61 ++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 135279874bb3b..69df60a7c3e18 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2712,6 +2712,13 @@ impl AsMut<[T]> for Vec { #[stable(feature = "rust1", since = "1.0.0")] impl From<&[T]> for Vec { + /// Allocate a `Vec` and fill it by cloning `s`'s items. + /// + /// # Examples + /// + /// ``` + /// assert_eq!(Vec::from(&[1, 2, 3][..]), vec![1, 2, 3]); + /// ``` #[cfg(not(test))] fn from(s: &[T]) -> Vec { s.to_vec() @@ -2724,6 +2731,13 @@ impl From<&[T]> for Vec { #[stable(feature = "vec_from_mut", since = "1.19.0")] impl From<&mut [T]> for Vec { + /// Allocate a `Vec` and fill it by cloning `s`'s items. + /// + /// # Examples + /// + /// ``` + /// assert_eq!(Vec::from(&mut [1, 2, 3][..]), vec![1, 2, 3]); + /// ``` #[cfg(not(test))] fn from(s: &mut [T]) -> Vec { s.to_vec() @@ -2740,6 +2754,13 @@ impl From<[T; N]> for Vec { fn from(s: [T; N]) -> Vec { <[T]>::into_vec(box s) } + /// Allocate a `Vec` and move `s`'s items into it. + /// + /// # Examples + /// + /// ``` + /// assert_eq!(Vec::from([1, 2, 3]), vec![1, 2, 3]); + /// ``` #[cfg(test)] fn from(s: [T; N]) -> Vec { crate::slice::into_vec(box s) @@ -2751,6 +2772,20 @@ impl<'a, T> From> for Vec where [T]: ToOwned>, { + /// Convert a clone-on-write slice into a vector. + /// + /// If `s` already owns a `Vec`, it will be returned directly. + /// If `s` is borrowing a slice, a new `Vec` will be allocated and + /// filled by cloning `s`'s items into it. + /// + /// # Examples + /// + /// ``` + /// # use std::borrow::Cow; + /// let o: Cow<[i32]> = Cow::Owned(vec![1, 2, 3]); + /// let b: Cow<[i32]> = Cow::Borrowed(&[1, 2, 3]); + /// assert_eq!(Vec::from(o), Vec::from(b)); + /// ``` fn from(s: Cow<'a, [T]>) -> Vec { s.into_owned() } @@ -2760,6 +2795,15 @@ where #[cfg(not(test))] #[stable(feature = "vec_from_box", since = "1.18.0")] impl From> for Vec { + /// Convert a boxed slice into a vector. + /// No heap allocation is performed, and the items are not copied. + /// + /// # Examples + /// + /// ``` + /// let b: Box<[i32]> = vec![1, 2, 3].into_boxed_slice(); + /// assert_eq!(Vec::from(b), vec![1, 2, 3]); + /// ``` fn from(s: Box<[T], A>) -> Self { let len = s.len(); Self { buf: RawVec::from_box(s), len } @@ -2770,6 +2814,16 @@ impl From> for Vec { #[cfg(not(test))] #[stable(feature = "box_from_vec", since = "1.20.0")] impl From> for Box<[T], A> { + /// Convert a vector into a boxed slice. + /// + /// If `v` has excess capacity, its items will be moved into a + /// newly-allocated buffer with exactly the right capacity. + /// + /// # Examples + /// + /// ``` + /// assert_eq!(Box::from(vec![1, 2, 3]), vec![1, 2, 3].into_boxed_slice()); + /// ``` fn from(v: Vec) -> Self { v.into_boxed_slice() } @@ -2777,6 +2831,13 @@ impl From> for Box<[T], A> { #[stable(feature = "rust1", since = "1.0.0")] impl From<&str> for Vec { + /// Allocate a `Vec` and fill it with a UTF-8 string. + /// + /// # Examples + /// + /// ``` + /// assert_eq!(Vec::from("123"), vec![b'1', b'2', b'3']); + /// ``` fn from(s: &str) -> Vec { From::from(s.as_bytes()) } From ef1bd5776d9ba162a275772a8a31fa9d6b463e0a Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 25 Mar 2021 02:58:34 -0700 Subject: [PATCH 2/2] Change wording --- library/alloc/src/vec/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 69df60a7c3e18..ff93c772b5b81 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2795,8 +2795,8 @@ where #[cfg(not(test))] #[stable(feature = "vec_from_box", since = "1.18.0")] impl From> for Vec { - /// Convert a boxed slice into a vector. - /// No heap allocation is performed, and the items are not copied. + /// Convert a boxed slice into a vector by transferring ownership of + /// the existing heap allocation. /// /// # Examples ///