H2CO3 / avocado

Strongly-typed MongoDB driver for Rust
MIT License
81 stars 2 forks source link

Doc for generic struct #3

Closed max-frai closed 5 years ago

max-frai commented 5 years ago

Hi! Thanks again for amazing wrapper :) I found, that it's not possible to derive Doc for generic structure:

> message: `Doc` can't be derived for a type that is generic over type parameters

#[derive(Doc, Serialize, Deserialize)]
pub struct Test<T: Serialize + Deserialize> {
   pub data<T>
}

We specify Output type for queries, maybe we also can specify somehow exact types for deserialization?

H2CO3 commented 5 years ago

Hey there!

I'm curious, what's the use-case or mindset behind wanting a heterogeneous collection? Collections in Avocado are intentionally homogeneous. It's quite hard to make a bulletproof implementation for a collection that may hold any type (and additional questions arise, such as what should the name be for each instantiation).

In the meantime, I suggest that you could use a non-generic document struct and an enum wrapping the different possible types. (Of which there should be a finite, closed set – as far as I can tell, trying to store any possible future types in a database doesn't really make sense.)

max-frai commented 5 years ago

Hello! Sorry for long request. You are right, there is no sense in such situation.