diff options
-rw-r--r-- | openpgp/src/packet/key_flags.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/openpgp/src/packet/key_flags.rs b/openpgp/src/packet/key_flags.rs index 830dcc3d..92dac82f 100644 --- a/openpgp/src/packet/key_flags.rs +++ b/openpgp/src/packet/key_flags.rs @@ -1,5 +1,6 @@ use std::fmt; use std::cmp; +use std::ops::{BitAnd, BitOr}; /// Describes how a key may be used, and stores additional /// information. @@ -78,6 +79,44 @@ impl PartialOrd for KeyFlags { } } +impl BitAnd for &KeyFlags { + type Output = KeyFlags; + + fn bitand(self, rhs: Self) -> KeyFlags { + let l = self.as_vec(); + let r = rhs.as_vec(); + + let mut c = Vec::with_capacity(cmp::min(l.len(), r.len())); + for (l, r) in l.into_iter().zip(r.into_iter()) { + c.push(l & r); + } + + KeyFlags::new(&c[..]) + } +} + +impl BitOr for &KeyFlags { + type Output = KeyFlags; + + fn bitor(self, rhs: Self) -> KeyFlags { + let l = self.as_vec(); + let r = rhs.as_vec(); + + // Make l the longer one. + let (mut l, r) = if l.len() > r.len() { + (l, r) + } else { + (r, l) + }; + + for (i, r) in r.into_iter().enumerate() { + l[i] = l[i] | r; + } + + KeyFlags::new(&l[..]) + } +} + impl KeyFlags { /// Creates a new instance from `bits`. pub fn new(bits: &[u8]) -> Self { |