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