summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2019-03-14 09:07:53 +0100
committerNeal H. Walfield <neal@pep.foundation>2019-03-14 14:56:37 +0100
commit6bea117391c1813e956e384c6638b7b0311d8d7c (patch)
treed608a02b634070fd2e93408d81562ea68d0ea9ed
parent38defdd34ac927560172439c93295d3cce6f6171 (diff)
openpgp: Implement BitAnd and BitOr for KeyFlags
-rw-r--r--openpgp/src/packet/key_flags.rs39
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 {