I've a unknown size at compilation time on function call.
And I don't really understand why.
Is it cause by the trait with types inside ?
Is it possible to easily fix it ?
Here's the error :
error[E0277]: the size for values of type `(dyn entities::component::Component + 'static)` cannot be known at compilation time
--> src\business\component\get_all_for_page.rs:48:22
|
48 | Some(b) => b.build_component(data.content, None),
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn entities::component::Component + 'static)`
dyn Trait is dynamically sized and also requires a vtable to be usable, so when it comes to dyn Trait values, they will be behind some kind of pointer -- &dyn Trait, Box<dyn Trait>, Arc<dyn Trait>, etc.
You can't return unsized values like dyn Trait, so dyn Trait doesn't make sense as the BaseComponent or AdminComponent types. Also, associated types (and generic type parameters) have an implicit Sized bound (which can be removed with : ?Sized).
So either these signatures (and any other that imply the associated types must be Sized) has to change and ?Sized has to be added to the associated types...
...or your type alias needs to be changed to use Sized types, and you need to make sure that the trait is implemented for those types (or change the bounds on the associated types).
pub type DynComponentBuilder = dyn ComponentBuilder<
AdminComponent=Box<dyn Component>,
BaseComponent=Box<dyn Component>,
>;
impl Component for Box<dyn Component + '_> {
// ...
// n.b. be careful to call methods on `*Self` and not `Self`
// to avoid a recursive call, e.g.
fn method(&self) {
(**self).method()
}
}