summaryrefslogtreecommitdiff
path: root/src/writer.rs
blob: 47d400bb0954686a0c45d7dcf9c3a2fb2aa432f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::io::Write;
use std::{fs::File, io::BufWriter};

use crate::byte_encode::ByteEncode;

pub struct FileWriter
{
	buf_writer: BufWriter<File>
}

impl FileWriter
{
	pub fn new(buf_writer: BufWriter<File>) -> Self
	{
		Self
		{
			buf_writer
		}
	}

	pub fn write_zeros(&mut self, c: usize)
	{
		for _ in 0..c
		{
			self.write(0 as u8);
		}
		// self.buf_writer.write_all(&[0u8; c]).expect("Could not write 0s");
	}

	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: ByteEncode<N>>(&mut self, x: T)
	{
		let data = x.to_le_bytes();
		self.buf_writer.write_all(&data).expect("Could not write");
	}
	
	pub fn flush(&mut self)
	{
		self.buf_writer.flush().expect("Could not flush");
	}
}