Coverage Report

Created: 2026-09-06 07:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/sawp-flags-0.13.1/src/lib.rs
Line
Count
Source
1
//! Bitflags handling and storage.
2
//!
3
//! This crate allows you to define flag values using an enum and derive
4
//! `BitFlags` to add convenience methods.
5
//!
6
//! This implementation was heavily inspired by
7
//! [enumflags2](https://crates.io/crates/enumflags2) and
8
//! [bitflags](https://crates.io/crates/bitflags) and customized for use in a
9
//! sawp parser. Consider using those two open source projects before resorting
10
//! to this one. One key feature is that we are automatically generating ffi
11
//! accessors using the [sawp-ffi](https://crates.io/crates/sawp-ffi) crate.
12
//!
13
//! This crate works as follows:
14
//! - `enum YourEnum` with a numeric representation (e.g. `#[repr(u8)]`) is used
15
//!   to define bit fields.
16
//! - deriving `BitFlags` on this enum will add convenience methods for bitwise
17
//!   operations and implement the `Flag` trait.
18
//! - Flag values are transparently stored as `Flags<YourEnum>` so you can perform
19
//!   more operations on this type.
20
//!
21
//! # Example
22
//! See `example` module for a generated example as well.
23
//! ```
24
//! use sawp_flags::{BitFlags, Flags, Flag};
25
//!
26
//! /// Example enum
27
//! #[derive(Debug, Clone, Copy, PartialEq, BitFlags)]
28
//! #[repr(u8)]
29
//! pub enum Test {
30
//!     A = 0b0001,
31
//!     B = 0b0010,
32
//!     C = 0b0100,
33
//!     D = 0b1000,
34
//!     /// Variants can be a bitmask of the other fields like so
35
//!     E = Test::A as u8 | Test::B as u8 | Test::C as u8 | Test::D as u8,
36
//! }
37
//!
38
//! // `flags` will be of transparent type `Flags<Test>`
39
//! let flags : Flags<Test> = Test::A | Test::C;
40
//!
41
//! // convert a number to flags using `from_bits()`
42
//! assert_eq!(flags, Flags::<Test>::from_bits(0b101));
43
//!
44
//! // convert flags to a number using `bits()`
45
//! assert_eq!(0b101, flags.bits());
46
//!
47
//! // perform bitwise operations
48
//! assert_eq!(Test::A | Test::B | Test::C, flags | Test::B);
49
//! assert_eq!(Test::A, flags & Test::A);
50
//! assert_eq!(Test::C, flags ^ Test::A);
51
//!
52
//! // check which flags are set
53
//! assert!(flags.contains(Test::A));
54
//! assert!(!flags.contains(Test::A | Test::B));
55
//! assert!(flags.intersects(Test::A));
56
//! assert!(flags.intersects(Test::A | Test::B));
57
//! ```
58
59
use std::ops::*;
60
61
/// The `BitFlags` derive macro will implement the `Flags` Trait on your enum and
62
/// provide convenience methods for bit operations and type conversions.
63
///
64
// Re-export derive macro for convenience.
65
pub use sawp_flags_derive::BitFlags;
66
67
/// A primitive numeric type to be used for flag storage.
68
pub trait Primitive:
69
    Default
70
    + BitOr<Self, Output = Self>
71
    + BitAnd<Self, Output = Self>
72
    + BitXor<Self, Output = Self>
73
    + Not<Output = Self>
74
    + PartialOrd<Self>
75
    + std::fmt::Debug
76
    + std::fmt::Binary
77
    + Copy
78
    + Clone
79
{
80
}
81
82
impl Primitive for u8 {}
83
impl Primitive for u16 {}
84
impl Primitive for u32 {}
85
impl Primitive for u64 {}
86
impl Primitive for u128 {}
87
88
/// A trait implemented by all flag enums.
89
pub trait Flag: Copy + Clone + std::fmt::Debug + std::fmt::Display + 'static {
90
    /// Associated primitive numeric type
91
    type Primitive: Primitive;
92
93
    /// A list of all flag variants in the enum
94
    const ITEMS: &'static [Self];
95
96
    /// Numeric representation of the variant
97
    fn bits(self) -> Self::Primitive;
98
99
    /// Flag value when no variants are set
100
    fn none() -> Flags<Self>;
101
102
    /// Flag value when all variants are set
103
    fn all() -> Flags<Self>;
104
}
105
106
/// Storage type for handling flags
107
#[derive(Copy, Clone, PartialEq, Eq)]
108
#[repr(transparent)]
109
pub struct Flags<Enum, Primitive = <Enum as Flag>::Primitive> {
110
    val: Primitive,
111
    marker: std::marker::PhantomData<Enum>,
112
}
113
114
impl<Enum> std::fmt::Debug for Flags<Enum>
115
where
116
    Enum: Flag,
117
{
118
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119
        self.val.fmt(f)
120
    }
121
}
122
123
impl<Enum> Default for Flags<Enum>
124
where
125
    Enum: Flag,
126
{
127
    fn default() -> Self {
128
        Self {
129
            val: <Enum as Flag>::Primitive::default(),
130
            marker: std::marker::PhantomData,
131
        }
132
    }
133
}
134
135
impl<Enum> Flags<Enum>
136
where
137
    Enum: Flag,
