wolfram_serialize/writer.rs
1//! Minimal byte-level writer.
2//!
3//! [`Writer`] is the raw output abstraction: two methods, `write_byte` and
4//! `write_bytes`. A blanket impl covers every [`std::io::Write`] sink — so
5//! `Vec<u8>`, `File`, `TcpStream`, `ZlibEncoder`, `BufWriter`, etc. all work
6//! without any extra code. Streaming compression is therefore free: pass a
7//! `ZlibEncoder<Vec<u8>>` as the inner sink of a [`WxfWriter`] and the token
8//! stream is compressed on the fly, with no intermediate allocation.
9
10use crate::Error;
11
12/// Raw byte sink.
13pub trait Writer {
14 /// Append a single byte.
15 fn write_byte(&mut self, b: u8) -> Result<(), Error>;
16
17 /// Append a slice of bytes.
18 fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), Error>;
19}
20
21/// Blanket impl: every [`std::io::Write`] is a [`Writer`].
22///
23/// Covers `Vec<u8>`, `File`, `TcpStream`, `ZlibEncoder<W>`, `BufWriter<W>`,
24/// `&mut W` (via std's own `impl<W: Write> Write for &mut W`), etc.
25impl<W: std::io::Write> Writer for W {
26 fn write_byte(&mut self, b: u8) -> Result<(), Error> {
27 std::io::Write::write_all(self, &[b]).map_err(Error::from)
28 }
29
30 fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), Error> {
31 std::io::Write::write_all(self, bytes).map_err(Error::from)
32 }
33}