Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/bitflags/examples/serde.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / bitflags / examples / serde.rs` 39 lines ยท 1116 bytes Raw Blame History 1 //! An example of implementing `serde::Serialize` and `serde::Deserialize`. 2 //! The `#[serde(transparent)]` attribute is recommended to serialize directly 3 //! to the underlying bits type without wrapping it in a `serde` newtype. 4 5 #[cfg(feature = "serde")] 6 fn main() { 7 use serde_lib::*; 8 9 bitflags::bitflags! { 10 #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] 11 #[serde(transparent)] 12 // NOTE: We alias the `serde` crate as `serde_lib` in this repository, 13 // but you don't need to do this 14 #[serde(crate = "serde_lib")] 15 pub struct Flags: u32 { 16 const A = 1; 17 const B = 2; 18 const C = 4; 19 const D = 8; 20 } 21 } 22 23 let flags = Flags::A | Flags::B; 24 25 let serialized = serde_json::to_string(&flags).unwrap(); 26 27 println!("{:?} -> {}", flags, serialized); 28 29 assert_eq!(serialized, r#""A | B""#); 30 31 let deserialized: Flags = serde_json::from_str(&serialized).unwrap(); 32 33 println!("{} -> {:?}", serialized, flags); 34 35 assert_eq!(deserialized, flags); 36 } 37 38 #[cfg(not(feature = "serde"))] 39 fn main() {}