-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement mut_chunks() method for MutableVector trait. #10739
Conversation
I manage to do that with safe code in my future mut_split() (that I'll submit later). The idea is to keep a mut_slice in the iterator, and split it. Here, that give me something like that: Some(idx) => {
let mut tmp = &mut [];
std::util::swap(&mut tmp, &mut self.v);
let (h, t) = tmp.mut_split(idx);
self.v = t.mut_slice_from(1);
Some(h)
} |
@TeXitoi Cool, that does work. Thanks! I rebased to get rid of the unsafe code. I'm still keeping a separate variable around with the remaining length of the vector which feels like a bit of duplication since the slice already knows it length. However, the size hint method is called on a &self and any attempt I make to call self.v.len() results in the error: |
mut_chunks() returns an iterator that produces mutable slices. This is the mutable version of the existing chunks() method on the ImmutableVector trait.
@alexcrichton Cool, I made that update. Thanks! EDIT: When I rebased, it looks like I knocked out the comment I was replying to. The update I made was to do: |
Is it possible to make this a double ended iterator like |
I can. Should I push something here or open a new pull request? |
Pushing it here is fine. (You will need a new r+ though.) |
Ok, done. Thanks! |
mut_chunks() returns an iterator that produces mutable slices. This is the mutable version of the existing chunks() method on the ImmutableVector trait. EDIT: This uses only safe code now. PREVIOUSLY: I tried to get this working with safe code only, but I couldn't figure out how to make that work. Before #8624, the exact same code worked without the need for the transmute() call. With that fix and without the transmute() call, the compiler complains about the call to mut_slice(). I think the issue is that the mutable slice that is returned will live longer than the self parameter since the self parameter doesn't have an explicit lifetime. However, that is the way that the Iterator trait defines the next() method. I'm sure there is a good reason for that, although I don't quite understand why. Anyway, I think the interface is safe, since the MutChunkIter will only hand out non-overlapping pointers and there is no way to get it to hand out the same pointer twice.
On Sat, Nov 30, 2013 at 03:53:22PM -0800, DaGenix wrote:
See issue #9629 (there is a pending PR to fix it) |
mut_chunks() returns an iterator that produces mutable slices. This is the mutable version of the existing chunks() method on the ImmutableVector trait.
EDIT: This uses only safe code now.
PREVIOUSLY:
I tried to get this working with safe code only, but I couldn't figure out how to make that work. Before #8624, the exact same code worked without the need for the transmute() call. With that fix and without the transmute() call, the compiler complains about the call to mut_slice(). I think the issue is that the mutable slice that is returned will live longer than the self parameter since the self parameter doesn't have an explicit lifetime. However, that is the way that the Iterator trait defines the next() method. I'm sure there is a good reason for that, although I don't quite understand why. Anyway, I think the interface is safe, since the MutChunkIter will only hand out non-overlapping pointers and there is no way to get it to hand out the same pointer twice.