#bit-flags #serialize-deserialize #flags #bitmask #bit

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

#141 in No standard library

Download history 753/week @ 2024-07-04 878/week @ 2024-07-11 1484/week @ 2024-07-18 1002/week @ 2024-07-25 1362/week @ 2024-08-01 1418/week @ 2024-08-08 1676/week @ 2024-08-15 1479/week @ 2024-08-22 1276/week @ 2024-08-29 1557/week @ 2024-09-05 1694/week @ 2024-09-12 564/week @ 2024-09-19 822/week @ 2024-09-26 804/week @ 2024-10-03 843/week @ 2024-10-10 1167/week @ 2024-10-17

3,728 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

~185–440KB