mp4_atom/
io.rs

1use std::io::{Read, Write};
2
3use super::*;
4
5/// Read a type from a reader.
6pub trait ReadFrom: Sized {
7    fn read_from<R: Read>(r: &mut R) -> Result<Self>;
8}
9
10/// Read an atom from a reader provided the header.
11pub trait ReadAtom: Sized {
12    fn read_atom<R: Read>(header: &Header, r: &mut R) -> Result<Self>;
13}
14
15/// Keep discarding atoms until the desired atom is found.
16pub trait ReadUntil: Sized {
17    fn read_until<R: Read>(r: &mut R) -> Result<Self>;
18}
19
20/// Write a type to a writer.
21pub trait WriteTo {
22    fn write_to<W: Write>(&self, w: &mut W) -> Result<()>;
23}
24
25impl<T: Encode> WriteTo for T {
26    fn write_to<W: Write>(&self, w: &mut W) -> Result<()> {
27        // TODO We should avoid allocating a buffer here.
28        let mut buf = Vec::new();
29        self.encode(&mut buf)?;
30        Ok(w.write_all(&buf)?)
31    }
32}