diff options
author | metamuffin <metamuffin@disroot.org> | 2022-12-07 18:40:24 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-12-07 18:40:24 +0100 |
commit | 7be0d5039db7e8660bced13698178bf1d6758109 (patch) | |
tree | a8723d67b6bab98656b182478b6418f2361260fa /evc/src/format/ser.rs | |
parent | 8ca219c6b0d5448fd4529713ccd093e89de4e252 (diff) | |
download | video-codec-experiments-7be0d5039db7e8660bced13698178bf1d6758109.tar video-codec-experiments-7be0d5039db7e8660bced13698178bf1d6758109.tar.bz2 video-codec-experiments-7be0d5039db7e8660bced13698178bf1d6758109.tar.zst |
refactor
Diffstat (limited to 'evc/src/format/ser.rs')
-rw-r--r-- | evc/src/format/ser.rs | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/evc/src/format/ser.rs b/evc/src/format/ser.rs index 65d5e26..731a7a3 100644 --- a/evc/src/format/ser.rs +++ b/evc/src/format/ser.rs @@ -1,7 +1,7 @@ use anyhow::Context; use std::io::{Read, Write}; -use crate::helpers::vector::Vec2; +use crate::helpers::{matrix::Mat2, vector::Vec2}; pub trait Sink { fn put<V: Ser>(&mut self, value: V) -> anyhow::Result<()>; @@ -49,6 +49,24 @@ impl<A: Ser, B: Ser, C: Ser> Ser for (A, B, C) { Ok((A::read(source)?, B::read(source)?, C::read(source)?)) } } +impl<A: Ser, B: Ser, C: Ser, D: Ser> Ser for (A, B, C, D) { + 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")?; + self.3.write(sink).context("fourth tuple field")?; + Ok(()) + } + + fn read(source: &mut impl Read) -> anyhow::Result<Self> { + Ok(( + A::read(source)?, + B::read(source)?, + C::read(source)?, + D::read(source)?, + )) + } +} impl<A: Ser, const N: usize> Ser for [A; N] { fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { @@ -192,8 +210,7 @@ impl Ser for f64 { } } - -impl Ser for Vec2<isize> { +impl<T: Ser + Copy> Ser for Vec2<T> { fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { sink.put((self.x, self.y)) } @@ -203,6 +220,16 @@ impl Ser for Vec2<isize> { Ok(Vec2 { x, y }) } } +impl<T: Ser + Copy> Ser for Mat2<T> { + fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { + sink.put((self.a, self.b, self.c, self.d)) + } + + fn read(source: &mut impl std::io::Read) -> anyhow::Result<Self> { + let (a, b, c, d) = source.get()?; + Ok(Mat2 { a, b, c, d }) + } +} pub struct Small<T>(pub T); impl Ser for Small<Vec2<isize>> { @@ -219,7 +246,6 @@ impl Ser for Small<Vec2<isize>> { } } - #[cfg(test)] mod test { use super::{Ser, Sink}; |