Skip to content

The implementation From<&'a [T; N]> for Cow<'a, [T]> seems missing #116890

Closed
@oisyn

Description

@oisyn

This fails to compile

use std::borrow::Cow;

fn foo<'a>(v: impl Into<Cow<'a, [i32]>>) { }

fn main() {
    foo(&[1, 2, 3]);
}

Rust playground link

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)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions