#bitflags #flags #bit #bitmask

no-std bitflags-serde-legacy

Implement serde traits for bitflags 2.x types compatibly with 1.x

2 releases

0.1.1 May 17, 2023
0.1.0 Feb 8, 2023

#1588 in Encoding

Download history 355/week @ 2024-01-03 817/week @ 2024-01-10 1021/week @ 2024-01-17 1013/week @ 2024-01-24 983/week @ 2024-01-31 1014/week @ 2024-02-07 1207/week @ 2024-02-14 1114/week @ 2024-02-21 328/week @ 2024-02-28 251/week @ 2024-03-06 732/week @ 2024-03-13 545/week @ 2024-03-20 314/week @ 2024-03-27 331/week @ 2024-04-03 1110/week @ 2024-04-10 1288/week @ 2024-04-17

3,145 downloads per month
Used in 11 crates (via zebra-chain)

MIT/Apache

10KB
117 lines

bitflags-serde-legacy

The serialization format used by bitflags! has changed between 1.x and 2.x. If you previously #[derive(Serialize, Deserialize], then you can use this library to maintain compatibility while upgrading.

Usage

You can either use this as a regular dependency, or pull the source into a private module in your project.

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)
    }
}

Dependencies

~210–475KB
~11K SLoC