Well, with this mundane example one can just rewrite it.
pub struct MyStruct {
some_data: Data,
some_more_data: MoreData,
}
impl MyStruct {
pub fn some_function(&mut self) {
let ref1 = &self.some_data.some_field;
self.some_more_datat.modify(ref1);
}
}
There are more complicated examples for which doing this transform is impossible, as the one in my first post. But as these are artificial examples I wonder if there is actual need for this.
I slightly disagree. I prefer the code to be explicit, otherwise the errors could get quite confusing.
It seems quite interesting and I do not view big complications.
struct Foo {
x: u32,
v: [u32; 2]
}
trait FooBar
{
mut x:u32,//mut here feels a little weird. But how else to make correctly the ShadowStruct example?
fn bar(&mut self) {
self.x += 1;
}
}
impl FooBar for Foo
{
x: self.x,
}
impl Foo {
fn new() -> Self {
Foo { x: 0, v: [0; 2] }
}
fn spam(&mut self) {
for _ in self.v.iter() {
//Legal since v and FooBar are disjoint
self.bar();
}
}
}
The mut bit is annoying. But it is useful for this example.
struct Real{
foo: Foo,
bar: Bar,
baz: Baz,
}
trait R
{
foo: Foo,//inmutable
mut bar: Bar,
fn compute(&mut self){...}
}
trait Z
{
foo: Foo,//inmutable
mut baz: Baz,
fn compute(&mut self){...}
}
impl R for Real
{
foo: self.foo,
bar: self.bar,
}
impl Z for Real
{
foo: self.foo,
baz: self.baz,
}
impl Real
{
fn compute(&mut self)
{
//This is legal because the intersection of R and Z is foo, which is always used as inmutable
let r=self as &mut R;
let z=self as &mut Z;
//Call r.compute(...) and z.compute(...) in parallel or whatever
}
}
I see you have been discussing traits with fields for quite a long time, while I have been using Rust for just a few weeks. So you should know better which problems there are to adopt this.