Hello I wanted to make an interface similar to the built in slices
[T] for collections such as
VecDeque, whose items aren’t necessarily contiguous. But I have had a few questions/problems.
(1) First of all, if this is already implemented somewhere, then that would have saved me a lot of trouble, but I guess its also a good exercise to do it myself.
(2) The main issue that I’m having is that I can’t wrangle my lifetimes of my own Iterator implementation to match the
Iterator trait. Well, upon further experimentation it seems to work fine for the immutable version, but the mutable version (using the exact same code, just with different idents and
& replaced with
&mut) won’t borrowck.
(3) Writing implementations such as these requires a LOT of code duplication because Rust currently doesn’t support parametric polymorphism for mutability. If I remember correctly this was disccussed briefly a while ago, but it was deemed to be only useful for low-levelish crates like new datastructures or std itself, and therefore the complexity wasn’t worth it. I guess I understand that, although some days I do dream of a world in which we could ‘parameterize all the things’ . For now though does anyone have suggestions for eg: a
macro_rules! macro that could do it? I think the problem is that
IterMut right now are two seperate identifiers, and you can’t just take
Iter and append
Mut to it.
Edit: Now I’m here I remember a few more gripes I had with this stuff. For example, is there a reason why the
IndexMut traits enforce returning shared and mutable references respectively? I would have thought that
fn index(...) -> Self::Output ala Iterators is much more powerful. Or even better, could we have an
Apply trait and be like Scala
Finally: Here’s the code: https://play.rust-lang.org/?gist=7c929706b6cb3a371af0ea628d2566a4&version=stable&backtrace=0
Thanks so much guys if you could help out