diff options
author | metamuffin <metamuffin@disroot.org> | 2022-12-06 08:16:09 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-12-06 08:16:09 +0100 |
commit | 5002d0df81f74418665e4e99179ba56d8e78cbe1 (patch) | |
tree | 2ece906f886934dd0fc335a4aa31bc5363fa92e8 /evc/src/ser.rs | |
parent | bafb1df8b7764a0a62f1c656eb52fbe7bfd8b8ac (diff) | |
download | video-codec-experiments-5002d0df81f74418665e4e99179ba56d8e78cbe1.tar video-codec-experiments-5002d0df81f74418665e4e99179ba56d8e78cbe1.tar.bz2 video-codec-experiments-5002d0df81f74418665e4e99179ba56d8e78cbe1.tar.zst |
more codde
Diffstat (limited to 'evc/src/ser.rs')
-rw-r--r-- | evc/src/ser.rs | 116 |
1 files changed, 72 insertions, 44 deletions
diff --git a/evc/src/ser.rs b/evc/src/ser.rs index 2d0bc82..6e1ef98 100644 --- a/evc/src/ser.rs +++ b/evc/src/ser.rs @@ -1,61 +1,62 @@ -use std::io::{self, Read, Write}; +use anyhow::Context; +use std::io::{Read, Write}; pub trait Sink { - fn put<V: Ser>(&mut self, value: V) -> io::Result<()>; + fn put<V: Ser>(&mut self, value: V) -> anyhow::Result<()>; } pub trait Source { - fn get<V: Ser>(&mut self) -> io::Result<V>; + fn get<V: Ser>(&mut self) -> anyhow::Result<V>; } impl<T: Write> Sink for T { - fn put<V: Ser>(&mut self, value: V) -> io::Result<()> { + fn put<V: Ser>(&mut self, value: V) -> anyhow::Result<()> { value.write(self) } } impl<T: Read> Source for T { - fn get<V: Ser>(&mut self) -> io::Result<V> { + fn get<V: Ser>(&mut self) -> anyhow::Result<V> { V::read(self) } } pub trait Ser: Sized { - fn write(&self, sink: &mut impl Write) -> io::Result<()>; - fn read(source: &mut impl Read) -> io::Result<Self>; + fn write(&self, sink: &mut impl Write) -> anyhow::Result<()>; + fn read(source: &mut impl Read) -> anyhow::Result<Self>; } impl<A: Ser, B: Ser> Ser for (A, B) { - fn write(&self, sink: &mut impl Write) -> io::Result<()> { - self.0.write(sink)?; - self.1.write(sink)?; + fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { + self.0.write(sink).context("first tuple field")?; + self.1.write(sink).context("second tuple field")?; Ok(()) } - fn read(source: &mut impl Read) -> io::Result<Self> { + fn read(source: &mut impl Read) -> anyhow::Result<Self> { Ok((A::read(source)?, B::read(source)?)) } } impl<A: Ser, B: Ser, C: Ser> Ser for (A, B, C) { - fn write(&self, sink: &mut impl Write) -> io::Result<()> { - self.0.write(sink)?; - self.1.write(sink)?; - self.2.write(sink)?; + fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { + self.0.write(sink).context("first tuple field")?; + self.1.write(sink).context("second tuple field")?; + self.2.write(sink).context("third tuple field")?; Ok(()) } - fn read(source: &mut impl Read) -> io::Result<Self> { + fn read(source: &mut impl Read) -> anyhow::Result<Self> { Ok((A::read(source)?, B::read(source)?, C::read(source)?)) } } impl<A: Ser, const N: usize> Ser for [A; N] { - fn write(&self, sink: &mut impl Write) -> io::Result<()> { + fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { for e in self { - e.write(sink)?; + e.write(sink).context("some array")?; } Ok(()) } - fn read(source: &mut impl Read) -> io::Result<Self> { + fn read(source: &mut impl Read) -> anyhow::Result<Self> { let mut k: [A; N] = unsafe { std::mem::zeroed() }; for i in 0..N { k[i] = A::read(source)?; @@ -65,15 +66,15 @@ impl<A: Ser, const N: usize> Ser for [A; N] { } impl<T: Ser> Ser for Vec<T> { - fn write(&self, sink: &mut impl Write) -> io::Result<()> { + fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { self.len().write(sink)?; for e in self { - e.write(sink)?; + e.write(sink).context("some vec")?; } Ok(()) } - fn read(source: &mut impl Read) -> io::Result<Self> { + fn read(source: &mut impl Read) -> anyhow::Result<Self> { let mut v = vec![]; for _ in 0..usize::read(source)? { v.push(T::read(source)?) @@ -83,70 +84,82 @@ impl<T: Ser> Ser for Vec<T> { } impl Ser for u8 { - fn write(&self, sink: &mut impl Write) -> io::Result<()> { - sink.write_all(&[*self]) + fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { + Ok(sink.write_all(&[*self]).context("write u8")?) } - fn read(source: &mut impl Read) -> io::Result<Self> { + fn read(source: &mut impl Read) -> anyhow::Result<Self> { let mut buf = [0u8; 1]; source.read_exact(&mut buf)?; Ok(buf[0]) } } impl Ser for u16 { - fn write(&self, sink: &mut impl Write) -> io::Result<()> { - sink.write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 2]>(self) }) + fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { + Ok(sink + .write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 2]>(self) }) + .context("write 16")?) } - fn read(source: &mut impl Read) -> io::Result<Self> { + fn read(source: &mut impl Read) -> anyhow::Result<Self> { let mut buf = [0u8; 2]; source.read_exact(&mut buf)?; Ok(unsafe { std::mem::transmute_copy(&buf) }) } } impl Ser for u32 { - fn write(&self, sink: &mut impl Write) -> io::Result<()> { - sink.write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 4]>(self) }) + fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { + Ok(sink + .write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 4]>(self) }) + .context("write u32")?) } - fn read(source: &mut impl Read) -> io::Result<Self> { + fn read(source: &mut impl Read) -> anyhow::Result<Self> { let mut buf = [0u8; 4]; source.read_exact(&mut buf)?; Ok(unsafe { std::mem::transmute_copy(&buf) }) } } impl Ser for u64 { - fn write(&self, sink: &mut impl Write) -> io::Result<()> { - sink.write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 8]>(self) }) + fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { + Ok(sink + .write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 8]>(self) }) + .context("write u64")?) } - fn read(source: &mut impl Read) -> io::Result<Self> { + fn read(source: &mut impl Read) -> anyhow::Result<Self> { let mut buf = [0u8; 8]; source.read_exact(&mut buf)?; Ok(unsafe { std::mem::transmute_copy(&buf) }) } } impl Ser for usize { - fn write(&self, sink: &mut impl Write) -> io::Result<()> { - sink.write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 8]>(self) }) + fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { + Ok(sink + .write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 8]>(self) }) + .context("write usize")?) } - fn read(source: &mut impl Read) -> io::Result<Self> { + fn read(source: &mut impl Read) -> anyhow::Result<Self> { let mut buf = [0u8; 8]; source.read_exact(&mut buf)?; Ok(unsafe { std::mem::transmute_copy(&buf) }) } } impl Ser for f32 { - fn write(&self, sink: &mut impl Write) -> io::Result<()> { - sink.write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 4]>(self) }) + fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { + Ok(sink + .write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 4]>(self) }) + .context("write f32")?) } - fn read(source: &mut impl Read) -> io::Result<Self> { + fn read(source: &mut impl Read) -> anyhow::Result<Self> { let mut buf = [0u8; 4]; source.read_exact(&mut buf)?; Ok(unsafe { std::mem::transmute_copy(&buf) }) } } impl Ser for f64 { - fn write(&self, sink: &mut impl Write) -> io::Result<()> { - sink.write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 8]>(self) }) + fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { + Ok(sink + .write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 8]>(self) }) + .context("write f64")?) } - fn read(source: &mut impl Read) -> io::Result<Self> { + fn read(source: &mut impl Read) -> anyhow::Result<Self> { let mut buf = [0u8; 8]; source.read_exact(&mut buf)?; Ok(unsafe { std::mem::transmute_copy(&buf) }) @@ -155,10 +168,18 @@ impl Ser for f64 { #[cfg(test)] mod test { - use super::Sink; + use super::{Ser, Sink}; + use crate::header::Header; use crate::ser::Source; + use std::fmt::Debug; use std::io::Cursor; + fn test_ser<T: PartialEq + Ser + Debug + Copy>(value: T) { + let mut buf = vec![]; + Cursor::new(&mut buf).put(value).unwrap(); + assert_eq!(value, Cursor::new(&mut buf).get().unwrap()); + } + #[test] fn simple() { let mut buf = vec![]; @@ -171,4 +192,11 @@ mod test { Cursor::new(&mut buf).put((10usize, 5u8, 3u16)).unwrap(); assert_eq!((10usize, 5u8, 3u16), Cursor::new(&mut buf).get().unwrap()); } + #[test] + fn header() { + test_ser(Header { + frame_count: 123, + resolution: (13, 37), + }); + } } |