Compilation help

Here is the gist of the code I am trying to write:

use std::io::{Read, Write, Error};

trait XFlatFile {
    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(*self)

    }

    fn read(reader: &mut dyn Read) -> Result<i32, std::io::Error> {
        reader.read_i32()
    }
}

Would appreciate pointers on how to make this compile.

How about this?
playground

use std::io::{Read, Write};
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>()
    }
}
1 Like