summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2023-12-11 18:15:01 +0100
committerJustus Winter <justus@sequoia-pgp.org>2024-01-25 11:19:44 +0100
commitde60c80822845f7a79a5ef394e215949bcd2738c (patch)
tree99396a0a7246e19ec03f7d3ea6dbb76c56971fc0
parent50a8131156684353cb51f62c2ea5fa9d7be5c549 (diff)
openpgp: Make TSK optionally own the Cert.
-rw-r--r--openpgp/src/serialize/cert.rs27
1 files changed, 25 insertions, 2 deletions
diff --git a/openpgp/src/serialize/cert.rs b/openpgp/src/serialize/cert.rs
index 83e804ed..b1dfe3f0 100644
--- a/openpgp/src/serialize/cert.rs
+++ b/openpgp/src/serialize/cert.rs
@@ -1,3 +1,7 @@
+use std::{
+ borrow::Cow,
+};
+
use crate::Result;
use crate::cert::prelude::*;
use crate::packet::{header::BodyLength, key, Signature, Tag};
@@ -264,6 +268,15 @@ impl Cert {
pub fn as_tsk(&self) -> TSK {
TSK::new(self)
}
+
+ /// Derive a [`TSK`] object from this key that owns the `Cert`.
+ ///
+ /// This object writes out secret keys during serialization.
+ ///
+ /// [`TSK`]: crate::serialize::TSK
+ pub fn into_tsk(self) -> TSK<'static> {
+ TSK::from(self)
+ }
}
/// A reference to a `Cert` that allows serialization of secret keys.
@@ -293,7 +306,7 @@ impl Cert {
/// # Ok(()) }
/// ```
pub struct TSK<'a> {
- pub(crate) cert: &'a Cert,
+ pub(crate) cert: Cow<'a, Cert>,
filter: Box<dyn Fn(&key::UnspecifiedSecret) -> bool + 'a>,
emit_stubs: bool,
}
@@ -333,11 +346,21 @@ impl PartialEq for TSK<'_> {
}
}
+impl From<Cert> for TSK<'_> {
+ fn from(c: Cert) -> Self {
+ Self {
+ cert: Cow::Owned(c),
+ filter: Box::new(|_| true),
+ emit_stubs: false,
+ }
+ }
+}
+
impl<'a> TSK<'a> {
/// Creates a new view for the given `Cert`.
fn new(cert: &'a Cert) -> Self {
Self {
- cert,
+ cert: Cow::Borrowed(cert),
filter: Box::new(|_| true),
emit_stubs: false,
}