Skip to content

Disjointness annotations for functions #1562

Closed

Description

@jFransham

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.

Activity

  1. oli-obk commented on Apr 1, 2016

    @oli-obk
    Contributor

    dupe of #1215

  2. steveklabnik commented on Apr 1, 2016

    @steveklabnik
    Contributor

    Yes, closing as a duplicate. Thanks!

  3. jFransham commented on Apr 2, 2016

    @jFransham
    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?

  4. added
    T-lang
    on Feb 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-lang

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions