How to share a file object?

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?

When I tried your code in the playground (I had to tweak some mutability on references, or it wouldn't compile), it seems to work correctly.

Could you give a more complete example of the problem?

I've found the problem. There was a bug in my code. Sorry for the noise.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.