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

Cannot move out of dereference for struct with vector. #15495

Closed
JP-Ellis opened this issue Jul 7, 2014 · 2 comments
Closed

Cannot move out of dereference for struct with vector. #15495

JP-Ellis opened this issue Jul 7, 2014 · 2 comments

Comments

@JP-Ellis
Copy link
Contributor

JP-Ellis commented Jul 7, 2014

I have encountered an issue which seems very unusual to me and I believe it might be a bug.

Below is a small example which illustrates the issue.

struct CountDown {
    count: uint,
    // Including the vector causes `Some(self.current())` to fail later on.
    vector: Vec<uint>
}

impl CountDown {
    fn new() -> CountDown {
        CountDown {
            count: 10,
            vector: vec!(3u, 4, 5)
        }
    }

    fn current(self) -> uint {
        self.count
    }
}

impl Iterator<uint> for CountDown {
    fn next(&mut self) -> Option<uint> {
        self.count -= 1;
        if self.count == 0 {
            None
        } else {
            // Whether the vector is in the struct or not, this work:
            //Some(self.count)

            // When the struct contains a vector, the following causes the error:
            //   cannot move out of dereference of `&mut`-pointer
            // If the vector is not in the struct, this work fine.
            Some(self.current())
        }
    }
}

fn main() {
    for n in CountDown::new() {
        println!("n = {}", n);
    }
}

It appears to me quite strange that the presence of a Vector within the struct (even if it isn't used) causes the issue. It also seems strange to me that returning a struct field works, but returning a method which itself returns that struct field fails.

Admittedly, I don't really understand what cannot move out of dereference of '&mut'-pointer really means; a Google search doesn't yield very much, and the documentation doesn't seem to contain much regarding the error.

@huonw
Copy link
Member

huonw commented Jul 7, 2014

This is a little subtle and comes down to moves vs. copies, due to current taking self by value.

    fn current(self) -> uint {

If you change that to fn current(&self) -> uint, i.e. take self by reference it will work.

The reason it works when there is no Vec is CountDown becomes Copy, i.e. a by-value use doesn't have to consume the source, essentially it just duplicates the CountDown instance out of the &mut self in next giving current its own independent copy. As soon as you have a Vec this is illegal (see my linked stack overflow answer above), and so the compiler complains.

Thanks for filing, but this isn't a bug, closing. :)

@huonw huonw closed this as completed Jul 7, 2014
@JP-Ellis
Copy link
Contributor Author

JP-Ellis commented Jul 7, 2014

Thank you for clarifying this for me. Sorry for bringing up a non-issue, though hopefully others with the same issue might stumble upon this and find their answer.

bors added a commit to rust-lang-ci/rust that referenced this issue Sep 18, 2023
internal: unpin serde

Serde no longer uses blobs as of

serde-rs/serde#2590

As such, there's no longer need for us to pin it.

Note that this doesn't upgrade serde version we use: I am fairly confident that the blobs are already there are fine, and now I am fairly confident that all future versions of serde will be fine as well.
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

No branches or pull requests

2 participants