diff options
author | Neal H. Walfield <neal@pep.foundation> | 2020-03-03 23:20:33 +0100 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2020-03-03 23:52:03 +0100 |
commit | a61bfdab687e9c0b6c5e263ed304f48183059a45 (patch) | |
tree | 545d516b6b2fe513f63caf3bcc81024338459583 /openpgp/src/serialize/sexp.rs | |
parent | 93dbeb3160262726d4c60b4811cf352c08dfc5b3 (diff) |
openpgp: Only impl Serialize for objects that are normally exported.
- Add two new traits: `Marshal` and `MarshalInto`.
- Implement them instead of `Serialize` and `SerializeInto`.
- Only implement `Serialize` and `SerializeInto` for data structures
that are normally exported.
- This should prevent users from accidentally serializing a bare
signature (`Signature`) when they meant to serialize a signature
packet (`Packet`), for instance.
- Fixes #368.
Diffstat (limited to 'openpgp/src/serialize/sexp.rs')
-rw-r--r-- | openpgp/src/serialize/sexp.rs | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/openpgp/src/serialize/sexp.rs b/openpgp/src/serialize/sexp.rs index a5468d2d..8d8243a8 100644 --- a/openpgp/src/serialize/sexp.rs +++ b/openpgp/src/serialize/sexp.rs @@ -1,9 +1,15 @@ use crate::Result; -use crate::serialize::{Serialize, SerializeInto, generic_serialize_into}; +use crate::serialize::{ + Marshal, + MarshalInto, + generic_serialize_into, +}; use crate::crypto::sexp::{Sexp, String_}; -impl Serialize for Sexp { +impl crate::serialize::Serialize for Sexp {} + +impl Marshal for Sexp { fn serialize(&self, o: &mut dyn std::io::Write) -> Result<()> { match self { Sexp::String(ref s) => s.serialize(o), @@ -19,7 +25,9 @@ impl Serialize for Sexp { } } -impl SerializeInto for Sexp { +impl crate::serialize::SerializeInto for Sexp {} + +impl MarshalInto for Sexp { fn serialized_len(&self) -> usize { match self { Sexp::String(ref s) => s.serialized_len(), @@ -33,7 +41,7 @@ impl SerializeInto for Sexp { } } -impl Serialize for String_ { +impl Marshal for String_ { fn serialize(&self, o: &mut dyn std::io::Write) -> Result<()> { if let Some(display) = self.display_hint() { write!(o, "[{}:", display.len())?; @@ -59,7 +67,7 @@ fn size_tag_len(len: usize) -> usize { std::cmp::max(1, digits) // 0 takes up 1 char, too. } -impl SerializeInto for String_ { +impl MarshalInto for String_ { fn serialized_len(&self) -> usize { self.display_hint() .map(|d| size_tag_len(d.len()) + 3 + d.len()).unwrap_or(0) |