Expand description
This library is a generic implementation of Serialize and Deserialize that can be used by
any flags type generated by bitflags!.
§Usage
Add bitflags-serde-legacy to your Cargo.toml:
[dependencies.bitflags_serde_legacy]
version = "0.1.1"Then, replace an existing #[derive(Serialize, Deserialize)] on your bitflags!
generated types with the following manual implementations:
use bitflags::bitflags;
bitflags! {
// #[derive(Serialize, Deserialize)]
struct Flags: u32 {
const A = 0b00000001;
const B = 0b00000010;
const C = 0b00000100;
const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
}
}
impl serde::Serialize for Flags {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
bitflags_serde_legacy::serialize(self, "Flags", serializer)
}
}
impl<'de> serde::Deserialize<'de> for Flags {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
bitflags_serde_legacy::deserialize("Flags", deserializer)
}
}Functions§
- deserialize
- Deserialize a flags type equivalently to how
#[derive(Deserialize)]on a flags type frombitflags1.xwould. - serialize
- Serialize a flags type equivalently to how
#[derive(Serialize)]on a flags type frombitflags1.xwould.