So i have this object trait
trait Object {
fn update(world: &mut World, index: usize) where Self: Sized;
}
And in that is an update function.
Now originally the update function looked like this
fn update(&mut self world: &mut World);
Next i had the world struct
struct World {
objects: Vec<Box<dyn Object>>
}
And in that world struct there was a different update function that was supposed to update all the objects in the world. My original implementation of Object::update could not work because
- I had to split the borrow meaning hat i had to copy the object, which breaks rusts vtable rules
- There were 2 mutable borrows
So i changed it so there was one mutable borrow which was the world, and to get the object i had to use the index parameter.
impl World {
fn update(&mut self) {
for index in 0..self.objects.len() {
self.objects[index].update(self, index)
}
}
}
fn main() {}
But there was another problem, and that is what i want to fix
error[E0599]: no method named
update
found for structBox<(dyn Object + 'static)>
in the current scope
--> src/main.rs:12:33
|
12 | self.objects[index].update(self, index)
| ^^^^^^ this is an associated function, not a method
|
= note: found the following associated functions; to be used as methods, functions must have aself
parameter
note: the candidate is defined in the traitObject
Now the compiler did give me a help message
help: disambiguate the associated function for the candidate
|
12 | <Box<(dyn Object + 'static)> as Object>::update(self.objects[index], self, index)
|
using it as a guide i came up with this code
<Box<(dyn Object + 'static)> as Object>::update(self, index)
However the compiler complained again saying that
the trait
Object
is not implemented forBox<(dyn Object + 'static)>
unboxing and using lifetimes gives a similar error
So, how can i fix this?