musli_binary_common/
io.rs

1//! Helpers for integrating musli with I/O types like [std::io] and
2//! [std::io::Write].
3//!
4//! The central function in this module is the [wrap] function which constructs
5//! an adapter around an I/O type to work with musli.
6
7/// Wrapper constructed with [wrap].
8pub struct Wrap<T> {
9    #[cfg_attr(not(feature = "std"), allow(unused))]
10    inner: T,
11}
12
13/// Wrap a type so that it implements [Reader] or [Writer] as appropriate.
14///
15/// [Reader]: crate::reader::Reader
16/// [Writer]: crate::writer::Writer
17pub fn wrap<T>(inner: T) -> Wrap<T> {
18    Wrap { inner }
19}
20
21#[cfg(feature = "std")]
22impl<W> crate::writer::Writer for Wrap<W>
23where
24    W: std::io::Write,
25{
26    type Error = std::io::Error;
27
28    fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
29        self.inner.write_all(bytes)
30    }
31}