-
Notifications
You must be signed in to change notification settings - Fork 744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to write a generic type bounds for deserialize trait? #964
Comments
In general, you should not write trait bounds on structs. See https://github.com/Manishearth/rust-clippy/issues/1689. The solution here is not writing a trait bound. You haven't written one for Debug or Clone, so don't write one for Deserialize either. #[derive(Debug, Clone, Deserialize)]
pub struct Response<ResponseResult> {
/// some fields ...
pub result: ResponseResult,
} This will expand to something like: impl<'de, ResponseResult> Deserialize<'de> for Response<ResponseResult>
where ResponseResult: Deserialize<'de>
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
/* ... */
}
} |
@dtolnay Okay! But, I am just being curious now, is there a way to do that with rust syntax at all? |
If someone else ends up here like I did, where contained types have more complicated bounds for their Example [play]: extern crate serde;
#[macro_use]
extern crate serde_derive;
use serde::ser::Serialize;
use serde::de::Deserialize;
use std::collections::HashSet;
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hash};
#[derive(Serialize, Deserialize)]
pub struct MySet<T, S = RandomState>(
#[serde(bound(serialize = "HashSet<T, S>: Serialize",
deserialize = "HashSet<T, S>: Deserialize<'de>"))]
HashSet<T, S>,
);
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Serialize, Deserialize)]
pub struct MySet_<T: Eq + Hash, S: BuildHasher = RandomState>(
#[serde(bound(deserialize = "HashSet<T, S>: Deserialize<'de>"))]
HashSet<T, S>,
); There is no such behavior currently expressible for regular |
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.Gives the error:
I understand that
Deserialize
trait requires a lifetime but I don't know how to specialize it right here. Is there a way?The text was updated successfully, but these errors were encountered: