Trait method that returns a value that implements `serde::Serialize`?

This is what I’d like to do:

pub trait MyTrait {
    fn get_template_data(&self) -> &dyn serde::Serialize;
        // Error: the trait `Serialize` cannot be made into an object

Alas, serde::Serialize isn’t object-safe, so dyn doesn’t work here.

MyTrait must remain object-safe, so I can’t use generics. impl would also have been OK but is not allowed in traits, either. Is there a way to do this?

You probably want erased-serde, or something built on top of it.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.