use std::io::{Read, Write, Error};
use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian};
trait XFlatFile: Sized {
fn write(&self, writer: &mut dyn std::io::Write) -> Result<(), std::io::Error>;
fn read(reader: &mut dyn std::io::Read) -> Result<Self, std::io::Error>;
}
impl XFlatFile for i32 {
fn write(&self, writer: &mut dyn Write) -> Result<(), std::io::Error> {
writer.write_i32::<LittleEndian>(*self)
}
fn read(reader: &mut dyn Read) -> Result<i32, std::io::Error> {
reader.read_i32::<LittleEndian>()
}
}
impl <A: XFlatFile, B: XFlatFile> XFlatFile for (A, B) {
fn write(&self, writer: &mut dyn Write) -> Result<(), Error> {
self.0.write(writer)?;
self.1.write(writer)?;
Ok(())
}
fn read(reader: &mut dyn Read) -> Result<Self, Error> {
let a = reader.read::<A>()?;
let b = reader.read::<B>()?;
Ok((a, b))
}
}
the part I am stuck on is the reader.read::<A>()
part. So I need a way to somehow signal that we are reading an A
first then a B
afterwards.