¿Cómo implementar `serde :: Serialize` para un objeto de rasgo en caja?
Me encontré con un problema al intentar crear un vector genérico para una estructura. Este fue mi primer intento
#[derive(Serialize)]
struct Card {
sections: Vec<Section<WidgetTrait>>
}
#[derive(Serialize)]
struct Section<T: WidgetTrait> {
header: String,
widgets: Vec<T>
}
Esto me ha llevado a un error queSized
no está implementado yWidgetTrait
tamaño no se conoce en tiempo de compilación.
Mi siguiente intento fue usarBox<WidgetTrait>
al igual que
#[derive(Serialize)]
struct Section {
header: String,
widgets: Vec<Box<WidgetTrait>>
}
Esto me ha llevado a un error:
error[E0277]: the trait bound `WidgetTrait: serde::Serialize` is not satisfied
--> src/main.rs:11:10
|
11 | #[derive(Serialize)]
| ^^^^^^^^^ the trait `serde::Serialize` is not implemented for `WidgetTrait`
|
= note: required because of the requirements on the impl of `serde::Serialize` for `std::boxed::Box<WidgetTrait>`
= note: required because of the requirements on the impl of `serde::Serialize` for `std::vec::Vec<std::boxed::Box<WidgetTrait>>`
= note: required by `serde::ser::SerializeStruct::serialize_field`
i objetivo es para el vector de widgets enSection
struct para poder aceptar diferentes tipos de widgets que implementanWidgetTrait
rasgo, tal como lo haría con una interfaz.