pub trait WriteTo {
type Error;
// Required method
fn write_to<W: Write>(&self, output: W) -> Result<usize, Self::Error>;
}Expand description
Used to serialize types into an output stream (i.e. a Write
implementation).
§Example
This implements WriteTo for a simple TinyRational struct.
use read_from::WriteTo;
use std::io::{self, Write};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TinyRational {
numer: i8,
denom: u8
}
impl WriteTo for TinyRational {
type Error = io::Error;
fn write_to<W: Write>(&self, mut out: W) -> Result<usize, Self::Error> {
out.write_all(&[self.numer as u8, self.denom]).and(Ok(2))
}
}
let mut buf = Vec::new();
TinyRational { numer: 3, denom: 4 }.write_to(&mut buf).unwrap();
assert_eq!(buf, &[3, 4]);Required Associated Types§
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".