Attribute Macro serbia::serbia [−][src]
#[serbia]
An attribute macro that enables (de)serializing arrays of length larger than 32 with Serde.
Simply slap it on top of your struct or enum, before the Serialize/Deserialize derive.
Usage
Just slap #[serbia]
on top of your type definition. Structs and enums both work!
use serbia::serbia; use serde::{Serialize, Deserialize}; #[serbia] #[derive(Serialize, Deserialize)] struct S { arr_big: [u8; 300], // custom serialize/deserialize code generated here arr_small: [u8; 8], // no custom code - this is handled by Serde fine } #[serbia] #[derive(Serialize, Deserialize)] enum E { ArrBig([u8; 300]), ArrSmall([u8; 22]), Mixed([u8; 8], [i32; 44], String), }
If Serbia sees an array length given as a constant, it will generate custom serialize/deserialize code by default, without inspecting whether the constant is larger than 32 or not. This is a limitation of macros.
const BUFSIZE: usize = 22; #[serbia] #[derive(Serialize, Deserialize)] struct S { arr: [i32; BUFSIZE], // custom serialize/deserialize code generated here foo: String, }
Skipping fields
If for some reason you don’t want Serbia to generate custom serialize/deserialize code for a field that it would normally handle, you can skip it.
const BUFSIZE: usize = 24; #[serbia] #[derive(Serialize, Deserialize)] struct S { #[serbia(skip)] arr_a: [u8; BUFSIZE], arr_b: [u8; 42], arr_small: [u8; 8], }
It’s possible to be more granular if needed for some reason.
const BUFSIZE: usize = 24; #[serbia] #[derive(Serialize, Deserialize)] struct S { #[serbia(skip_serializing, skip_deserializing)] arr_a: [u8; BUFSIZE], arr_b: [u8; 42], arr_small: [u8; 8], }
Manual array length
You can use the #[serbia(bufsize = ... )]
option to set an array length for
a field. This can be useful to make type aliases work. Constants work here!
type BigArray = [i32; 300]; #[serbia] #[derive(Serialize, Deserialize)] struct S { #[serbia(bufsize = 300)] arr_a: BigArray, foo: String, }
const BUFSIZE: usize = 300; type BigArray = [i32; BUFSIZE]; #[serbia] #[derive(Serialize, Deserialize)] struct S { #[serbia(bufsize = "BUFSIZE")] arr_a: BigArray, foo: String, }
Interaction with Serde field attributes
Serbia detects when certain Serde field attributes are used and avoids generating code that would cause a conflict, instead yielding to Serde.
#[serbia] #[derive(Serialize, Deserialize)] struct S { big_arr: [u8; 40], // serbia generates code for this #[serde(serialize_with="ser", deserialize_with="de")] bigger_arr: [u8; 42], // serbia ignores this in favor of the (de)serializers you provided }
Serbia is intended to play nice with Serde field attributes. If there are problems, please create an issue or submit a PR!
What doesn’t work
Nested types.
#[serbia] #[derive(Debug, Serialize, Deserialize, PartialEq)] struct S { big_arr: Option<[u8; 300]>, // no code generated for this nested array }
Serbia doesn’t yet pick up on Serde variant attributes,
so there might be conflicts there. This can probably be worked around by using
#[serbia(skip)]
on each field that Serbia would try to generate custom
(de)serialization code for.