diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-07-24 15:49:43 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-07-24 16:45:10 +0200 |
commit | 0ac1c16d62b7a1900779be513579419f6a0ec730 (patch) | |
tree | 29b3c71f21b38c8cff04dd84de93c1dc561d9f33 /openpgp/src/parse.rs | |
parent | 14fe75c02b0ae03440d5ba25efe8d1c64119272c (diff) |
openpgp: Improve PacketParserResult::as_ref, as_mut, and map.
- Previously, these method withheld information in the EOF case (and
in case of `map` this loss is irrecoverable). Fix this by
returning a Result instead.
Diffstat (limited to 'openpgp/src/parse.rs')
-rw-r--r-- | openpgp/src/parse.rs | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs index 16c7ec6e..f36f634c 100644 --- a/openpgp/src/parse.rs +++ b/openpgp/src/parse.rs @@ -175,6 +175,7 @@ use std::str; use std::mem; use std::fmt; use std::path::Path; +use std::result::Result as StdResult; use ::buffered_reader::*; @@ -3315,20 +3316,20 @@ impl<'a> PacketParserResult<'a> { } /// Like `Option::as_ref`(). - pub fn as_ref(&self) -> Option<&PacketParser<'a>> { - if let PacketParserResult::Some(ref pp) = self { - Some(pp) - } else { - None + pub fn as_ref(&self) + -> StdResult<&PacketParser<'a>, &PacketParserEOF> { + match self { + PacketParserResult::Some(pp) => Ok(pp), + PacketParserResult::EOF(eof) => Err(eof), } } /// Like `Option::as_mut`(). - pub fn as_mut(&mut self) -> Option<&mut PacketParser<'a>> { - if let PacketParserResult::Some(ref mut pp) = self { - Some(pp) - } else { - None + pub fn as_mut(&mut self) + -> StdResult<&mut PacketParser<'a>, &mut PacketParserEOF> { + match self { + PacketParserResult::Some(pp) => Ok(pp), + PacketParserResult::EOF(eof) => Err(eof), } } @@ -3345,12 +3346,12 @@ impl<'a> PacketParserResult<'a> { } /// Like `Option::map`(). - pub fn map<U, F>(self, f: F) -> Option<U> + pub fn map<U, F>(self, f: F) -> StdResult<U, PacketParserEOF> where F: FnOnce(PacketParser<'a>) -> U { match self { - PacketParserResult::Some(x) => Some(f(x)), - PacketParserResult::EOF(_) => None, + PacketParserResult::Some(x) => Ok(f(x)), + PacketParserResult::EOF(e) => Err(e), } } } |