Implement `From<&[T; N]>` for `Cow<[T]>`

Add the From trait to Cow<[T]> to create it from an array reference.

impl<'a, T: Clone, const N: usize> From<&'a [T; N]> for Cow<'a, [T]>
{
    fn from(s: &'a [T; N]) -> Cow<'a, [T]> {
        Cow::Borrowed(s as &[_])
    }
}

Here's a concrete example where it may help.

fn foo(data: impl Into<Cow<'static, [&'static str]>>) { /* ... */ }

fn main() {
    foo(vec!["hello", "world"]);
    // foo(&["hello", "world"]); // the trait `From<&[&str; 2]>` is not implemented for `Cow<'static, [&'static str]>`
    foo(&["hello", "world"] as &[_]); // Explicit convertion into a slice is required
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.