Skip to content

Derive macro should support a way to specify concrete types for generics #3006

@thou-vow

Description

@thou-vow
struct Type1;
struct Type2;

struct Struct<T>

It's currently not possible to derive only for Struct<Type1> for example.
The implementation for this could be pretty similar to #[serde(bound)], however it would need it's own parser.
I don't know if there's any syntax standard for this kind of clause anywhere, so I just brainstormed one.

#[derive(Serialize)]
#[serde(select = "T = u32|u64, U = bool || U  = String" )]
struct Struct<T, U> {}

// Which expands to...
impl Serialize for Struct<u32, bool> {}
impl Serialize for Struct<u64, bool> {}
impl<T> Serialize for Struct<T, String> {}

Whenever the same generic type is used by this attribute and #[serde(bound)], it could output an error. However, I believe there's no reason for trait bounds to not belong to this attribute too.

#[derive(Serialize)]
#[serde(select = "T = u32|u64, U: Clone || U = String" )]
struct Struct<T, U> {}

// Which expands to...
impl<U> Serialize for Struct<u32, U> where U: Clone {}
impl<U> Serialize for Struct<u64, U> where U: Clone {}
impl<T> Serialize for Struct<T, String> {} 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions