So, by syntax support, what I mean is that, for example, the Vec type doesn’t actually have a lot of the methods it appears to. Vec doesn’t have sort, what it has is “Methods from Deref<Target=[T]>”, which includes sort. So when you call my_vec.sort() what you get is Deref Magic that figures out to think about the Vec as a slice and then use the sort method of the slice type. And it all works out because slice is a semi-magical type within the language.
Also, you can do foo.as_slice() to slice out the whole thing, and then i guess the [(2,2)..(4,4)] part means you want to sub-slice out some smaller portion… but I sure don’t think you can write that method. Going back to your playground link, the bitmap slice type was defined as
pub struct Slice<'a> {
bitmap: &'a PaletteBitmap,
from: (usize, usize),
to: (usize, usize),
}
So your “subslice with a range” Index trait impl would look like this, right?
impl<'a> Index<Range<(usize, usize)>> for Slice<'a> {
type Output = Slice<'a>;
fn index(&self, r: Range<(usize, usize)>) -> &Self::Output {
// just ignore bounds checks for a moment, not important
& Slice {
bitmap: self.bitmap,
from: (self.from.0 + r.start.0, self.from.1 + r.start.1),
to: (self.from.0 + r.end.0, self.from.1 + r.end.1),
}
}
}
But of course that’s a classic borrowed value does not live long enough error. As far as I can tell you also can’t jigger the lifetimes to make the output slice key off of the lifetime of the original source bitmap or anything like that. I’m not an expert, but a few folks on IRC and Discord were all defeated by this problem, maybe you can see something none of us do. Otherwise, not only can you not use . to activate Deref coercions, but you also can’t use Index or IndexMut with a Range to pull out a sub-slice or mutable sub-slice.