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

Disjointness annotations for functions #1562

Closed
jFransham opened this issue Mar 31, 2016 · 3 comments
Closed

Disjointness annotations for functions #1562

jFransham opened this issue Mar 31, 2016 · 3 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@jFransham
Copy link

Currently the getter/setter pattern is problematic in Rust because the borrow checker cannot be sure that a given function only accesses a certain set of fields, which means that getter functions are strictly less useful than field accesses. I propose a syntax somehow similar to this:

struct Bar;

struct Foo {
    a: Bar,
    b: Bar,
}

impl Foo {
    fn a_mut(&mut self) -> &mut Bar {
        &mut self.a
    }

    fn b_mut(&mut self) -> &mut Bar {
        &mut self.b
    }

    fn a_mut_2(&mut Self { a }) -> &mut Bar {
        &mut a
    }

    fn b_mut_2(&mut  Self { b }) -> &mut Bar {
        &mut b
    }
}

fn main() {
    // This currently does not work in Rust
    {
        let mut foo = Foo { a: Bar, b: Bar };
        let a = foo.a_mut();
        let b = foo.b_mut();
    }
    // This would work, with disjointness annotations
    {
        let mut foo = Foo { a: Bar, b: Bar };
        let a = foo.a_mut_2();
        let b = foo.b_mut_2();
    }
}

This would also probably work well with the fields in traits proposal.

@oli-obk
Copy link
Contributor

oli-obk commented Apr 1, 2016

dupe of #1215

@steveklabnik
Copy link
Member

Yes, closing as a duplicate. Thanks!

@jFransham
Copy link
Author

I asked on IRC if that existed and I couldn't find it when I searched for "disjoint". Is there a better way to find duplicates?

@Centril Centril added the T-lang Relevant to the language team, which will review and decide on the RFC. label Feb 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

4 participants