From 7be0d5039db7e8660bced13698178bf1d6758109 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 7 Dec 2022 18:40:24 +0100 Subject: refactor --- evc/src/format/ser.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'evc/src/format') 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(&mut self, value: V) -> anyhow::Result<()>; @@ -49,6 +49,24 @@ impl Ser for (A, B, C) { Ok((A::read(source)?, B::read(source)?, C::read(source)?)) } } +impl 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 { + Ok(( + A::read(source)?, + B::read(source)?, + C::read(source)?, + D::read(source)?, + )) + } +} impl 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 { +impl Ser for Vec2 { 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 { Ok(Vec2 { x, y }) } } +impl Ser for Mat2 { + 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 { + let (a, b, c, d) = source.get()?; + Ok(Mat2 { a, b, c, d }) + } +} pub struct Small(pub T); impl Ser for Small> { @@ -219,7 +246,6 @@ impl Ser for Small> { } } - #[cfg(test)] mod test { use super::{Ser, Sink}; -- cgit v1.2.3-70-g09d2