I have a custom trait like this:
pub struct SomeObject;
pub trait Renderer {
fn render(&self) -> Vec<SomeObject>;
}
and a module with multiple structs implementing said trait. The module exposes a single function:
mod renderer {
use super::*;
pub fn all() -> Vec<Box<Renderer>> {
vec![]
}
}
which gets used in the main function like so:
fn main() {
let renderers = renderer::all();
loop {
// ...
let objects: Vec<SomeObject> = renderers.iter().flat_map(|r| r.render()).collect();
// ...
}
}
That's working nicely so far.
I now need to change the render()
method to take a mutable reference to self
. Doing so gives the following error:
error[E0596]: cannot borrow immutable `Box` content `**r` as mutable
--> src/main.rs:20:74
|
20 | let objects: Vec<SomeObject> = renderers.iter().flat_map(|r| r.render()).collect();
| ^ cannot borrow as mutable
How can I rectify this? The renderers
instance is not used passed to any other place, so I should be able to borrow mutably within the scope of this line, at least according to my intuition.