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