Closed
Description
This fails to compile
use std::borrow::Cow;
fn foo<'a>(v: impl Into<Cow<'a, [i32]>>) { }
fn main() {
foo(&[1, 2, 3]);
}
With the following diagnostics:
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `Cow<'_, [i32]>: From<&[{integer}; 3]>` is not satisfied
--> src/main.rs:8:9
|
8 | foo(&[1, 2, 3]);
| --- ^^^^^^^^^^ the trait `From<&[{integer}; 3]>` is not implemented for `Cow<'_, [i32]>`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `From<T>`:
<Cow<'a, OsStr> as From<OsString>>
<Cow<'a, OsStr> as From<&'a OsStr>>
<Cow<'a, OsStr> as From<&'a OsString>>
<Cow<'a, CStr> as From<CString>>
<Cow<'a, CStr> as From<&'a CStr>>
<Cow<'a, CStr> as From<&'a CString>>
<Cow<'a, Path> as From<&'a Path>>
<Cow<'a, Path> as From<PathBuf>>
and 7 others
= note: required for `&[{integer}; 3]` to implement `Into<Cow<'_, [i32]>>`
note: required by a bound in `foo`
--> src/main.rs:5:20
|
5 | fn foo<'a>(v: impl Into<Cow<'a, [i32]>>) { }
| ^^^^^^^^^^^^^^^^^^^^ required by this bound in `foo`
For more information about this error, try `rustc --explain E0277`.
It seems that From<&'a [T; N]>
is not implemented for Cow<'a [T]>
, while From<&'a [T]>
is. Has this been an oversight, or is there a rationale behind this?
Providing my own implementation does seem to work
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)
}
}