diff options
| author | Dylan <boss@tehbox.org> | 2026-06-21 15:27:50 +1200 |
|---|---|---|
| committer | Dylan <boss@tehbox.org> | 2026-06-21 15:27:50 +1200 |
| commit | db14bdce8eac0d7d83109365cbeda389febfb788 (patch) | |
| tree | 755a7d22efde8f283dc73b5b943d5c37f4caf158 /src/writer.rs | |
| parent | a472b1cdaa8a1f72b96988009b3d4d9820e7128f (diff) | |
| download | tehimage-rs-db14bdce8eac0d7d83109365cbeda389febfb788.tar.gz tehimage-rs-db14bdce8eac0d7d83109365cbeda389febfb788.zip | |
feat: Moved from and to bytes to a single trait
Now called `ByteEncode`
Diffstat (limited to 'src/writer.rs')
| -rw-r--r-- | src/writer.rs | 30 |
1 files changed, 4 insertions, 26 deletions
diff --git a/src/writer.rs b/src/writer.rs index dc1ac85..47d400b 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -1,6 +1,8 @@ use std::io::Write; use std::{fs::File, io::BufWriter}; +use crate::byte_encode::ByteEncode; + pub struct FileWriter { buf_writer: BufWriter<File> @@ -25,14 +27,14 @@ impl FileWriter // self.buf_writer.write_all(&[0u8; c]).expect("Could not write 0s"); } - pub fn write_array<const N: usize, T: ToBytes<N>, const C: usize>(&mut self, xs: [T; C]) + pub fn write_array<const N: usize, T: ByteEncode<N>, const C: usize>(&mut self, xs: [T; C]) { for x in xs { self.write(x); } } - pub fn write<const N: usize, T: ToBytes<N>>(&mut self, x: T) + pub fn write<const N: usize, T: ByteEncode<N>>(&mut self, x: T) { let data = x.to_le_bytes(); self.buf_writer.write_all(&data).expect("Could not write"); @@ -43,27 +45,3 @@ impl FileWriter self.buf_writer.flush().expect("Could not flush"); } } - -pub trait ToBytes<const N: usize>: Sized + Copy -{ - fn to_le_bytes(&self) -> [u8; N]; -} - -impl ToBytes<4> for u32 -{ - fn to_le_bytes(&self) -> [u8; 4] { - u32::to_le_bytes(*self) - } -} -impl ToBytes<2> for u16 -{ - fn to_le_bytes(&self) -> [u8; 2] { - u16::to_le_bytes(*self) - } -} -impl ToBytes<1> for u8 -{ - fn to_le_bytes(&self) -> [u8; 1] { - u8::to_le_bytes(*self) - } -} |
