You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I got a compile error while trying to deserialize a strong type struct called ApiResponse, see below:
use serde::de::DeserializeOwned;#[derive(Deserialize,Debug)]pubstructApiResponse<T:DeserializeOwned>{pubcode:u32,pubdata:T,pubmessage:String,}
My REST API response has a fixed format which seems like to this:
{"code": 200,"data": {},// "data" could be a objcet or list"message" : "success"}
That is why I need an ApiResponse struct which keep data as a generic type to deserialize every possible response. For example the type T could be those types:
#[derive(Serialize,Deserialize,Debug)]pubstructTagList{pubtags:Vec<String>,}// or #[derive(Serialize,Deserialize,Debug)]pubstructPost{pubtitle:String,pubauthor:String,pubtag:String,}
then I can deserialize ApiResponse<TagList> or ApiResponse<Post>, but I get such a compile error:
You should not have trait bounds like T: DeserializeOwned on a data structure. See rust-lang/rust-clippy#1689 for discussion about using trait bounds in this way.
Hi, I got a compile error while trying to deserialize a strong type struct called ApiResponse, see below:
My REST API response has a fixed format which seems like to this:
That is why I need an ApiResponse struct which keep data as a generic type to deserialize every possible response. For example the type T could be those types:
then I can deserialize ApiResponse<TagList> or ApiResponse<Post>, but I get such a compile error:
Did I do something wrong? How to fix this issue? Any comment is welcome, thanks. 😄
The text was updated successfully, but these errors were encountered: