Skip to content

How to write a generic type bounds for deserialize trait? #964

@iddm

Description

@iddm

I want to declare a generic struct with de-serialization but I don't know how to correctly require a Deserialize trait to be implemented for a generic type.

#[derive(Debug, Clone, Deserialize)]
pub struct Response<ResponseResult: Deserialize> {
    /// some fields ...
    pub result: ResponseResult,
}

Gives the error:


error[E0106]: missing lifetime specifier
  --> src/model.rs:43:37
   |
43 | pub struct Response<ResponseResult: Deserialize> {
   |                                     ^^^^^^^^^^^^^^ expected lifetime parameter

I understand that Deserialize trait requires a lifetime but I don't know how to specialize it right here. Is there a way?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions