| 1 | use super::Vec; |
| 2 | use crate::borrow::Cow; |
| 3 | |
| 4 | #[stable (feature = "cow_from_vec" , since = "1.8.0" )] |
| 5 | impl<'a, T: Clone> From<&'a [T]> for Cow<'a, [T]> { |
| 6 | /// Creates a [`Borrowed`] variant of [`Cow`] |
| 7 | /// from a slice. |
| 8 | /// |
| 9 | /// This conversion does not allocate or clone the data. |
| 10 | /// |
| 11 | /// [`Borrowed`]: crate::borrow::Cow::Borrowed |
| 12 | fn from(s: &'a [T]) -> Cow<'a, [T]> { |
| 13 | Cow::Borrowed(s) |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | #[stable (feature = "cow_from_array_ref" , since = "1.77.0" )] |
| 18 | impl<'a, T: Clone, const N: usize> From<&'a [T; N]> for Cow<'a, [T]> { |
| 19 | /// Creates a [`Borrowed`] variant of [`Cow`] |
| 20 | /// from a reference to an array. |
| 21 | /// |
| 22 | /// This conversion does not allocate or clone the data. |
| 23 | /// |
| 24 | /// [`Borrowed`]: crate::borrow::Cow::Borrowed |
| 25 | fn from(s: &'a [T; N]) -> Cow<'a, [T]> { |
| 26 | Cow::Borrowed(s as &[_]) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | #[stable (feature = "cow_from_vec" , since = "1.8.0" )] |
| 31 | impl<'a, T: Clone> From<Vec<T>> for Cow<'a, [T]> { |
| 32 | /// Creates an [`Owned`] variant of [`Cow`] |
| 33 | /// from an owned instance of [`Vec`]. |
| 34 | /// |
| 35 | /// This conversion does not allocate or clone the data. |
| 36 | /// |
| 37 | /// [`Owned`]: crate::borrow::Cow::Owned |
| 38 | fn from(v: Vec<T>) -> Cow<'a, [T]> { |
| 39 | Cow::Owned(v) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | #[stable (feature = "cow_from_vec_ref" , since = "1.28.0" )] |
| 44 | impl<'a, T: Clone> From<&'a Vec<T>> for Cow<'a, [T]> { |
| 45 | /// Creates a [`Borrowed`] variant of [`Cow`] |
| 46 | /// from a reference to [`Vec`]. |
| 47 | /// |
| 48 | /// This conversion does not allocate or clone the data. |
| 49 | /// |
| 50 | /// [`Borrowed`]: crate::borrow::Cow::Borrowed |
| 51 | fn from(v: &'a Vec<T>) -> Cow<'a, [T]> { |
| 52 | Cow::Borrowed(v.as_slice()) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #[stable (feature = "rust1" , since = "1.0.0" )] |
| 57 | impl<'a, T> FromIterator<T> for Cow<'a, [T]> |
| 58 | where |
| 59 | T: Clone, |
| 60 | { |
| 61 | #[track_caller ] |
| 62 | fn from_iter<I: IntoIterator<Item = T>>(it: I) -> Cow<'a, [T]> { |
| 63 | Cow::Owned(FromIterator::from_iter(it)) |
| 64 | } |
| 65 | } |
| 66 | |