Use of `impl Into<Cow<[_]>>` for an API

I'm writing a function that accepts a Cow<'static, [&'static str]>.
I tried to use impl Into<_> to simplify the usage of this function, but passing an array didn't work without explicitly convert the reference to the array into a slice.

Is there a better way to handle this ?

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 &[_]);
}

Also is there a reason why there is no implementation of From for a reference to an array ?

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

Alternatives:

    foo(&["hello", "world"][..]);
    foo(["hello", "world"].as_slice());

You could make your own wrapper trait (though I personally don't think it's worth it).

Seems like it would be a reasonable addition to me.

I created a post on the internals forum for that.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.