summaryrefslogtreecommitdiffstats
path: root/openpgp/src/types
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2023-07-03 12:37:07 +0200
committerJustus Winter <justus@sequoia-pgp.org>2023-07-18 17:06:52 +0200
commit68d52deb8bf7c3b50066a91ab4af9ab5ac74402b (patch)
treee8f89bc2253e3b985bb2cb6766622d120a1ba784 /openpgp/src/types
parentdc8f39503894b282d0a98e5012e512768cabb49f (diff)
openpgp: Modify Bitfields in-place.
Diffstat (limited to 'openpgp/src/types')
-rw-r--r--openpgp/src/types/bitfield.rs10
-rw-r--r--openpgp/src/types/features.rs12
-rw-r--r--openpgp/src/types/key_flags.rs12
-rw-r--r--openpgp/src/types/server_preferences.rs12
4 files changed, 27 insertions, 19 deletions
diff --git a/openpgp/src/types/bitfield.rs b/openpgp/src/types/bitfield.rs
index d54ba2be..f4e5893f 100644
--- a/openpgp/src/types/bitfield.rs
+++ b/openpgp/src/types/bitfield.rs
@@ -93,33 +93,29 @@ impl Bitfield {
}
/// Canonicalize by removing any trailing zero bytes.
- pub fn canonicalize(mut self) -> Self {
+ pub fn canonicalize(&mut self) {
while !self.raw.is_empty() && self.raw[self.raw.len() - 1] == 0 {
self.raw.truncate(self.raw.len() - 1);
}
-
- self
}
/// Sets the specified flag.
- pub fn set(mut self, bit: usize) -> Self {
+ pub fn set(&mut self, bit: usize) {
let byte = bit / 8;
while self.raw.len() <= byte {
self.raw.push(0);
}
self.raw[byte] |= 1 << (bit % 8);
- self
}
/// Clears the specified flag.
///
/// Note: This does not implicitly canonicalize the bit field. To
/// do that, invoke [`Bitfield::canonicalize`].
- pub fn clear(mut self, bit: usize) -> Self {
+ pub fn clear(&mut self, bit: usize) {
let byte = bit / 8;
if byte < self.raw.len() {
self.raw[byte] &= !(1 << (bit % 8));
}
- self
}
}
diff --git a/openpgp/src/types/features.rs b/openpgp/src/types/features.rs
index 856ccf2f..2a3689ce 100644
--- a/openpgp/src/types/features.rs
+++ b/openpgp/src/types/features.rs
@@ -198,8 +198,10 @@ impl Features {
/// # assert!(! f.supports_aead());
/// # Ok(()) }
/// ```
- pub fn set(self, bit: usize) -> Self {
- Self(self.0.set(bit).canonicalize())
+ pub fn set(mut self, bit: usize) -> Self {
+ self.0.set(bit);
+ self.0.canonicalize();
+ self
}
/// Clears the specified feature flag.
@@ -224,8 +226,10 @@ impl Features {
/// # assert!(! f.supports_aead());
/// # Ok(()) }
/// ```
- pub fn clear(self, bit: usize) -> Self {
- Self(self.0.clear(bit).canonicalize())
+ pub fn clear(mut self, bit: usize) -> Self {
+ self.0.clear(bit);
+ self.0.canonicalize();
+ self
}
/// Returns whether the MDC feature flag is set.
diff --git a/openpgp/src/types/key_flags.rs b/openpgp/src/types/key_flags.rs
index a1867bad..5c3a8945 100644
--- a/openpgp/src/types/key_flags.rs
+++ b/openpgp/src/types/key_flags.rs
@@ -237,8 +237,10 @@ impl KeyFlags {
/// # assert!(kf.for_certification());
/// # Ok(()) }
/// ```
- pub fn set(self, bit: usize) -> Self {
- Self(self.0.set(bit).canonicalize())
+ pub fn set(mut self, bit: usize) -> Self {
+ self.0.set(bit);
+ self.0.canonicalize();
+ self
}
/// Clears the specified key flag.
@@ -261,8 +263,10 @@ impl KeyFlags {
/// # assert!(kf.for_certification());
/// # Ok(()) }
/// ```
- pub fn clear(self, bit: usize) -> Self {
- Self(self.0.clear(bit).canonicalize())
+ pub fn clear(mut self, bit: usize) -> Self {
+ self.0.clear(bit);
+ self.0.canonicalize();
+ self
}
/// This key may be used to certify other keys.
diff --git a/openpgp/src/types/server_preferences.rs b/openpgp/src/types/server_preferences.rs
index f2a8e637..cd3d10aa 100644
--- a/openpgp/src/types/server_preferences.rs
+++ b/openpgp/src/types/server_preferences.rs
@@ -175,8 +175,10 @@ impl KeyServerPreferences {
/// # assert!(! ksp.no_modify());
/// # Ok(()) }
/// ```
- pub fn set(self, bit: usize) -> Self {
- Self(self.0.set(bit).canonicalize())
+ pub fn set(mut self, bit: usize) -> Self {
+ self.0.set(bit);
+ self.0.canonicalize();
+ self
}
/// Clears the specified keyserver preference flag.
@@ -199,8 +201,10 @@ impl KeyServerPreferences {
/// # assert!(! ksp.no_modify());
/// # Ok(()) }
/// ```
- pub fn clear(self, bit: usize) -> Self {
- Self(self.0.clear(bit).canonicalize())
+ pub fn clear(mut self, bit: usize) -> Self {
+ self.0.clear(bit);
+ self.0.canonicalize();
+ self
}
/// Returns whether the certificate's owner requests that the