Skip to content

Commit b85a9a7

Browse files
authored
Rollup merge of rust-lang#113489 - tguichaoua:cow_from_array, r=dtolnay
impl `From<&[T; N]>` for `Cow<[T]>` Implement `From<&[T; N]>` for `Cow<[T]>` to simplify its usage in the following example. ```rust fn foo(data: impl Into<Cow<'static, [&'static str]>>) { /* ... */ } fn main() { foo(vec!["hello", "world"]); foo(&["hello", "world"]); // Error: the trait `From<&[&str; 2]>` is not implemented for `Cow<'static, [&'static str]>` foo(&["hello", "world"] as &[_]); // Explicit convertion into a slice is required } ```
2 parents 94723d4 + 6bfad31 commit b85a9a7

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

library/alloc/src/vec/cow.rs

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ impl<'a, T: Clone> From<&'a [T]> for Cow<'a, [T]> {
1515
}
1616
}
1717

18+
#[stable(feature = "cow_from_array_ref", since = "CURRENT_RUSTC_VERSION")]
19+
impl<'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+
1831
#[stable(feature = "cow_from_vec", since = "1.8.0")]
1932
impl<'a, T: Clone> From<Vec<T>> for Cow<'a, [T]> {
2033
/// Creates an [`Owned`] variant of [`Cow`]

0 commit comments

Comments
 (0)