summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/byte_encode.rs33
-rw-r--r--src/main.rs1
-rw-r--r--src/reader.rs30
-rw-r--r--src/writer.rs30
4 files changed, 42 insertions, 52 deletions
diff --git a/src/byte_encode.rs b/src/byte_encode.rs
new file mode 100644
index 0000000..4be57b2
--- /dev/null
+++ b/src/byte_encode.rs
@@ -0,0 +1,33 @@
+pub trait ByteEncode<const N: usize>: Sized + Copy
+{
+ fn from_le_bytes(buffer: &[u8; N]) -> Self;
+ fn to_le_bytes(&self) -> [u8; N];
+}
+
+impl ByteEncode<4> for u32
+{
+ fn from_le_bytes(buffer: &[u8; 4]) -> Self {
+ u32::from_le_bytes(*buffer)
+ }
+ fn to_le_bytes(&self) -> [u8; 4] {
+ u32::to_le_bytes(*self)
+ }
+}
+impl ByteEncode<2> for u16
+{
+ fn from_le_bytes(buffer: &[u8; 2]) -> Self {
+ u16::from_le_bytes(*buffer)
+ }
+ fn to_le_bytes(&self) -> [u8; 2] {
+ u16::to_le_bytes(*self)
+ }
+}
+impl ByteEncode<1> for u8
+{
+ fn from_le_bytes(buffer: &[u8; 1]) -> Self {
+ u8::from_le_bytes(*buffer)
+ }
+ fn to_le_bytes(&self) -> [u8; 1] {
+ u8::to_le_bytes(*self)
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 2d68a02..1e78abb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,6 +5,7 @@ mod reader;
mod image;
mod bmp;
mod writer;
+mod byte_encode;
fn main() {
let file = File::open("smiley.bmp").expect("File does not exist");
diff --git a/src/reader.rs b/src/reader.rs
index 00f66cb..6f18e0b 100644
--- a/src/reader.rs
+++ b/src/reader.rs
@@ -1,5 +1,7 @@
use std::{fs::File, io::{BufReader, Read}};
+use crate::byte_encode::ByteEncode;
+
pub struct FileReader
{
buf_reader: BufReader<File>
@@ -18,39 +20,15 @@ impl FileReader {
{
self.buf_reader.seek_relative(c as i64).expect("Cannot seek!");
}
- pub fn read_array<const N: usize, T: FromBytes<N> + Sized, const C: usize>(&mut self) -> [T; C]
+ pub fn read_array<const N: usize, T: ByteEncode<N> + Sized, const C: usize>(&mut self) -> [T; C]
{
std::array::from_fn(|_| self.read())
}
- pub fn read<const N: usize, T: FromBytes<N>>(&mut self) -> T
+ pub fn read<const N: usize, T: ByteEncode<N>>(&mut self) -> T
{
let mut data = [0u8; N];
self.buf_reader.read_exact(&mut data).expect("Bad read");
T::from_le_bytes(&data)
}
}
-
-pub trait FromBytes<const N: usize>: Sized + Copy
-{
- fn from_le_bytes(buffer: &[u8; N]) -> Self;
-}
-
-impl FromBytes<4> for u32
-{
- fn from_le_bytes(buffer: &[u8; 4]) -> Self {
- u32::from_le_bytes(*buffer)
- }
-}
-impl FromBytes<2> for u16
-{
- fn from_le_bytes(buffer: &[u8; 2]) -> Self {
- u16::from_le_bytes(*buffer)
- }
-}
-impl FromBytes<1> for u8
-{
- fn from_le_bytes(buffer: &[u8; 1]) -> Self {
- u8::from_le_bytes(*buffer)
- }
-}
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)
- }
-}