diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-05-06 13:32:13 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-05-06 18:38:19 +0200 |
commit | 320884d10326fe186e974352391d51caa5cde571 (patch) | |
tree | 141d749c79f46183dfb2bd01e7cd7ae659292921 /openpgp/src | |
parent | c575a9b4d3d0644457d3a2ddfbfa0160d3490f19 (diff) |
openpgp: Streamline iteration over MessageStructure.
- Implement IntoIter and Deref to &'a [MessageLayer<'a>], drop the
custom iteration structs.
Diffstat (limited to 'openpgp/src')
-rw-r--r-- | openpgp/src/parse/stream.rs | 33 | ||||
-rw-r--r-- | openpgp/src/policy.rs | 4 |
2 files changed, 12 insertions, 25 deletions
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 { |