[−][src]Trait read_from::WriteTo
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]);
Associated Types
type Error
What error can happen when trying to write?
Required methods
fn write_to<W: Write>(&self, output: W) -> Result<usize, Self::Error>
Attempts to write self to the given output stream, returning the number
of bytes written on success.