diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2023-07-04 18:24:19 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2023-07-04 18:36:22 +0200 |
commit | c7adc7a5b3929956c1960493dfc1c7c5c624af9b (patch) | |
tree | 95f68f70a15178e6218cdfc1961b66c5e0571d37 | |
parent | e4427e5d0f589ac72eb409e43a547003347b2fc5 (diff) |
openpgp: Avoid extra copy.
-rw-r--r-- | openpgp/src/parse.rs | 32 |
1 files changed, 8 insertions, 24 deletions
diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs index 03670bdf..d78fff36 100644 --- a/openpgp/src/parse.rs +++ b/openpgp/src/parse.rs @@ -527,35 +527,19 @@ impl<'a> PacketHeaderParser<'a> { fn ok(mut self, packet: Packet) -> Result<PacketParser<'a>> { let total_out = self.reader.total_out(); - let mut reader = if self.state.settings.map { - // Read the body for the map. Note that - // `total_out` does not account for the body. - // - // XXX avoid the extra copy. + if self.state.settings.map { + // Steal the body for the map. + self.reader.rewind(); let body = self.reader.steal_eof()?; if !body.is_empty() { self.field("body", body.len()); } + self.map.as_mut().unwrap().finalize(body); + } - // This is a buffered_reader::Dup, so this always has an - // inner. - let inner = Box::new(self.reader).into_inner().unwrap(); - - // Combine the header with the body for the map. - let mut data = Vec::with_capacity(total_out + body.len()); - // We know that the inner reader must have at least - // `total_out` bytes buffered, otherwise we could never - // have read that much from the `buffered_reader::Dup`. - data.extend_from_slice(&inner.buffer()[..total_out]); - data.extend(body); - self.map.as_mut().unwrap().finalize(data); - - inner - } else { - // This is a buffered_reader::Dup, so this always has an - // inner. - Box::new(self.reader).into_inner().unwrap() - }; + // This is a buffered_reader::Dup, so this always has an + // inner. + let mut reader = Box::new(self.reader).into_inner().unwrap(); if total_out > 0 { // We know the data has been read, so this cannot fail. |