diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-08-30 13:05:38 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-08-30 16:52:39 +0200 |
commit | 540069fe1baa001deafe910f4e678512be6da122 (patch) | |
tree | fad90c1206d29824b678a38b6b46eba883ceca64 | |
parent | a06338a6b54d2104f485ae2f113b8cdced7a275a (diff) |
openpgp: Fix traversing writer stack across generics.
-rw-r--r-- | openpgp/src/crypto/aead.rs | 11 | ||||
-rw-r--r-- | openpgp/src/crypto/symmetric.rs | 11 | ||||
-rw-r--r-- | openpgp/src/serialize/writer/mod.rs | 20 | ||||
-rw-r--r-- | openpgp/src/serialize/writer/writer_bzip2.rs | 4 | ||||
-rw-r--r-- | openpgp/src/serialize/writer/writer_deflate.rs | 8 |
5 files changed, 44 insertions, 10 deletions
diff --git a/openpgp/src/crypto/aead.rs b/openpgp/src/crypto/aead.rs index 9b70c812..4bda0e41 100644 --- a/openpgp/src/crypto/aead.rs +++ b/openpgp/src/crypto/aead.rs @@ -707,6 +707,17 @@ impl<W: io::Write> Encryptor<W> { "Inner writer was taken").into()) } } + + /// Acquires a reference to the underlying writer. + pub fn get_ref(&self) -> Option<&W> { + self.inner.as_ref() + } + + /// Acquires a mutable reference to the underlying writer. + #[allow(dead_code)] + pub fn get_mut(&mut self) -> Option<&mut W> { + self.inner.as_mut() + } } impl<W: io::Write> io::Write for Encryptor<W> { diff --git a/openpgp/src/crypto/symmetric.rs b/openpgp/src/crypto/symmetric.rs index 681dd88f..4c352ad6 100644 --- a/openpgp/src/crypto/symmetric.rs +++ b/openpgp/src/crypto/symmetric.rs @@ -440,6 +440,17 @@ impl<W: io::Write> Encryptor<W> { "Inner writer was taken").into()) } } + + /// Acquires a reference to the underlying writer. + pub fn get_ref(&self) -> Option<&W> { + self.inner.as_ref() + } + + /// Acquires a mutable reference to the underlying writer. + #[allow(dead_code)] + pub fn get_mut(&mut self) -> Option<&mut W> { + self.inner.as_mut() + } } impl<W: io::Write> io::Write for Encryptor<W> { diff --git a/openpgp/src/serialize/writer/mod.rs b/openpgp/src/serialize/writer/mod.rs index 4545cdd2..2566f699 100644 --- a/openpgp/src/serialize/writer/mod.rs +++ b/openpgp/src/serialize/writer/mod.rs @@ -341,9 +341,17 @@ impl<'a, W: io::Write, C> Stackable<'a, C> for Generic<W, C> { fn mount(&mut self, _new: BoxStack<'a, C>) { } fn inner_mut(&mut self) -> Option<&mut Stackable<'a, C>> { + // If you use Generic to wrap an io::Writer, and you know that + // the io::Writer's inner is also a Stackable, then return a + // reference to the innermost Stackable in your + // implementation. See e.g. writer::ZLIB. None } fn inner_ref(&self) -> Option<&Stackable<'a, C>> { + // If you use Generic to wrap an io::Writer, and you know that + // the io::Writer's inner is also a Stackable, then return a + // reference to the innermost Stackable in your + // implementation. See e.g. writer::ZLIB. None } fn cookie_set(&mut self, cookie: C) -> C { @@ -410,10 +418,12 @@ impl<'a, C: 'a> Stackable<'a, C> for Encryptor<'a, C> { unreachable!("Only implemented by Signer") } fn inner_mut(&mut self) -> Option<&mut Stackable<'a, C>> { - self.inner.inner_mut() + // XXX: Unfortunately, this doesn't work due to a lifetime mismatch: + // self.inner.inner.get_mut().map(|r| r.as_mut()) + None } fn inner_ref(&self) -> Option<&Stackable<'a, C>> { - self.inner.inner_ref() + self.inner.inner.get_ref().map(|r| r.as_ref()) } fn cookie_set(&mut self, cookie: C) -> C { self.inner.cookie_set(cookie) @@ -481,10 +491,12 @@ impl<'a, C: 'a> Stackable<'a, C> for AEADEncryptor<'a, C> { unreachable!("Only implemented by Signer") } fn inner_mut(&mut self) -> Option<&mut Stackable<'a, C>> { - self.inner.inner_mut() + // XXX: Unfortunately, this doesn't work due to a lifetime mismatch: + // self.inner.inner.get_mut().map(|r| r.as_mut()) + None } fn inner_ref(&self) -> Option<&Stackable<'a, C>> { - self.inner.inner_ref() + self.inner.inner.get_ref().map(|r| r.as_ref()) } fn cookie_set(&mut self, cookie: C) -> C { self.inner.cookie_set(cookie) diff --git a/openpgp/src/serialize/writer/writer_bzip2.rs b/openpgp/src/serialize/writer/writer_bzip2.rs index b951b596..efcc5866 100644 --- a/openpgp/src/serialize/writer/writer_bzip2.rs +++ b/openpgp/src/serialize/writer/writer_bzip2.rs @@ -52,10 +52,10 @@ impl<'a, C: 'a> Stackable<'a, C> for BZ<'a, C> { unreachable!("Only implemented by Signer") } fn inner_mut(&mut self) -> Option<&mut Stackable<'a, C>> { - self.inner.inner_mut() + Some(self.inner.inner.get_mut()) } fn inner_ref(&self) -> Option<&Stackable<'a, C>> { - self.inner.inner_ref() + Some(self.inner.inner.get_ref()) } fn cookie_set(&mut self, cookie: C) -> C { self.inner.cookie_set(cookie) diff --git a/openpgp/src/serialize/writer/writer_deflate.rs b/openpgp/src/serialize/writer/writer_deflate.rs index 664b5319..90b19526 100644 --- a/openpgp/src/serialize/writer/writer_deflate.rs +++ b/openpgp/src/serialize/writer/writer_deflate.rs @@ -52,10 +52,10 @@ impl<'a, C: 'a> Stackable<'a, C> for ZIP<'a, C> { unreachable!("Only implemented by Signer") } fn inner_mut(&mut self) -> Option<&mut Stackable<'a, C>> { - self.inner.inner_mut() + Some(self.inner.inner.get_mut()) } fn inner_ref(&self) -> Option<&Stackable<'a, C>> { - self.inner.inner_ref() + Some(self.inner.inner.get_ref()) } fn cookie_set(&mut self, cookie: C) -> C { self.inner.cookie_set(cookie) @@ -117,10 +117,10 @@ impl<'a, C: 'a> Stackable<'a, C> for ZLIB<'a, C> { unreachable!("Only implemented by Signer") } fn inner_mut(&mut self) -> Option<&mut Stackable<'a, C>> { - self.inner.inner_mut() + Some(self.inner.inner.get_mut()) } fn inner_ref(&self) -> Option<&Stackable<'a, C>> { - self.inner.inner_ref() + Some(self.inner.inner.get_ref()) } fn cookie_set(&mut self, cookie: C) -> C { self.inner.cookie_set(cookie) |