summaryrefslogtreecommitdiff
path: root/vendor/bitflags/examples
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/bitflags/examples')
-rw-r--r--vendor/bitflags/examples/custom_bits_type.rs97
-rw-r--r--vendor/bitflags/examples/custom_derive.rs23
-rw-r--r--vendor/bitflags/examples/fmt.rs49
-rw-r--r--vendor/bitflags/examples/macro_free.rs61
-rw-r--r--vendor/bitflags/examples/serde.rs36
5 files changed, 266 insertions, 0 deletions
diff --git a/vendor/bitflags/examples/custom_bits_type.rs b/vendor/bitflags/examples/custom_bits_type.rs
new file mode 100644
index 0000000..8924bfd
--- /dev/null
+++ b/vendor/bitflags/examples/custom_bits_type.rs
@@ -0,0 +1,97 @@
+use std::ops::{BitAnd, BitOr, BitXor, Not};
+
+use bitflags::{Bits, Flag, Flags};
+
+// Define a custom container that can be used in flags types
+// Note custom bits types can't be used in `bitflags!`
+// without making the trait impls `const`. This is currently
+// unstable
+#[derive(Clone, Copy, Debug)]
+pub struct CustomBits([bool; 3]);
+
+impl Bits for CustomBits {
+ const EMPTY: Self = CustomBits([false; 3]);
+
+ const ALL: Self = CustomBits([true; 3]);
+}
+
+impl PartialEq for CustomBits {
+ fn eq(&self, other: &Self) -> bool {
+ self.0 == other.0
+ }
+}
+
+impl BitAnd for CustomBits {
+ type Output = Self;
+
+ fn bitand(self, other: Self) -> Self {
+ CustomBits([
+ self.0[0] & other.0[0],
+ self.0[1] & other.0[1],
+ self.0[2] & other.0[2],
+ ])
+ }
+}
+
+impl BitOr for CustomBits {
+ type Output = Self;
+
+ fn bitor(self, other: Self) -> Self {
+ CustomBits([
+ self.0[0] | other.0[0],
+ self.0[1] | other.0[1],
+ self.0[2] | other.0[2],
+ ])
+ }
+}
+
+impl BitXor for CustomBits {
+ type Output = Self;
+
+ fn bitxor(self, other: Self) -> Self {
+ CustomBits([
+ self.0[0] & other.0[0],
+ self.0[1] & other.0[1],
+ self.0[2] & other.0[2],
+ ])
+ }
+}
+
+impl Not for CustomBits {
+ type Output = Self;
+
+ fn not(self) -> Self {
+ CustomBits([!self.0[0], !self.0[1], !self.0[2]])
+ }
+}
+
+#[derive(Clone, Copy, Debug)]
+pub struct CustomFlags(CustomBits);
+
+impl CustomFlags {
+ pub const A: Self = CustomFlags(CustomBits([true, false, false]));
+ pub const B: Self = CustomFlags(CustomBits([false, true, false]));
+ pub const C: Self = CustomFlags(CustomBits([false, false, true]));
+}
+
+impl Flags for CustomFlags {
+ const FLAGS: &'static [Flag<Self>] = &[
+ Flag::new("A", Self::A),
+ Flag::new("B", Self::B),
+ Flag::new("C", Self::C),
+ ];
+
+ type Bits = CustomBits;
+
+ fn bits(&self) -> Self::Bits {
+ self.0
+ }
+
+ fn from_bits_retain(bits: Self::Bits) -> Self {
+ CustomFlags(bits)
+ }
+}
+
+fn main() {
+ println!("{:?}", CustomFlags::A.union(CustomFlags::C));
+}
diff --git a/vendor/bitflags/examples/custom_derive.rs b/vendor/bitflags/examples/custom_derive.rs
new file mode 100644
index 0000000..5a85afb
--- /dev/null
+++ b/vendor/bitflags/examples/custom_derive.rs
@@ -0,0 +1,23 @@
+//! An example of implementing the `BitFlags` trait manually for a flags type.
+
+use std::str;
+
+use bitflags::bitflags;
+
+// Define a flags type outside of the `bitflags` macro as a newtype
+// It can accept custom derives for libaries `bitflags` doesn't support natively
+#[derive(zerocopy::AsBytes, zerocopy::FromBytes)]
+#[repr(transparent)]
+pub struct ManualFlags(u32);
+
+// Next: use `impl Flags` instead of `struct Flags`
+bitflags! {
+ impl ManualFlags: u32 {
+ const A = 0b00000001;
+ const B = 0b00000010;
+ const C = 0b00000100;
+ const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
+ }
+}
+
+fn main() {}
diff --git a/vendor/bitflags/examples/fmt.rs b/vendor/bitflags/examples/fmt.rs
new file mode 100644
index 0000000..724b207
--- /dev/null
+++ b/vendor/bitflags/examples/fmt.rs
@@ -0,0 +1,49 @@
+//! An example of implementing Rust's standard formatting and parsing traits for flags types.
+
+use core::{fmt, str};
+
+bitflags::bitflags! {
+ // You can `#[derive]` the `Debug` trait, but implementing it manually
+ // can produce output like `A | B` instead of `Flags(A | B)`.
+ // #[derive(Debug)]
+ #[derive(PartialEq, Eq)]
+ pub struct Flags: u32 {
+ const A = 1;
+ const B = 2;
+ const C = 4;
+ const D = 8;
+ }
+}
+
+impl fmt::Debug for Flags {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ bitflags::parser::to_writer(self, f)
+ }
+}
+
+impl fmt::Display for Flags {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ bitflags::parser::to_writer(self, f)
+ }
+}
+
+impl str::FromStr for Flags {
+ type Err = bitflags::parser::ParseError;
+
+ fn from_str(flags: &str) -> Result<Self, Self::Err> {
+ bitflags::parser::from_str(flags)
+ }
+}
+
+fn main() -> Result<(), bitflags::parser::ParseError> {
+ let flags = Flags::A | Flags::B;
+
+ println!("{}", flags);
+
+ let formatted = flags.to_string();
+ let parsed: Flags = formatted.parse()?;
+
+ assert_eq!(flags, parsed);
+
+ Ok(())
+}
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)
+ );
+}
diff --git a/vendor/bitflags/examples/serde.rs b/vendor/bitflags/examples/serde.rs
new file mode 100644
index 0000000..22eae2d
--- /dev/null
+++ b/vendor/bitflags/examples/serde.rs
@@ -0,0 +1,36 @@
+//! An example of implementing `serde::Serialize` and `serde::Deserialize`.
+//! The `#[serde(transparent)]` attribute is recommended to serialize directly
+//! to the underlying bits type without wrapping it in a `serde` newtype.
+
+#[cfg(feature = "serde")]
+fn main() {
+ use serde_derive::*;
+
+ bitflags::bitflags! {
+ #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
+ #[serde(transparent)]
+ pub struct Flags: u32 {
+ const A = 1;
+ const B = 2;
+ const C = 4;
+ const D = 8;
+ }
+ }
+
+ let flags = Flags::A | Flags::B;
+
+ let serialized = serde_json::to_string(&flags).unwrap();
+
+ println!("{:?} -> {}", flags, serialized);
+
+ assert_eq!(serialized, r#""A | B""#);
+
+ let deserialized: Flags = serde_json::from_str(&serialized).unwrap();
+
+ println!("{} -> {:?}", serialized, flags);
+
+ assert_eq!(deserialized, flags);
+}
+
+#[cfg(not(feature = "serde"))]
+fn main() {}