diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-10-19 13:16:40 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-10-19 13:16:40 +0200 |
commit | c5d1b7e3847d56b33b8e738c8cff4c8e2a24527c (patch) | |
tree | 981ef79b4dd895a9f5d03326af02d9d2ab0d916b | |
parent | 7f7c192de8a94064bfab07a41f50b1588fbcee54 (diff) |
buffered-reader: New buffered reader 'Adapter'.
-rw-r--r-- | buffered-reader/src/adapter.rs | 111 | ||||
-rw-r--r-- | buffered-reader/src/lib.rs | 2 |
2 files changed, 113 insertions, 0 deletions
diff --git a/buffered-reader/src/adapter.rs b/buffered-reader/src/adapter.rs new file mode 100644 index 00000000..6dc081e0 --- /dev/null +++ b/buffered-reader/src/adapter.rs @@ -0,0 +1,111 @@ +use std::io; + +use super::*; + +/// Changes the cookie type without introducing any buffering. +/// +/// If you have a `b: BufferedReader<B>` but need a `c: +/// BufferedReader<C>`, then one way to do that is to use `let c = +/// Generic::with_cookie(b, _)`, but that introduces buffering. This +/// `Adapter` also changes cookie types, but does no buffering of its +/// own. +pub struct Adapter<T: BufferedReader<B>, B, C> { + reader: T, + _ghostly_cookie: std::marker::PhantomData<B>, + cookie: C, +} + +impl<T: BufferedReader<B>, B, C> fmt::Display for Adapter<T, B, C> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Adapter") + } +} + +impl<T: BufferedReader<B>, B, C> fmt::Debug for Adapter<T, B, C> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("Adapter") + .field("reader", &self.reader) + .finish() + } +} + +impl<T: BufferedReader<B>, B> Adapter<T, B, ()> { + /// Instantiates a new adapter. + /// + /// `reader` is the source to wrap. + pub fn new(reader: T) -> Self { + Self::with_cookie(reader, ()) + } +} + +impl<T: BufferedReader<B>, B, C> Adapter<T, B, C> { + /// Like `new()`, but sets a cookie. + /// + /// The cookie can be retrieved using the `cookie_ref` and + /// `cookie_mut` methods, and set using the `cookie_set` method. + pub fn with_cookie(reader: T, cookie: C) + -> Adapter<T, B, C> { + Adapter { + reader, + _ghostly_cookie: Default::default(), + cookie, + } + } +} + +impl<T: BufferedReader<B>, B, C> io::Read for Adapter<T, B, C> { + fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { + self.reader.read(buf) + } +} + +impl<T: BufferedReader<B>, B, C> BufferedReader<C> for Adapter<T, B, C> { + fn buffer(&self) -> &[u8] { + self.reader.buffer() + } + + fn data(&mut self, amount: usize) -> Result<&[u8], io::Error> { + self.reader.data(amount) + } + + fn consume(&mut self, amount: usize) -> &[u8] { + self.reader.consume(amount) + } + + fn data_consume(&mut self, amount: usize) -> Result<&[u8], io::Error> { + self.reader.data_consume(amount) + } + + fn data_consume_hard(&mut self, amount: usize) -> Result<&[u8], io::Error> { + self.reader.data_consume_hard(amount) + } + + fn consummated(&mut self) -> bool { + self.reader.consummated() + } + + fn get_mut(&mut self) -> Option<&mut dyn BufferedReader<C>> { + None + } + + fn get_ref(&self) -> Option<&dyn BufferedReader<C>> { + None + } + + fn into_inner<'b>(self: Box<Self>) -> Option<Box<dyn BufferedReader<C> + 'b>> + where Self: 'b { + None + } + + fn cookie_set(&mut self, cookie: C) -> C { + std::mem::replace(&mut self.cookie, cookie) + } + + fn cookie_ref(&self) -> &C { + &self.cookie + } + + fn cookie_mut(&mut self) -> &mut C { + &mut self.cookie + } +} diff --git a/buffered-reader/src/lib.rs b/buffered-reader/src/lib.rs index 1b08e73c..c508b2b7 100644 --- a/buffered-reader/src/lib.rs +++ b/buffered-reader/src/lib.rs @@ -235,6 +235,7 @@ mod limitor; mod reserve; mod dup; mod eof; +mod adapter; #[cfg(feature = "compression-deflate")] mod decompress_deflate; #[cfg(feature = "compression-bzip2")] @@ -246,6 +247,7 @@ pub use self::limitor::Limitor; pub use self::reserve::Reserve; pub use self::dup::Dup; pub use self::eof::EOF; +pub use self::adapter::Adapter; #[cfg(feature = "compression-deflate")] pub use self::decompress_deflate::Deflate; #[cfg(feature = "compression-deflate")] |