Skip to content
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

Merged
merged 2 commits into from
Dec 1, 2013

Conversation

DaGenix
Copy link

@DaGenix DaGenix commented Nov 30, 2013

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.

@TeXitoi
Copy link
Contributor

TeXitoi commented Nov 30, 2013

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)
            }

@DaGenix
Copy link
Author

DaGenix commented Nov 30, 2013

@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: cannot borrow an &mutin a&pointer; try an&mut instead. I feel like there might be some way around doing that as well, but, I'm not sure how.

mut_chunks() returns an iterator that produces mutable slices. This is the
mutable version of the existing chunks() method on the ImmutableVector trait.
@DaGenix
Copy link
Author

DaGenix commented Dec 1, 2013

@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: let tmp = util::replace(&mut self.v, &mut []); instead of using util::swap.

@huonw
Copy link
Member

huonw commented Dec 1, 2013

Is it possible to make this a double ended iterator like ChunkIter?

@DaGenix
Copy link
Author

DaGenix commented Dec 1, 2013

I can. Should I push something here or open a new pull request?

@huonw
Copy link
Member

huonw commented Dec 1, 2013

Pushing it here is fine. (You will need a new r+ though.)

@DaGenix
Copy link
Author

DaGenix commented Dec 1, 2013

Ok, done. Thanks!

bors added a commit that referenced this pull request Dec 1, 2013
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.
@bors bors closed this Dec 1, 2013
@bors bors merged commit 2a8dfc3 into rust-lang:master Dec 1, 2013
@DaGenix DaGenix deleted the mut-chunks branch December 1, 2013 19:17
@nikomatsakis
Copy link
Contributor

On Sat, Nov 30, 2013 at 03:53:22PM -0800, DaGenix wrote:

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: "cannot borrow an &mut in a & pointer; try an &mut instead". I feel like there might be some way around doing that as well, but, I'm not sure how.

See issue #9629 (there is a pending PR to fix it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants