Skip to content

serde_as cannot be cfg-gated #331

Answered by jonasbb
chipsenkbeil asked this question in Q&A
Discussion options

You must be logged in to vote

Since you use serde_as on a field with a generic, you need to manually specify the correct trait bounds for de-/serializing type T.

use serde::{Serialize, Deserialize};
use serde_with::serde_as;

#[serde_as]
#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(bound(serialize = "T: Serialize", deserialize = "T: Deserialize<'de>"))]
struct FixedMatrix<T, const ROW: usize, const COL: usize> {
    #[serde_as(as = "[[_; ROW]; COL]")]
    data: [[T; ROW]; COL],
}

Applying any of the serde attributes with, serialize_with, or deserialize_with on generics changes how serde infers the trait bounds of the resulting implementations. By default, serde adds T: Serialize as a requireme…

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@jonasbb
Comment options

@chipsenkbeil
Comment options

@jonasbb
Comment options

@chipsenkbeil
Comment options

@jonasbb
Comment options

Answer selected by chipsenkbeil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #330 on June 27, 2021 12:50.