I've got something like the following:
struct Foo {
file: File
}
struct Bar<'a> {
file: &'a mut File
}
impl Foo {
fn test(&self) {
let x = Bar { file: &mut self.file };
x.work();
}
}
impl<'a> Bar<'a> {
fn work(&mut self) {
self.file.write(...).unwrap();
}
}
Now when I call function test
in Foo
I would expect that the file position in Foo
has changed (foo.file.stream_position().unwrap()
). But it hasn't. The file position has only changed in Bar
, but not in Foo
. I' ve tried it also with RefCell
. Same problem.
I thought that file
in Foo
and Bar
always have the same file position, because file
in Bar
ist just a reference to file
in Foo
.
How can I share a File
object between several structs?