use std::io; use super::*; /// Changes the cookie type without introducing any buffering. /// /// If you have a `b: BufferedReader` but need a `c: /// BufferedReader`, 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. #[derive(Debug)] pub struct Adapter, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> { _ghostly_cookie: std::marker::PhantomData, cookie: C, reader: T, } assert_send_and_sync!(Adapter where T: BufferedReader, B: fmt::Debug, C: fmt::Debug); impl, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> fmt::Display for Adapter { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Adapter").finish() } } impl, B: fmt::Debug + Sync + Send> Adapter { /// Instantiates a new adapter. /// /// `reader` is the source to wrap. pub fn new(reader: T) -> Self { Self::with_cookie(reader, ()) } } impl, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> Adapter { /// 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 { Adapter { reader, _ghostly_cookie: Default::default(), cookie, } } } impl, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> io::Read for Adapter { fn read(&mut self, buf: &mut [u8]) -> Result { self.reader.read(buf) } } impl, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> BufferedReader for Adapter { 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> { None } fn get_ref(&self) -> Option<&dyn BufferedReader> { None } fn into_inner<'b>(self: Box) -> Option + '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 } }