summaryrefslogtreecommitdiff
path: root/src/sstream/mod.rs
blob: 83536d052eb844abd503f0367e1df2dd818bfb11 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2024 Gabriel Bjørnager Jensen.
//
// This file is part of bzipper.
//
// bzipper is free software: you can redistribute
// it and/or modify it under the terms of the GNU
// Lesser General Public License as published by
// the Free Software Foundation, either version 3
// of the License, or (at your option) any later
// version.
//
// bzipper is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Less-
// er General Public License along with bzipper. If
// not, see <https://www.gnu.org/licenses/>.

use crate::{Error, Result};

use core::fmt::{Debug, Formatter};

/// Byte stream for serialisation.
///
/// This type borrows a byte slice (hence [`new`](Sstream::new)), keeping track internally of the used bytes.
#[derive(Eq, PartialEq)]
pub struct Sstream<'a> {
	data: &'a mut [u8],
	len:  usize
}

impl<'a> Sstream<'a> {
	/// Constructs a new byte stream.
	///
	/// If the borrowed slice already contains data, this may overwritten by subsequent serialisations.
	#[inline(always)]
	#[must_use]
	pub fn new(data: &'a mut [u8]) -> Self { Self { data, len: 0x0 } }

	/// Extends the byte stream.
	///
	/// # Errors
	///
	/// If the stream cannot hold the requested bytes, an [`EndOfStream`](Error::EndOfStream) instance is returned.
	pub fn add(&mut self, extra: &[u8]) -> Result<usize> {
		let rem = self.data.len() - self.len;
		let req = extra.len();

		if rem.checked_sub(req).is_none() {
			return Err(Error::EndOfStream { req, rem });
		}

		let start = self.len;
		let stop  = self.len + req;

		self.len += req;
		self.data[start..stop].copy_from_slice(extra);

		Ok(req)
	}

	/// Extends the byte stream by a single byte.
	///
	/// # Errors
	///
	/// If the stream cannot hold the byte, an [`EndOfStream`](Error::EndOfStream) instance is returned.
	pub fn add_byte(&mut self, extra: u8) -> Result<usize> {
		self.add(&[extra])
	}

	/// Yields the length of the stream.
	///
	/// That is, the amount of bytes written so far.
	#[inline(always)]
	#[must_use]
	pub const fn len(&self) -> usize { self.len }

	/// Tests if the stream is empty.
	#[inline(always)]
	#[must_use]
	pub const fn is_empty(&self) -> bool { self.len == 0x0 }

	/// Returns a slice to the stream contents.
	///
	/// This includes all previously written bytes.
	#[inline(always)]
	#[must_use]
	pub fn as_slice(&self) -> &[u8] { &self.data[0x0..self.len] }
}

impl AsRef<[u8]> for Sstream<'_> {
	#[inline(always)]
	fn as_ref(&self) -> &[u8] { self.as_slice() }
}

impl Debug for Sstream<'_> {
	fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
		self.data.fmt(f)
	}
}