use super::to_io_error; /// Adapter from `std::io` traits. #[cfg_attr(docsrs, doc(cfg(feature = "std")))] pub struct FromStd { inner: T, } impl FromStd { /// Create a new adapter. pub fn new(inner: T) -> Self { Self { inner } } /// Consume the adapter, returning the inner object. pub fn into_inner(self) -> T { self.inner } } impl FromStd { /// Borrow the inner object. pub fn inner(&self) -> &T { &self.inner } /// Mutably borrow the inner object. pub fn inner_mut(&mut self) -> &mut T { &mut self.inner } } impl crate::Io for FromStd { type Error = std::io::Error; } impl crate::blocking::Read for FromStd { fn read(&mut self, buf: &mut [u8]) -> Result { self.inner.read(buf) } } impl crate::blocking::Write for FromStd { fn write(&mut self, buf: &[u8]) -> Result { self.inner.write(buf) } fn flush(&mut self) -> Result<(), Self::Error> { self.inner.flush() } } impl crate::blocking::Seek for FromStd { fn seek(&mut self, pos: crate::SeekFrom) -> Result { self.inner.seek(pos.into()) } } /// Adapter to `std::io` traits. #[cfg_attr(docsrs, doc(cfg(feature = "std")))] pub struct ToStd { inner: T, } impl ToStd { /// Create a new adapter. pub fn new(inner: T) -> Self { Self { inner } } /// Consume the adapter, returning the inner object. pub fn into_inner(self) -> T { self.inner } } impl ToStd { /// Borrow the inner object. pub fn inner(&self) -> &T { &self.inner } /// Mutably borrow the inner object. pub fn inner_mut(&mut self) -> &mut T { &mut self.inner } } impl std::io::Read for ToStd { fn read(&mut self, buf: &mut [u8]) -> Result { self.inner.read(buf).map_err(to_io_error) } } impl std::io::Write for ToStd { fn write(&mut self, buf: &[u8]) -> Result { self.inner.write(buf).map_err(to_io_error) } fn flush(&mut self) -> Result<(), std::io::Error> { self.inner.flush().map_err(to_io_error) } } impl std::io::Seek for ToStd { fn seek(&mut self, pos: std::io::SeekFrom) -> Result { self.inner.seek(pos.into()).map_err(to_io_error) } }