serde_as
cannot be cfg-gated
#331
-
I've seen some examples of using use serde::{Serialize, Deserialize};
use serde_with::serde_as;
#[serde_as]
#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct FixedMatrix<T, const ROW: usize, const COL: usize> {
#[serde_as(as = "[[_; ROW]; COL]")]
data: [[T; ROW]; COL],
} Was browsing through serde-rs/serde#1937 and saw mention of: #[serde_with::serde_as]
#[derive(Serialize)]
struct Foo {
#[serde_as(as = "[_; 2]")]
bar: [String; 2],
#[serde_as(as = "Vec<[_; 5]>")]
foobar: Vec<[u8; 5]>,
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Since you use 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 The same applies to Your attribute would work as is, if a normal type is used in the array, e.g., #[serde_as(as = "[[_; ROW]; COL]")]
data: [[u8; ROW]; COL], |
Beta Was this translation helpful? Give feedback.
Since you use
serde_as
on a field with a generic, you need to manually specify the correct trait bounds for de-/serializing typeT
.Applying any of the serde attributes
with
,serialize_with
, ordeserialize_with
on generics changes how serde infers the trait bounds of the resulting implementations. By default, serde addsT: Serialize
as a requireme…