Skip to content

&mut [T] should implement Collection #16896

Closed
@japaric

Description

@japaric

The following code should work, but it doesn't:

// Newtype wrapper to represent a row vector (or the row of a matrix)
struct Row<D> {
    data: D,
}

impl<D: Collection> Collection for Row<D> {
    fn len(&self) -> uint {
        self.data.len()
    }
}

fn main() {
    let mut v = vec![0u8, 1, 2];

    // Works because &[T] implements `Collection`
    println!("{}", Row { data: v.as_slice() }.len());
    // Works because &mut [T] gets reborrowed as &[T] (I think)
    println!("{}", v.as_mut_slice().len());
    // Doesn't work because &mut [T] doesn't implement the `Collection` trait
    println!("{}", Row { data: v.as_mut_slice() }.len());
}

Output

len.rs:20:20: 20:56 error: failed to find an implementation of trait core::collections::Collection for &mut [u8]
len.rs:20     println!("{}", Row { data: v.as_mut_slice() }.len());

I can send a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions