Skip to content

Guide: ownership #16487

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

Merged
merged 1 commit into from
Aug 21, 2014
Merged

Guide: ownership #16487

merged 1 commit into from
Aug 21, 2014

Conversation

steveklabnik
Copy link
Member

Whew. This section was so important, I saved it for last.

/cc @wycats, @nikomatsakis


If two distinct bindings share a pointer, and the memory that pointer points to
is immutable, then there are no problems. But if it's mutable, both pointers
can attempt to write to the memory at the same time, causing a **data race**.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Data races" happen in concurrent systems, to my knowledge. They can be caused by pointer aliasing, but we'd better mention a simpler concept. Iterator invalidation comes to my mind. Unfortunately, it's specific to C++, so perhaps someone else has a better idea.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, the term "data race" is meaningful, whereas I've never heard the term "iterator invalidation"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure about the term, but this concept DOES also protect against concurrency-based data races as well, so this isn't outright wrong, exactly. Maybe should point out both, and be more explicit?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also just fall back to a generic term everyone has heard, "race condition", and then elaborate later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's most common with a pointer to a mutable structure and another pointer to its part, mutable or not. In C++, an insertion into a vector can invalidate all its iterators and references. There are several other rules. In Rust, every data structure is borrowed for the lifetime of an iterator.

Iterator invalidation is basically pointer invalidation: mutation occurs from some other place, and suddenly a pointer points to uninitialized/invalid memory.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also consider it a data race (or iterator-invalidation problem) if one borrower tries to write while another tries to read.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to go with 'race condition', as that seems most general.

@steveklabnik
Copy link
Member Author

I've now updated the text with all of the current feedback. I moved a section around a bit to make which rule i'm talking about more clear.

@Gankra
Copy link
Contributor

Gankra commented Aug 18, 2014

This looks great now! Great, job @steveklabnik

bors added a commit that referenced this pull request Aug 21, 2014
Whew. This section was so important, I saved it for last.

/cc @wycats, @nikomatsakis
@bors bors closed this Aug 21, 2014
@bors bors merged commit c47dfbb into rust-lang:master Aug 21, 2014
matthiaskrgr pushed a commit to matthiaskrgr/rust that referenced this pull request Feb 11, 2024
…=Veykril

Remove unnecessary `.as_ref()` in generate getter assist

Resolves rust-lang#12389
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.