I've got the following code:
use std::fmt::Debug;
use rocket::serde::{Serialize};
#[serde(crate = "rocket::serde")]
pub trait ComponentDto: Debug + Send + Sync + Serialize {}
use rocket::serde::{Deserialize, Serialize};
#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
pub struct ParagraphDto {
pub id: i32,
pub uuid: String,
pub user_id: i32,
pub project_id: i32,
pub position: i32,
pub component: Box<dyn ComponentDto>,
pub status: String,
pub created_at: String,
pub updated_at: String,
}
impl ParagraphDto {
How do I serialize trait object using rocket::serde?
Using rockett::serde as a normal struct to trait object results in the following error:
Compiling testa v0.1.0 (/mnt/repository/repository/projects/rust/testa)
error[E0038]: the trait `MyTrait` cannot be made into an object
--> src/main.rs:21:20
|
21 | fn getStruct() -> Box<dyn MyTrait> {
| ^^^^^^^^^^^^^^^^^ `MyTrait` cannot be made into an object
|
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits. html#object-safety>