aboutsummaryrefslogtreecommitdiff
path: root/vendor/bitflags/examples/macro_free.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/bitflags/examples/macro_free.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/bitflags/examples/macro_free.rs')
-rw-r--r--vendor/bitflags/examples/macro_free.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/vendor/bitflags/examples/macro_free.rs b/vendor/bitflags/examples/macro_free.rs
new file mode 100644
index 0000000..7563379
--- /dev/null
+++ b/vendor/bitflags/examples/macro_free.rs
@@ -0,0 +1,61 @@
+//! An example of implementing the `BitFlags` trait manually for a flags type.
+//!
+//! This example doesn't use any macros.
+
+use std::{fmt, str};
+
+use bitflags::{Flag, Flags};
+
+// First: Define your flags type. It just needs to be `Sized + 'static`.
+pub struct ManualFlags(u32);
+
+// Not required: Define some constants for valid flags
+impl ManualFlags {
+ pub const A: ManualFlags = ManualFlags(0b00000001);
+ pub const B: ManualFlags = ManualFlags(0b00000010);
+ pub const C: ManualFlags = ManualFlags(0b00000100);
+ pub const ABC: ManualFlags = ManualFlags(0b00000111);
+}
+
+// Next: Implement the `BitFlags` trait, specifying your set of valid flags
+// and iterators
+impl Flags for ManualFlags {
+ const FLAGS: &'static [Flag<Self>] = &[
+ Flag::new("A", Self::A),
+ Flag::new("B", Self::B),
+ Flag::new("C", Self::C),
+ ];
+
+ type Bits = u32;
+
+ fn bits(&self) -> u32 {
+ self.0
+ }
+
+ fn from_bits_retain(bits: u32) -> Self {
+ Self(bits)
+ }
+}
+
+// Not required: Add parsing support
+impl str::FromStr for ManualFlags {
+ type Err = bitflags::parser::ParseError;
+
+ fn from_str(input: &str) -> Result<Self, Self::Err> {
+ bitflags::parser::from_str(input)
+ }
+}
+
+// Not required: Add formatting support
+impl fmt::Display for ManualFlags {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ bitflags::parser::to_writer(self, f)
+ }
+}
+
+fn main() {
+ println!(
+ "{}",
+ ManualFlags::A.union(ManualFlags::B).union(ManualFlags::C)
+ );
+}