novifinancial / serde-reflection

Rust libraries and tools to help with interoperability and testing of serialization formats based on Serde.
Apache License 2.0
139 stars 42 forks source link

[Bug] How to generate code with generics? #99

Closed ufoscout closed 3 years ago

ufoscout commented 3 years ago

I wonder if it is possible to generate code with generics. For example, this simple code fails to run:

    use serde::{Deserialize, Serialize};
    use serde_reflection::{Tracer, TracerConfig};

    #[derive(Serialize, Deserialize)]
    struct Test<T> {
        a: T,
    }

    #[derive(Serialize, Deserialize)]
    struct TestString {
        test: Test<String>,
    }

    #[derive(Serialize, Deserialize)]
    struct TestNumber {
        test: Test<usize>,
    }

    let mut tracer = Tracer::new(TracerConfig::default());
    tracer.trace_simple_type::<TestString>().unwrap();
    tracer.trace_simple_type::<TestNumber>().unwrap();
    let registry = tracer.registry().unwrap();

The tracer.registry().unwrap() function call fails with this error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Incompatible("Str", "U64")'

I can't figure out how to add the generic Test<T> struct to the tracer to let the code run as expected.

ma2bd commented 3 years ago

@ufoscout I'm happy to report that a work around is now available using new classes in the serde-name crate: https://docs.rs/serde-name/0.2.0/serde_name/