summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2020-05-06 13:32:13 +0200
committerJustus Winter <justus@sequoia-pgp.org>2020-05-06 18:38:19 +0200
commit320884d10326fe186e974352391d51caa5cde571 (patch)
tree141d749c79f46183dfb2bd01e7cd7ae659292921
parentc575a9b4d3d0644457d3a2ddfbfa0160d3490f19 (diff)
openpgp: Streamline iteration over MessageStructure.
- Implement IntoIter and Deref to &'a [MessageLayer<'a>], drop the custom iteration structs.
-rw-r--r--openpgp-ffi/examples/decrypt-with.c4
-rw-r--r--openpgp-ffi/include/sequoia/openpgp.h2
-rw-r--r--openpgp-ffi/src/parse/stream.rs18
-rw-r--r--openpgp/src/parse/stream.rs33
-rw-r--r--openpgp/src/policy.rs4
-rw-r--r--tool/src/commands/mod.rs2
6 files changed, 24 insertions, 39 deletions
diff --git a/openpgp-ffi/examples/decrypt-with.c b/openpgp-ffi/examples/decrypt-with.c
index 8bdb2d9d..4519a2f1 100644
--- a/openpgp-ffi/examples/decrypt-with.c
+++ b/openpgp-ffi/examples/decrypt-with.c
@@ -41,7 +41,8 @@ get_certs_cb (void *cookie_raw,
static pgp_status_t
check_cb (void *cookie_opaque, pgp_message_structure_t structure)
{
- pgp_message_structure_iter_t iter = pgp_message_structure_iter (structure);
+ pgp_message_structure_iter_t iter =
+ pgp_message_structure_into_iter (structure);
for (pgp_message_layer_t layer = pgp_message_structure_iter_next (iter);
layer;
@@ -142,7 +143,6 @@ check_cb (void *cookie_opaque, pgp_message_structure_t structure)
}
pgp_message_structure_iter_free (iter);
- pgp_message_structure_free (structure);
/* Implement your verification policy here. */
return PGP_STATUS_SUCCESS;
diff --git a/openpgp-ffi/include/sequoia/openpgp.h b/openpgp-ffi/include/sequoia/openpgp.h
index d6a7b219..580134ee 100644
--- a/openpgp-ffi/include/sequoia/openpgp.h
+++ b/openpgp-ffi/include/sequoia/openpgp.h
@@ -1823,7 +1823,7 @@ void pgp_message_structure_free (pgp_message_structure_t);
char *pgp_message_structure_debug (const pgp_message_structure_t);
pgp_message_structure_iter_t
-pgp_message_structure_iter (pgp_message_structure_t);
+pgp_message_structure_into_iter (pgp_message_structure_t);
/*/
/// Frees this object.
diff --git a/openpgp-ffi/src/parse/stream.rs b/openpgp-ffi/src/parse/stream.rs
index 9e23cb47..da9d45c3 100644
--- a/openpgp-ffi/src/parse/stream.rs
+++ b/openpgp-ffi/src/parse/stream.rs
@@ -59,14 +59,15 @@ pub struct MessageStructure<'a>(stream::MessageStructure<'a>);
/// Iterates over the message structure.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "C"
-fn pgp_message_structure_iter(structure: *const MessageStructure)
- -> *mut MessageStructureIter {
- structure.ref_raw().iter().move_into_raw()
+fn pgp_message_structure_into_iter(structure: *mut MessageStructure)
+ -> *mut MessageStructureIter {
+ structure.move_from_raw().into_iter().move_into_raw()
}
/// Iterates over the message structure.
#[crate::ffi_wrapper_type(prefix = "pgp_", derive = "Iterator(MessageLayer)")]
-pub struct MessageStructureIter<'a>(stream::MessageStructureIter<'a>);
+pub struct MessageStructureIter<'a>(
+ std::vec::IntoIter<stream::MessageLayer<'a>>);
/// Represents a layer of the message structure.
#[crate::ffi_wrapper_type(prefix = "pgp_", derive = "Debug")]
@@ -570,7 +571,7 @@ impl VerificationHelper for VHelper {
/// check_cb (void *cookie_opaque, pgp_message_structure_t structure)
/// {
/// pgp_message_structure_iter_t iter =
-/// pgp_message_structure_iter (structure);
+/// pgp_message_structure_into_iter (structure);
/// pgp_message_layer_t layer = pgp_message_structure_iter_next (iter);
/// assert (layer);
/// assert (pgp_message_layer_compression (layer, NULL));
@@ -591,7 +592,6 @@ impl VerificationHelper for VHelper {
/// pgp_verification_result_iter_free (results);
/// pgp_message_layer_free (layer);
/// pgp_message_structure_iter_free (iter);
-/// pgp_message_structure_free (structure);
/// return PGP_STATUS_SUCCESS;
/// }
///
@@ -697,7 +697,7 @@ pub struct DetachedVerifier(openpgp::parse::stream::DetachedVerifier<'static, VH
/// check_cb (void *cookie_opaque, pgp_message_structure_t structure)
/// {
/// pgp_message_structure_iter_t iter =
-/// pgp_message_structure_iter (structure);
+/// pgp_message_structure_into_iter (structure);
/// pgp_message_layer_t layer = pgp_message_structure_iter_next (iter);
/// assert (layer);
/// pgp_verification_result_iter_t results;
@@ -714,7 +714,6 @@ pub struct DetachedVerifier(openpgp::parse::stream::DetachedVerifier<'static, VH
/// pgp_verification_result_iter_free (results);
/// pgp_message_layer_free (layer);
/// pgp_message_structure_iter_free (iter);
-/// pgp_message_structure_free (structure);
/// return PGP_STATUS_SUCCESS;
/// }
///
@@ -932,13 +931,12 @@ impl DecryptionHelper for DHelper {
/// check_cb (void *cookie_opaque, pgp_message_structure_t structure)
/// {
/// pgp_message_structure_iter_t iter =
-/// pgp_message_structure_iter (structure);
+/// pgp_message_structure_into_iter (structure);
/// pgp_message_layer_t layer = pgp_message_structure_iter_next (iter);
/// assert (layer);
/// assert (pgp_message_layer_encryption (layer, NULL, NULL));
/// pgp_message_layer_free (layer);
/// pgp_message_structure_iter_free (iter);
-/// pgp_message_structure_free (structure);
/// return PGP_STATUS_SUCCESS;
/// }
///
diff --git a/openpgp/src/parse/stream.rs b/openpgp/src/parse/stream.rs
index 383b883e..fb7d0cf2 100644
--- a/openpgp/src/parse/stream.rs
+++ b/openpgp/src/parse/stream.rs
@@ -282,35 +282,22 @@ impl<'a> MessageStructure<'a> {
panic!("cannot push to encryption or compression layer");
}
}
-
- /// Iterates over the message structure.
- pub fn iter(&self) -> MessageStructureIter {
- MessageStructureIter(self.0.iter())
- }
-
- /// Iterates over the message structure.
- pub fn into_iter(self) -> impl Iterator<Item = MessageLayer<'a>> {
- MessageStructureIntoIter(self.0.into_iter())
- }
}
-/// Iterates over the message structure.
-pub struct MessageStructureIter<'a>(::std::slice::Iter<'a, MessageLayer<'a>>);
+impl<'a> std::ops::Deref for MessageStructure<'a> {
+ type Target = [MessageLayer<'a>];
-impl<'a> Iterator for MessageStructureIter<'a> {
- type Item = &'a MessageLayer<'a>;
- fn next(&mut self) -> Option<Self::Item> {
- self.0.next()
+ fn deref(&self) -> &Self::Target {
+ &self.0[..]
}
}
-/// Iterates over the message structure.
-struct MessageStructureIntoIter<'a>(::std::vec::IntoIter<MessageLayer<'a>>);
-
-impl<'a> Iterator for MessageStructureIntoIter<'a> {
+impl<'a> IntoIterator for MessageStructure<'a> {
type Item = MessageLayer<'a>;
- fn next(&mut self) -> Option<Self::Item> {
- self.0.next()
+ type IntoIter = std::vec::IntoIter<MessageLayer<'a>>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.0.into_iter()
}
}
@@ -1816,7 +1803,7 @@ mod test {
}
fn check(&mut self, structure: MessageStructure) -> Result<()> {
- assert_eq!(structure.iter().count(), 2);
+ assert_eq!(structure.len(), 2);
for (i, layer) in structure.into_iter().enumerate() {
match layer {
MessageLayer::SignatureGroup { results } => {
diff --git a/openpgp/src/policy.rs b/openpgp/src/policy.rs
index af676a69..22eb32ed 100644
--- a/openpgp/src/policy.rs
+++ b/openpgp/src/policy.rs
@@ -1178,7 +1178,7 @@ mod test {
fn check(&mut self, structure: MessageStructure) -> Result<()>
{
- for layer in structure.iter() {
+ for layer in structure {
match layer {
MessageLayer::SignatureGroup { ref results } =>
for result in results {
@@ -1643,7 +1643,7 @@ mod test {
fn check(&mut self, structure: MessageStructure) -> Result<()>
{
- for layer in structure.iter() {
+ for layer in structure {
match layer {
MessageLayer::SignatureGroup { ref results } =>
for result in results {
diff --git a/tool/src/commands/mod.rs b/tool/src/commands/mod.rs
index b1de8fa8..08219286 100644
--- a/tool/src/commands/mod.rs
+++ b/tool/src/commands/mod.rs
@@ -357,7 +357,7 @@ impl<'a> VerificationHelper for VHelper<'a> {
}
fn check(&mut self, structure: MessageStructure) -> Result<()> {
- for layer in structure.iter() {
+ for layer in structure {
match layer {
MessageLayer::Compression { algo } =>
eprintln!("Compressed using {}", algo),