gapbuffer icon indicating copy to clipboard operation
gapbuffer copied to clipboard

Implement DoubleEndedIterator for Items

Open crespyl opened this issue 11 years ago • 4 comments

This makes Items also implement DoubleEndedIterator, allowing traversal backwards from the end of the buffer.

Specifically, gapbuffer.iter().rev() should now work as expected.

crespyl avatar Jan 16 '15 17:01 crespyl

I don't think this actually works, according to the Rust definition of DoubleEndedIterator. That is, I am pretty sure that both next() and next_back() are supposed to take from either end of the underlying data type (essentially, thing of it as a double ended queue). So there's no way to support this API with the current Items representation, which was why I removed it. Adding an extra length isn't a big deal though, I mostly didn't want to do it to keep the initial implementation simple.

pythonesque avatar Jan 16 '15 17:01 pythonesque

I may have misunderstood the API then. So basically Items would need a idx_front and an idx_back, with next() advancing idx_front and next_back() rewinding idx_back?

crespyl avatar Jan 16 '15 18:01 crespyl

Hm. Given that we can't really use slices normally here, we need some alternative way to start from some arbitrary point and iterate backwards. With a Vec, we could use v.slice_to(n).iter().rev(), but that doesn't really work here. Do we need an iter_from(isize)?

crespyl avatar Jan 19 '15 14:01 crespyl

You could reimplement the Slice interface by declaring a new type that worked like a slice (i.e. takes a range, etc.).

pythonesque avatar Jan 19 '15 15:01 pythonesque