138
{
139
    /// Get a flag from a single enum value
140
17.6M
    pub fn from_flag(flag: Enum) -> Self {
141
17.6M
        Self {
142
17.6M
            val: flag.bits(),
143
17.6M
            marker: std::marker::PhantomData,
144
17.6M
        }
145
17.6M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::from_flag
Line
Count
Source
140
4.21M
    pub fn from_flag(flag: Enum) -> Self {
141
4.21M
        Self {
142
4.21M
            val: flag.bits(),
143
4.21M
            marker: std::marker::PhantomData,
144
4.21M
        }
145
4.21M
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::from_flag
Line
Count
Source
140
987k
    pub fn from_flag(flag: Enum) -> Self {
141
987k
        Self {
142
987k
            val: flag.bits(),
143
987k
            marker: std::marker::PhantomData,
144
987k
        }
145
987k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::from_flag
Line
Count
Source
140
1.66k
    pub fn from_flag(flag: Enum) -> Self {
141
1.66k
        Self {
142
1.66k
            val: flag.bits(),
143
1.66k
            marker: std::marker::PhantomData,
144
1.66k
        }
145
1.66k
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::from_flag
Line
Count
Source
140
274
    pub fn from_flag(flag: Enum) -> Self {
141
274
        Self {
142
274
            val: flag.bits(),
143
274
            marker: std::marker::PhantomData,
144
274
        }
145
274
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::from_flag
Line
Count
Source
140
1.42M
    pub fn from_flag(flag: Enum) -> Self {
141
1.42M
        Self {
142
1.42M
            val: flag.bits(),
143
1.42M
            marker: std::marker::PhantomData,
144
1.42M
        }
145
1.42M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::from_flag
Line
Count
Source
140
1.44M
    pub fn from_flag(flag: Enum) -> Self {
141
1.44M
        Self {
142
1.44M
            val: flag.bits(),
143
1.44M
            marker: std::marker::PhantomData,
144
1.44M
        }
145
1.44M
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::from_flag
Line
Count
Source
140
392k
    pub fn from_flag(flag: Enum) -> Self {
141
392k
        Self {
142
392k
            val: flag.bits(),
143
392k
            marker: std::marker::PhantomData,
144
392k
        }
145
392k
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::from_flag
Line
Count
Source
140
132k
    pub fn from_flag(flag: Enum) -> Self {
141
132k
        Self {
142
132k
            val: flag.bits(),
143
132k
            marker: std::marker::PhantomData,
144
132k
        }
145
132k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::from_flag
Line
Count
Source
140
50.2k
    pub fn from_flag(flag: Enum) -> Self {
141
50.2k
        Self {
142
50.2k
            val: flag.bits(),
143
50.2k
            marker: std::marker::PhantomData,
144
50.2k
        }
145
50.2k
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::from_flag
Line
Count
Source
140
13.7k
    pub fn from_flag(flag: Enum) -> Self {
141
13.7k
        Self {
142
13.7k
            val: flag.bits(),
143
13.7k
            marker: std::marker::PhantomData,
144
13.7k
        }
145
13.7k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::from_flag
Line
Count
Source
140
5.43M
    pub fn from_flag(flag: Enum) -> Self {
141
5.43M
        Self {
142
5.43M
            val: flag.bits(),
143
5.43M
            marker: std::marker::PhantomData,
144
5.43M
        }
145
5.43M
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::from_flag
Line
Count
Source
140
1.34M
    pub fn from_flag(flag: Enum) -> Self {
141
1.34M
        Self {
142
1.34M
            val: flag.bits(),
143
1.34M
            marker: std::marker::PhantomData,
144
1.34M
        }
145
1.34M
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::from_flag
Line
Count
Source
140
418k
    pub fn from_flag(flag: Enum) -> Self {
141
418k
        Self {
142
418k
            val: flag.bits(),
143
418k
            marker: std::marker::PhantomData,
144
418k
        }
145
418k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::from_flag
Line
Count
Source
140
1.52M
    pub fn from_flag(flag: Enum) -> Self {
141
1.52M
        Self {
142
1.52M
            val: flag.bits(),
143
1.52M
            marker: std::marker::PhantomData,
144
1.52M
        }
145
1.52M
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::from_flag
Line
Count
Source
140
183k
    pub fn from_flag(flag: Enum) -> Self {
141
183k
        Self {
142
183k
            val: flag.bits(),
143
183k
            marker: std::marker::PhantomData,
144
183k
        }
145
183k
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::from_flag
Line
Count
Source
140
85.3k
    pub fn from_flag(flag: Enum) -> Self {
141
85.3k
        Self {
142
85.3k
            val: flag.bits(),
143
85.3k
            marker: std::marker::PhantomData,
144
85.3k
        }
145
85.3k
    }
146
147
    /// Get a flag from a numeric value
148
    ///
149
    /// Note: the value is unchecked so any bit may be set. Be
150
    /// careful because `PartialEq` is a direct comparison of
151
    /// underlying bits.
152
40.0M
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
40.0M
        Self {
154
40.0M
            val: bits,
155
40.0M
            marker: std::marker::PhantomData,
156
40.0M
        }
157
40.0M
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::from_bits
Line
Count
Source
152
987k
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
987k
        Self {
154
987k
            val: bits,
155
987k
            marker: std::marker::PhantomData,
156
987k
        }
157
987k
    }
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::from_bits
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::from_bits
Line
Count
Source
152
1.36M
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
1.36M
        Self {
154
1.36M
            val: bits,
155
1.36M
            marker: std::marker::PhantomData,
156
1.36M
        }
157
1.36M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::from_bits
Line
Count
Source
152
8.94M
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
8.94M
        Self {
154
8.94M
            val: bits,
155
8.94M
            marker: std::marker::PhantomData,
156
8.94M
        }
157
8.94M
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::from_bits
Line
Count
Source
152
384k
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
384k
        Self {
154
384k
            val: bits,
155
384k
            marker: std::marker::PhantomData,
156
384k
        }
157
384k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::from_bits
Line
Count
Source
152
3.62M
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
3.62M
        Self {
154
3.62M
            val: bits,
155
3.62M
            marker: std::marker::PhantomData,
156
3.62M
        }
157
3.62M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::from_bits
Line
Count
Source
152
2.50M
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
2.50M
        Self {
154
2.50M
            val: bits,
155
2.50M
            marker: std::marker::PhantomData,
156
2.50M
        }
157
2.50M
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::from_bits
Line
Count
Source
152
964k
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
964k
        Self {
154
964k
            val: bits,
155
964k
            marker: std::marker::PhantomData,
156
964k
        }
157
964k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::from_bits
Line
Count
Source
152
2.50M
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
2.50M
        Self {
154
2.50M
            val: bits,
155
2.50M
            marker: std::marker::PhantomData,
156
2.50M
        }
157
2.50M
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::from_bits
Line
Count
Source
152
18.1k
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
18.1k
        Self {
154
18.1k
            val: bits,
155
18.1k
            marker: std::marker::PhantomData,
156
18.1k
        }
157
18.1k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::from_bits
Line
Count
Source
152
10.9M
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
10.9M
        Self {
154
10.9M
            val: bits,
155
10.9M
            marker: std::marker::PhantomData,
156
10.9M
        }
157
10.9M
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::from_bits
Line
Count
Source
152
1.34M
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
1.34M
        Self {
154
1.34M
            val: bits,
155
1.34M
            marker: std::marker::PhantomData,
156
1.34M
        }
157
1.34M
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::from_bits
Line
Count
Source
152
423k
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
423k
        Self {
154
423k
            val: bits,
155
423k
            marker: std::marker::PhantomData,
156
423k
        }
157
423k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::from_bits
Line
Count
Source
152
2.08M
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
2.08M
        Self {
154
2.08M
            val: bits,
155
2.08M
            marker: std::marker::PhantomData,
156
2.08M
        }
157
2.08M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::from_bits
Line
Count
Source
152
2.73M
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
2.73M
        Self {
154
2.73M
            val: bits,
155
2.73M
            marker: std::marker::PhantomData,
156
2.73M
        }
157
2.73M
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::from_bits
Line
Count
Source
152
1.20M
    pub fn from_bits(bits: <Enum as Flag>::Primitive) -> Self {
153
1.20M
        Self {
154
1.20M
            val: bits,
155
1.20M
            marker: std::marker::PhantomData,
156
1.20M
        }
157
1.20M
    }
158
159
    /// Numeric representation of the variant
160
68.8M
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
68.8M
        self.val
162
68.8M
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::bits
Line
Count
Source
160
1.82M
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
1.82M
        self.val
162
1.82M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::bits
Line
Count
Source
160
17.5M
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
17.5M
        self.val
162
17.5M
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::bits
Line
Count
Source
160
3.94M
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
3.94M
        self.val
162
3.94M
    }
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::bits
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::bits
Line
Count
Source
160
264k
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
264k
        self.val
162
264k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::bits
Line
Count
Source
160
5.67M
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
5.67M
        self.val
162
5.67M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::bits
Line
Count
Source
160
3.08M
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
3.08M
        self.val
162
3.08M
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::bits
Line
Count
Source
160
401k
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
401k
        self.val
162
401k
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::bits
Line
Count
Source
160
60.9k
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
60.9k
        self.val
162
60.9k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::bits
Line
Count
Source
160
3.45M
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
3.45M
        self.val
162
3.45M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::bits
Line
Count
Source
160
21.9M
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
21.9M
        self.val
162
21.9M
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::bits
Line
Count
Source
160
5.36M
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
5.36M
        self.val
162
5.36M
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::bits
Line
Count
Source
160
170k
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
170k
        self.val
162
170k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::bits
Line
Count
Source
160
1.65M
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
1.65M
        self.val
162
1.65M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::bits
Line
Count
Source
160
3.06M
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
3.06M
        self.val
162
3.06M
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::bits
Line
Count
Source
160
368k
    pub fn bits(&self) -> <Enum as Flag>::Primitive {
161
368k
        self.val
162
368k
    }
163
164
    /// Reference to numeric representation of the variant
165
    pub fn bits_ref(&self) -> &<Enum as Flag>::Primitive {
166
        &self.val
167
    }
168
169
    /// Check if at least one flag in common is set
170
12.3M
    pub fn intersects<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
171
12.3M
        (self & rhs.into()).bits() != Enum::none().bits()
172
12.3M
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::intersects::<sawp_flags::Flags<sawp_modbus::AccessType, u8>>
Line
Count
Source
170
456k
    pub fn intersects<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
171
456k
        (self & rhs.into()).bits() != Enum::none().bits()
172
456k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::intersects::<sawp_modbus::ErrorFlags>
Line
Count
Source
170
4.21M
    pub fn intersects<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
171
4.21M
        (self & rhs.into()).bits() != Enum::none().bits()
172
4.21M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::intersects::<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>
Line
Count
Source
170
172k
    pub fn intersects<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
171
172k
        (self & rhs.into()).bits() != Enum::none().bits()
172
172k
    }
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::intersects::<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::intersects::<sawp_modbus::AccessType>
Line
Count
Source
170
841k
    pub fn intersects<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
171
841k
        (self & rhs.into()).bits() != Enum::none().bits()
172
841k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::intersects::<sawp_modbus::ErrorFlags>
Line
Count
Source
170
92.4k
    pub fn intersects<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
171
92.4k
        (self & rhs.into()).bits() != Enum::none().bits()
172
92.4k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::intersects::<sawp_flags::Flags<sawp_modbus::AccessType, u8>>
Line
Count
Source
170
818k
    pub fn intersects<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
171
818k
        (self & rhs.into()).bits() != Enum::none().bits()
172
818k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::intersects::<sawp_modbus::ErrorFlags>
Line
Count
Source
170
5.42M
    pub fn intersects<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
171
5.42M
        (self & rhs.into()).bits() != Enum::none().bits()
172
5.42M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::intersects::<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>
Line
Count
Source
170
45.4k
    pub fn intersects<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
171
45.4k
        (self & rhs.into()).bits() != Enum::none().bits()
172
45.4k
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::intersects::<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>
Line
Count
Source
170
1.35k
    pub fn intersects<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
171
1.35k
        (self & rhs.into()).bits() != Enum::none().bits()
172
1.35k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::intersects::<sawp_modbus::AccessType>
Line
Count
Source
170
286k
    pub fn intersects<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
171
286k
        (self & rhs.into()).bits() != Enum::none().bits()
172
286k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::intersects::<sawp_modbus::ErrorFlags>
Line
Count
Source
170
6.44k
    pub fn intersects<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
171
6.44k
        (self & rhs.into()).bits() != Enum::none().bits()
172
6.44k
    }
173
174
    /// Check if all flags provided in `rhs` are set
175
3.09M
    pub fn contains<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
176
3.09M
        let rhs = rhs.into();
177
3.09M
        (self & rhs).bits() == rhs.bits()
178
3.09M
    }
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::AccessType, u8>>::contains::<sawp_modbus::AccessType>
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::contains::<sawp_modbus::ErrorFlags>
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::contains::<sawp_modbus::CodeCategory>
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::contains::<sawp_pop3::ErrorFlag>
Line
Count
Source
175
987k
    pub fn contains<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
176
987k
        let rhs = rhs.into();
177
987k
        (self & rhs).bits() == rhs.bits()
178
987k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::contains::<sawp_modbus::AccessType>
Line
Count
Source
175
575k
    pub fn contains<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
176
575k
        let rhs = rhs.into();
177
575k
        (self & rhs).bits() == rhs.bits()
178
575k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::contains::<sawp_modbus::AccessType>
Line
Count
Source
175
37.0k
    pub fn contains<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
176
37.0k
        let rhs = rhs.into();
177
37.0k
        (self & rhs).bits() == rhs.bits()
178
37.0k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8>>::contains::<sawp_modbus::ErrorFlags>
Line
Count
Source
175
15.4k
    pub fn contains<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
176
15.4k
        let rhs = rhs.into();
177
15.4k
        (self & rhs).bits() == rhs.bits()
178
15.4k
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8>>::contains::<sawp_modbus::CodeCategory>
Line
Count
Source
175
12.3k
    pub fn contains<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
176
12.3k
        let rhs = rhs.into();
177
12.3k
        (self & rhs).bits() == rhs.bits()
178
12.3k
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8>>::contains::<sawp_pop3::ErrorFlag>
Line
Count
Source
175
1.34M
    pub fn contains<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
176
1.34M
        let rhs = rhs.into();
177
1.34M
        (self & rhs).bits() == rhs.bits()
178
1.34M
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::contains::<sawp_modbus::AccessType>
Line
Count
Source
175
123k
    pub fn contains<B: Into<Flags<Enum>>>(self, rhs: B) -> bool {
176
123k
        let rhs = rhs.into();
177
123k
        (self & rhs).bits() == rhs.bits()
178
123k
    }
179
180
3.23k
    pub fn is_empty(&self) -> bool {
181
3.23k
        self.bits() == <Enum as Flag>::none().bits()
182
3.23k
    }
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::AccessType, u8>>::is_empty
<sawp_flags::Flags<sawp_modbus::AccessType, u8>>::is_empty
Line
Count
Source
180
3.23k
    pub fn is_empty(&self) -> bool {
181
3.23k
        self.bits() == <Enum as Flag>::none().bits()
182
3.23k
    }
183
184
    pub fn is_all(&self) -> bool {
185
        self.bits() == <Enum as Flag>::all().bits()
186
    }
187
}
188
189
impl<Enum: Flag> From<Enum> for Flags<Enum> {
190
17.6M
    fn from(flag: Enum) -> Self {
191
17.6M
        Self::from_flag(flag)
192
17.6M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::convert::From<sawp_modbus::ErrorFlags>>::from
Line
Count
Source
190
4.21M
    fn from(flag: Enum) -> Self {
191
4.21M
        Self::from_flag(flag)
192
4.21M
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8> as core::convert::From<sawp_pop3::ErrorFlag>>::from
Line
Count
Source
190
987k
    fn from(flag: Enum) -> Self {
191
987k
        Self::from_flag(flag)
192
987k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::convert::From<sawp_modbus::AccessType>>::from
Line
Count
Source
190
1.66k
    fn from(flag: Enum) -> Self {
191
1.66k
        Self::from_flag(flag)
192
1.66k
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::convert::From<sawp_modbus::CodeCategory>>::from
Line
Count
Source
190
274
    fn from(flag: Enum) -> Self {
191
274
        Self::from_flag(flag)
192
274
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::convert::From<sawp_modbus::AccessType>>::from
Line
Count
Source
190
1.42M
    fn from(flag: Enum) -> Self {
191
1.42M
        Self::from_flag(flag)
192
1.42M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::convert::From<sawp_modbus::ErrorFlags>>::from
Line
Count
Source
190
1.44M
    fn from(flag: Enum) -> Self {
191
1.44M
        Self::from_flag(flag)
192
1.44M
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::convert::From<sawp_modbus::CodeCategory>>::from
Line
Count
Source
190
392k
    fn from(flag: Enum) -> Self {
191
392k
        Self::from_flag(flag)
192
392k
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8> as core::convert::From<sawp_pop3::ErrorFlag>>::from
Line
Count
Source
190
132k
    fn from(flag: Enum) -> Self {
191
132k
        Self::from_flag(flag)
192
132k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::convert::From<sawp_modbus::AccessType>>::from
Line
Count
Source
190
50.2k
    fn from(flag: Enum) -> Self {
191
50.2k
        Self::from_flag(flag)
192
50.2k
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::convert::From<sawp_modbus::CodeCategory>>::from
Line
Count
Source
190
13.7k
    fn from(flag: Enum) -> Self {
191
13.7k
        Self::from_flag(flag)
192
13.7k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::convert::From<sawp_modbus::ErrorFlags>>::from
Line
Count
Source
190
5.43M
    fn from(flag: Enum) -> Self {
191
5.43M
        Self::from_flag(flag)
192
5.43M
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8> as core::convert::From<sawp_pop3::ErrorFlag>>::from
Line
Count
Source
190
1.34M
    fn from(flag: Enum) -> Self {
191
1.34M
        Self::from_flag(flag)
192
1.34M
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::convert::From<sawp_modbus::AccessType>>::from
Line
Count
Source
190
418k
    fn from(flag: Enum) -> Self {
191
418k
        Self::from_flag(flag)
192
418k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::convert::From<sawp_modbus::ErrorFlags>>::from
Line
Count
Source
190
1.52M
    fn from(flag: Enum) -> Self {
191
1.52M
        Self::from_flag(flag)
192
1.52M
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::convert::From<sawp_modbus::CodeCategory>>::from
Line
Count
Source
190
183k
    fn from(flag: Enum) -> Self {
191
183k
        Self::from_flag(flag)
192
183k
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8> as core::convert::From<sawp_pop3::ErrorFlag>>::from
Line
Count
Source
190
85.3k
    fn from(flag: Enum) -> Self {
191
85.3k
        Self::from_flag(flag)
192
85.3k
    }
193
}
194
195
impl<Enum: Flag> PartialEq<Enum> for Flags<Enum> {
196
770k
    fn eq(&self, other: &Enum) -> bool {
197
770k
        self.bits() == other.bits()
198
770k
    }
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::cmp::PartialEq<sawp_modbus::AccessType>>::eq
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::cmp::PartialEq<sawp_modbus::CodeCategory>>::eq
Line
Count
Source
196
401k
    fn eq(&self, other: &Enum) -> bool {
197
401k
        self.bits() == other.bits()
198
401k
    }
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::cmp::PartialEq<sawp_modbus::AccessType>>::eq
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::cmp::PartialEq<sawp_modbus::CodeCategory>>::eq
Line
Count
Source
196
368k
    fn eq(&self, other: &Enum) -> bool {
197
368k
        self.bits() == other.bits()
198
368k
    }
199
}
200
201
impl<T, B> std::ops::BitOr<B> for Flags<T>
202
where
203
    T: Flag,
204
    B: Into<Flags<T>>,
205
{
206
    type Output = Flags<T>;
207
16.2k
    fn bitor(self, other: B) -> Flags<T> {
208
16.2k
        Flags::from_bits(self.bits() | other.into().bits())
209
16.2k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::ops::bit::BitOr<sawp_modbus::AccessType>>::bitor
Line
Count
Source
207
500
    fn bitor(self, other: B) -> Flags<T> {
208
500
        Flags::from_bits(self.bits() | other.into().bits())
209
500
    }
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::ops::bit::BitOr<sawp_modbus::CodeCategory>>::bitor
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::ops::bit::BitOr<sawp_modbus::AccessType>>::bitor
Line
Count
Source
207
4.34k
    fn bitor(self, other: B) -> Flags<T> {
208
4.34k
        Flags::from_bits(self.bits() | other.into().bits())
209
4.34k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::ops::bit::BitOr<sawp_modbus::AccessType>>::bitor
Line
Count
Source
207
3.31k
    fn bitor(self, other: B) -> Flags<T> {
208
3.31k
        Flags::from_bits(self.bits() | other.into().bits())
209
3.31k
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::ops::bit::BitOr<sawp_modbus::CodeCategory>>::bitor
Line
Count
Source
207
12
    fn bitor(self, other: B) -> Flags<T> {
208
12
        Flags::from_bits(self.bits() | other.into().bits())
209
12
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::ops::bit::BitOr<sawp_modbus::AccessType>>::bitor
Line
Count
Source
207
8.09k
    fn bitor(self, other: B) -> Flags<T> {
208
8.09k
        Flags::from_bits(self.bits() | other.into().bits())
209
8.09k
    }
210
}
211
212
impl<T, B> std::ops::BitOrAssign<B> for Flags<T>
213
where
214
    T: Flag,
215
    B: Into<Flags<T>>,
216
{
217
3.09M
    fn bitor_assign(&mut self, rhs: B) {
218
3.09M
        *self = Flags::from_bits(self.bits() | rhs.into().bits())
219
3.09M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::ops::bit::BitOrAssign<sawp_modbus::ErrorFlags>>::bitor_assign
Line
Count
Source
217
1.35M
    fn bitor_assign(&mut self, rhs: B) {
218
1.35M
        *self = Flags::from_bits(self.bits() | rhs.into().bits())
219
1.35M
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8> as core::ops::bit::BitOrAssign<sawp_pop3::ErrorFlag>>::bitor_assign
Line
Count
Source
217
132k
    fn bitor_assign(&mut self, rhs: B) {
218
132k
        *self = Flags::from_bits(self.bits() | rhs.into().bits())
219
132k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::ops::bit::BitOrAssign<sawp_modbus::ErrorFlags>>::bitor_assign
Line
Count
Source
217
1.51M
    fn bitor_assign(&mut self, rhs: B) {
218
1.51M
        *self = Flags::from_bits(self.bits() | rhs.into().bits())
219
1.51M
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8> as core::ops::bit::BitOrAssign<sawp_pop3::ErrorFlag>>::bitor_assign
Line
Count
Source
217
85.3k
    fn bitor_assign(&mut self, rhs: B) {
218
85.3k
        *self = Flags::from_bits(self.bits() | rhs.into().bits())
219
85.3k
    }
220
}
221
222
impl<T, B> std::ops::BitAnd<B> for Flags<T>
223
where
224
    T: Flag,
225
    B: Into<Flags<T>>,
226
{
227
    type Output = Flags<T>;
228
15.4M
    fn bitand(self, other: B) -> Flags<T> {
229
15.4M
        Flags::from_bits(self.bits() & other.into().bits())
230
15.4M
    }
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::ops::bit::BitAnd<sawp_modbus::AccessType>>::bitand
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::ops::bit::BitAnd>::bitand
Line
Count
Source
228
456k
    fn bitand(self, other: B) -> Flags<T> {
229
456k
        Flags::from_bits(self.bits() & other.into().bits())
230
456k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::ops::bit::BitAnd>::bitand
Line
Count
Source
228
4.38M
    fn bitand(self, other: B) -> Flags<T> {
229
4.38M
        Flags::from_bits(self.bits() & other.into().bits())
230
4.38M
    }
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::ops::bit::BitAnd>::bitand
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8> as core::ops::bit::BitAnd>::bitand
Line
Count
Source
228
987k
    fn bitand(self, other: B) -> Flags<T> {
229
987k
        Flags::from_bits(self.bits() & other.into().bits())
230
987k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::ops::bit::BitAnd>::bitand
Line
Count
Source
228
1.41M
    fn bitand(self, other: B) -> Flags<T> {
229
1.41M
        Flags::from_bits(self.bits() & other.into().bits())
230
1.41M
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::ops::bit::BitAnd>::bitand
Line
Count
Source
228
92.4k
    fn bitand(self, other: B) -> Flags<T> {
229
92.4k
        Flags::from_bits(self.bits() & other.into().bits())
230
92.4k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::ops::bit::BitAnd<sawp_modbus::AccessType>>::bitand
Line
Count
Source
228
3.07k
    fn bitand(self, other: B) -> Flags<T> {
229
3.07k
        Flags::from_bits(self.bits() & other.into().bits())
230
3.07k
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::ops::bit::BitAnd>::bitand
Line
Count
Source
228
858k
    fn bitand(self, other: B) -> Flags<T> {
229
858k
        Flags::from_bits(self.bits() & other.into().bits())
230
858k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::ops::bit::BitAnd>::bitand
Line
Count
Source
228
5.48M
    fn bitand(self, other: B) -> Flags<T> {
229
5.48M
        Flags::from_bits(self.bits() & other.into().bits())
230
5.48M
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::ops::bit::BitAnd>::bitand
Line
Count
Source
228
13.6k
    fn bitand(self, other: B) -> Flags<T> {
229
13.6k
        Flags::from_bits(self.bits() & other.into().bits())
230
13.6k
    }
<sawp_flags::Flags<sawp_pop3::ErrorFlag, u8> as core::ops::bit::BitAnd>::bitand
Line
Count
Source
228
1.34M
    fn bitand(self, other: B) -> Flags<T> {
229
1.34M
        Flags::from_bits(self.bits() & other.into().bits())
230
1.34M
    }
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::ops::bit::BitAnd>::bitand
Line
Count
Source
228
410k
    fn bitand(self, other: B) -> Flags<T> {
229
410k
        Flags::from_bits(self.bits() & other.into().bits())
230
410k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::ops::bit::BitAnd>::bitand
Line
Count
Source
228
6.44k
    fn bitand(self, other: B) -> Flags<T> {
229
6.44k
        Flags::from_bits(self.bits() & other.into().bits())
230
6.44k
    }
231
}
232
233
impl<T, B> std::ops::BitAndAssign<B> for Flags<T>
234
where
235
    T: Flag,
236
    B: Into<Flags<T>>,
237
{
238
    fn bitand_assign(&mut self, rhs: B) {
239
        *self = Flags::from_bits(self.bits() & rhs.into().bits())
240
    }
241
}
242
243
impl<T, B> std::ops::BitXor<B> for Flags<T>
244
where
245
    T: Flag,
246
    B: Into<Flags<T>>,
247
{
248
    type Output = Flags<T>;
249
    fn bitxor(self, other: B) -> Flags<T> {
250
        Flags::from_bits(self.bits() ^ other.into().bits())
251
    }
252
}
253
254
impl<T, B> std::ops::BitXorAssign<B> for Flags<T>
255
where
256
    T: Flag,
257
    B: Into<Flags<T>>,
258
{
259
    fn bitxor_assign(&mut self, rhs: B) {
260
        *self = Flags::from_bits(self.bits() ^ rhs.into().bits())
261
    }
262
}
263
264
impl<T: Flag> std::ops::Not for Flags<T> {
265
    type Output = Flags<T>;
266
267
2
    fn not(self) -> Self::Output {
268
2
        Flags::from_bits(!self.bits())
269
2
    }
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::ops::bit::Not>::not
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::ops::bit::Not>::not
Line
Count
Source
267
2
    fn not(self) -> Self::Output {
268
2
        Flags::from_bits(!self.bits())
269
2
    }
270
}
271
272
impl<T: Flag> std::fmt::Display for Flags<T> {
273
    /// A pipe-separated list of set flags.
274
9.25k
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
275
9.25k
        let none = self.bits() == T::none().bits();
276
9.25k
        let mut first = true;
277
9.25k
        for val in <T as Flag>::ITEMS
278
9.25k
            .iter()
279
9.25k
            .cloned()
280
64.7k
            .filter(move |&flag| self.contains(flag))
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::fmt::Display>::fmt::{closure#0}
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::fmt::Display>::fmt::{closure#0}
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::fmt::Display>::fmt::{closure#0}
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::fmt::Display>::fmt::{closure#0}
Line
Count
Source
280
37.0k
            .filter(move |&flag| self.contains(flag))
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::fmt::Display>::fmt::{closure#0}
Line
Count
Source
280
15.4k
            .filter(move |&flag| self.contains(flag))
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::fmt::Display>::fmt::{closure#0}
Line
Count
Source
280
12.3k
            .filter(move |&flag| self.contains(flag))
281
        {
282
4.47k
            write!(f, "{}{:?}", if first { "" } else { " | " }, val)?;
283
4.47k
            first = false;
284
285
4.47k
            if none {
286
0
                return Ok(());
287
4.47k
            }
288
        }
289
290
9.25k
        if none {
291
5.78k
            write!(f, "NONE")?;
292
3.47k
        }
293
294
9.25k
        Ok(())
295
9.25k
    }
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::fmt::Display>::fmt
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::fmt::Display>::fmt
Unexecuted instantiation: <sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::fmt::Display>::fmt
<sawp_flags::Flags<sawp_modbus::AccessType, u8> as core::fmt::Display>::fmt
Line
Count
Source
274
3.08k
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
275
3.08k
        let none = self.bits() == T::none().bits();
276
3.08k
        let mut first = true;
277
3.08k
        for val in <T as Flag>::ITEMS
278
3.08k
            .iter()
279
3.08k
            .cloned()
280
3.08k
            .filter(move |&flag| self.contains(flag))
281
        {
282
1.56k
            write!(f, "{}{:?}", if first { "" } else { " | " }, val)?;
283
1.56k
            first = false;
284
285
1.56k
            if none {
286
0
                return Ok(());
287
1.56k
            }
288
        }
289
290
3.08k
        if none {
291
2.50k
            write!(f, "NONE")?;
292
579
        }
293
294
3.08k
        Ok(())
295
3.08k
    }
<sawp_flags::Flags<sawp_modbus::ErrorFlags, u8> as core::fmt::Display>::fmt
Line
Count
Source
274
3.08k
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
275
3.08k
        let none = self.bits() == T::none().bits();
276
3.08k
        let mut first = true;
277
3.08k
        for val in <T as Flag>::ITEMS
278
3.08k
            .iter()
279
3.08k
            .cloned()
280
3.08k
            .filter(move |&flag| self.contains(flag))
281
        {
282
545
            write!(f, "{}{:?}", if first { "" } else { " | " }, val)?;
283
545
            first = false;
284
285
545
            if none {
286
0
                return Ok(());
287
545
            }
288
        }
289
290
3.08k
        if none {
291
2.56k
            write!(f, "NONE")?;
292
525
        }
293
294
3.08k
        Ok(())
295
3.08k
    }
<sawp_flags::Flags<sawp_modbus::CodeCategory, u8> as core::fmt::Display>::fmt
Line
Count
Source
274
3.08k
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
275
3.08k
        let none = self.bits() == T::none().bits();
276
3.08k
        let mut first = true;
277
3.08k
        for val in <T as Flag>::ITEMS
278
3.08k
            .iter()
279
3.08k
            .cloned()
280
3.08k
            .filter(move |&flag| self.contains(flag))
281
        {
282
2.36k
            write!(f, "{}{:?}", if first { "" } else { " | " }, val)?;
283
2.36k
            first = false;
284
285
2.36k
            if none {
286
0
                return Ok(());
287
2.36k
            }
288
        }
289
290
3.08k
        if none {
291
718
            write!(f, "NONE")?;
292
2.36k
        }
293
294
3.08k
        Ok(())
295
3.08k
    }
296
}
297
298
impl<T: Flag> std::fmt::Binary for Flags<T> {
299
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
300
        std::fmt::Binary::fmt(&self.bits(), f)
301
    }
302
}
303
304
/// Example enum deriving `BitFlags`
305
pub mod example {
306
    use super::*;
307
308
    /// Example enum
309
    #[derive(Debug, Clone, Copy, PartialEq, Eq, BitFlags)]
310
    #[repr(u8)]
311
    pub enum Test {
312
        A = 0b0001,
313
        B = 0b0010,
314
        C = 0b0100,
315
        D = 0b1000,
316
        /// Variants can be bitmask of other fields
317
        E = Test::A as u8 | Test::B as u8 | Test::C as u8 | Test::D as u8,
318
    }
319
}
320
321
#[cfg(test)]
322
mod test {
323
    use super::{example::*, *};
324
325
    #[test]
326
    fn test_enum_bits() {
327
        let bits = 0b1010_1010;
328
        let flags = Flags::<Test>::from_bits(bits);
329
        assert_eq!(bits, flags.bits());
330
        assert_eq!(&bits, flags.bits_ref());
331
    }
332
333
    #[test]
334
    fn test_enum_or() {
335
        let mut flags = Test::A | Test::B;
336
        assert_eq!(0b0011, flags.bits());
337
338
        flags |= Test::C;
339
        assert_eq!(0b0111, flags.bits());
340
341
        flags |= Test::C | Test::D;
342
        assert_eq!(0b1111, flags.bits());
343
    }
344
345
    #[test]
346
    fn test_enum_and() {
347
        let mut flags = Test::E & Test::B;
348
        assert_eq!(0b0010, flags.bits());
349
350
        flags &= Test::B;
351
        assert_eq!(0b0010, flags.bits());
352
353
        flags &= Test::E & Test::B;
354
        assert_eq!(0b0010, flags.bits());
355
    }
356
357
    #[test]
358
    fn test_enum_xor() {
359
        let mut flags = Test::A ^ Test::B;
360
        assert_eq!(0b0011, flags.bits());
361
362
        flags ^= Test::C;
363
        assert_eq!(0b0111, flags.bits());
364
365
        flags ^= Test::D ^ Test::B;
366
        assert_eq!(0b1101, flags.bits());
367
    }
368
369
    #[test]
370
    fn test_enum_not() {
371
        let flags = !Test::A;
372
        assert_eq!(0b1111_1110, flags.bits());
373
        let flags = !(Test::A ^ Test::B);
374
        assert_eq!(0b1111_1100, flags.bits());
375
    }
376
377
    #[test]
378
    fn test_contains() {
379
        let flags = Test::A | Test::C;
380
        assert!(flags.contains(Test::A));
381
        assert!(!flags.contains(Test::B));
382
        assert!(!flags.contains(Test::E));
383
        assert!(!flags.contains(Test::B | Test::D));
384
        assert!(!flags.contains(Test::A | Test::B));
385
        assert!(flags.contains(Test::A | Test::C));
386
    }
387
388
    #[test]
389
    fn test_intersects() {
390
        let flags = Test::A | Test::C;
391
        assert!(flags.intersects(Test::A));
392
        assert!(flags.intersects(Test::E));
393
        assert!(flags.intersects(Test::A | Test::B));
394
        assert!(flags.intersects(Test::A | Test::C));
395
        assert!(flags.intersects(Test::A | Test::B | Test::C));
396
        assert!(!flags.intersects(Test::B | Test::D));
397
    }
398
399
    #[test]
400
    fn test_eq() {
401
        let flags = Test::A;
402
        assert_eq!(flags, Test::A);
403
        assert_eq!(Test::A, flags);
404
405
        let flags = Test::A | Test::C;
406
        assert_ne!(flags, Test::A);
407
        assert_ne!(flags, Test::C);
408
        assert_ne!(Test::A, flags);
409
        assert_eq!(flags, Test::A | Test::C);
410
        assert_ne!(flags, Test::A | Test::C | Test::E);
411
412
        let flags = Flags::<Test>::from_bits(0b1000_0001);
413
        assert_ne!(flags, Test::A);
414
    }
415
416
    #[test]
417
    fn test_enum_string() {
418
        assert_eq!("NONE", Test::none().to_string());
419
        assert_eq!("A", Test::A.to_string());
420
        assert_eq!("A | B", (Test::A | Test::B).to_string());
421
        assert_eq!("A | B | C | D | E", Test::E.to_string());
422
        assert_eq!("A | B | C | D | E", Flags::from_flag(Test::E).to_string());
423
    }
424
425
    #[test]
426
    fn test_enum_string_none() {
427
        #[derive(Debug, Clone, Copy, PartialEq, Eq, BitFlags)]
428
        #[repr(u8)]
429
        pub enum Test {
430
            Zero = 0b0000,
431
            A = 0b0001,
432
            B = 0b0010,
433
            C = 0b0100,
434
            D = 0b1000,
435
            /// Variants can be bitmask of other fields
436
            E = Test::A as u8 | Test::B as u8 | Test::C as u8 | Test::D as u8,
437
        }
438
        assert_eq!("Zero", Test::Zero.to_string());
439
        assert_eq!("Zero", Test::none().to_string());
440
        assert_eq!("Zero", Flags::from_flag(Test::Zero).to_string());
441
    }
442
443
    #[test]
444
    fn test_enum_format() {
445
        assert_eq!("A", format!("{:?}", Test::A));
446
        assert_eq!("E", format!("{:?}", Test::E));
447
        assert_eq!("0", format!("{:?}", Test::none()));
448
449
        assert_eq!("0", format!("{:b}", Test::none()));
450
        assert_eq!("1", format!("{:b}", Test::A));
451
        assert_eq!("1111", format!("{:b}", Test::E));
452
    }
453
454
    #[test]
455
    fn test_enum_from_str() {
456
        use std::str::FromStr;
457
        assert_eq!(Err(()), Test::from_str(""));
458
        assert_eq!(Ok(Test::A), Test::from_str("a"));
459
        assert_eq!(Ok(Test::A), Test::from_str("A"));
460
    }
461
462
    #[test]
463
    fn test_all() {
464
        assert_eq!(Test::E, Test::all());
465
        assert!(!Flags::from_flag(Test::A).is_all());
466
        assert!(Flags::from_flag(Test::E).is_all());
467
    }
468
469
    #[test]
470
    fn test_none() {
471
        assert_eq!(Flags::from_bits(0), Test::none());
472
        assert!(Flags::<Test>::from_bits(0).is_empty());
473
        assert!(!Flags::from_flag(Test::A).is_empty());
474
        assert!(!Flags::from_flag(Test::E).is_empty());
475
    }
476
}