aboutsummaryrefslogtreecommitdiff
path: root/evc/src/format
diff options
context:
space:
mode:
Diffstat (limited to 'evc/src/format')
-rw-r--r--evc/src/format/ser.rs34
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};