How to deserialise to a trait object using Serde

I have a trait like so

pub trait Predicate<T> {
    fn test(&self, variable: T) -> bool;
}

with structs that can implement it like so

pub struct EqPredicate<T> {
    pub variable: T,
}

impl<T: PartialOrd + PartialEq + Copy> Predicate<T> for EqPredicate<T> {
    fn test(&self, variable: T) -> bool {
        self.variable == variable
    }
}

Now I have a data structure that has this trait object as it member. For example

pub struct Data<T: PartialOrd + PartialEq + Copy  + 'static> {
    pub meta: String,
    pub predicate: Rc<dyn Predicate<T>>
}

Now I have data in some custom string, csv like format that I would like to turn into this Data struct, but can't seem to be able to implement a custom deserialise for the Data struct, and the problem seems to be related to the trait object dyn Predicate<T>

Any pointers on how to successfully implement a custom deserialiser that deserialises to a struct that contains a trait object?

You might want to look at the typetag crate.

1 Like

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.