Summary
Introduce a modifier “nonlocal”(subject to bike-shedding) to fns and methods. Borrowck will “inline” their implementations into where they’re used before checking, which will loose the lifetime restrictions a little more.
Motivation
Nowadays, even after we’ve got NLL, certain program’s correctness still can’t be proved, putting certain limitations into API surface design.
One common issue is that: We’re not able to provide Java-style getters/setters. Any such method will put the whole value into the borrowed state, causing limitations for upcoming program actions.
Guide-level explaination
When you want to write a function that provides a “partial borrow” or something like this, you can add the nonlocal modifier on the function or method.
struct S {
a: usize,
b: usize,
}
impl S {
nonlocal fn a_mut(&mut self) -> &mut usize { &mut self.a }
nonlocal fn b_mut(&mut self) -> &mut usize { &mut self.b }
}
fn main () {
let mut fib = S {a: 0, b: 1};
let a = fib.a_mut();
let b = fib.b_mut();
println!("{}", *a);
println!("{}", *b);
loop {
*a += *b;
println!("{}", *a);
*b += *a;
println!("{}", *b);
}
}
Reference-level explaination
To be written