Accessing a Write from two places

I need to write to a file from two places in my code -- I want to layer higher level functionality onto a lower level renderer which takes a Write instance. My idea is to pass a Write object in, get the lower-level renderer to write a chunk to the file, and then add some data to the file from another piece of code.

I could do this easily by having a single Write object that they both use, but referencing rules of rust stop me from doing this.

I can see various ways around this with an Rc perhaps, but it seems an obvious requirement; I can't find any crates or anything in the standard library which addresses is. Am I missing anything?

Write is automatically implemented on &mut references, so if you have x that implements Write, you can pass &mut x to functions too.

Write is also implemented for &File, so you can easily create multiple references to the same File, or write through an Rc. See also File::try_clone.

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.