use anyhow::Context; use std::io::{Read, Write}; pub trait Sink { fn put(&mut self, value: V) -> anyhow::Result<()>; } pub trait Source { fn get(&mut self) -> anyhow::Result; } impl Sink for T { fn put(&mut self, value: V) -> anyhow::Result<()> { value.write(self) } } impl Source for T { fn get(&mut self) -> anyhow::Result { V::read(self) } } pub trait Ser: Sized { fn write(&self, sink: &mut impl Write) -> anyhow::Result<()>; fn read(source: &mut impl Read) -> anyhow::Result; } impl Ser for (A, B) { 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) -> anyhow::Result { Ok((A::read(source)?, B::read(source)?)) } } impl Ser for (A, B, C) { 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) -> anyhow::Result { Ok((A::read(source)?, B::read(source)?, C::read(source)?)) } } impl Ser for [A; N] { fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { for e in self { e.write(sink).context("some array")?; } Ok(()) } fn read(source: &mut impl Read) -> anyhow::Result { let mut k: [A; N] = unsafe { std::mem::zeroed() }; for i in 0..N { k[i] = A::read(source)?; } Ok(k) } } impl Ser for Vec { fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { self.len().write(sink)?; for e in self { e.write(sink).context("some vec")?; } Ok(()) } fn read(source: &mut impl Read) -> anyhow::Result { let mut v = vec![]; for _ in 0..usize::read(source)? { v.push(T::read(source)?) } Ok(v) } } impl Ser for u8 { fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { Ok(sink.write_all(&[*self]).context("write u8")?) } fn read(source: &mut impl Read) -> anyhow::Result { let mut buf = [0u8; 1]; source.read_exact(&mut buf)?; Ok(buf[0]) } } impl Ser for u16 { 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) -> anyhow::Result { 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) -> anyhow::Result<()> { Ok(sink .write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 4]>(self) }) .context("write u32")?) } fn read(source: &mut impl Read) -> anyhow::Result { 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) -> anyhow::Result<()> { Ok(sink .write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 8]>(self) }) .context("write u64")?) } fn read(source: &mut impl Read) -> anyhow::Result { 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) -> anyhow::Result<()> { Ok(sink .write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 8]>(self) }) .context("write usize")?) } fn read(source: &mut impl Read) -> anyhow::Result { let mut buf = [0u8; 8]; source.read_exact(&mut buf)?; Ok(unsafe { std::mem::transmute_copy(&buf) }) } } impl Ser for isize { fn write(&self, sink: &mut impl Write) -> anyhow::Result<()> { Ok(sink .write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 8]>(self) }) .context("write isize")?) } fn read(source: &mut impl Read) -> anyhow::Result { 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) -> anyhow::Result<()> { Ok(sink .write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 4]>(self) }) .context("write f32")?) } fn read(source: &mut impl Read) -> anyhow::Result { 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) -> anyhow::Result<()> { Ok(sink .write_all(&unsafe { std::mem::transmute_copy::<_, [u8; 8]>(self) }) .context("write f64")?) } fn read(source: &mut impl Read) -> anyhow::Result { let mut buf = [0u8; 8]; source.read_exact(&mut buf)?; Ok(unsafe { std::mem::transmute_copy(&buf) }) } } #[cfg(test)] mod test { use super::{Ser, Sink}; use crate::header::Header; use crate::ser::Source; use crate::vec2::Vec2; use std::fmt::Debug; use std::io::Cursor; fn test_ser(value: T) { let mut buf = vec![]; Cursor::new(&mut buf).put(value.clone()).unwrap(); assert_eq!(value, Cursor::new(&mut buf).get().unwrap()); } #[test] fn simple() { let mut buf = vec![]; Cursor::new(&mut buf).put(10usize).unwrap(); assert_eq!(10usize, Cursor::new(&mut buf).get().unwrap()); } #[test] fn tuple() { let mut buf = vec![]; 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: Vec2 { x: 13, y: 37 }, }); } #[test] fn vec() { test_ser(vec![1u16, 2, 3, 4]); } #[test] fn array() { test_ser([1u16, 2, 3, 4]); } }