diff options
Diffstat (limited to 'bzipper/src/buffer')
-rw-r--r-- | bzipper/src/buffer/mod.rs | 126 | ||||
-rw-r--r-- | bzipper/src/buffer/test.rs | 36 |
2 files changed, 162 insertions, 0 deletions
diff --git a/bzipper/src/buffer/mod.rs b/bzipper/src/buffer/mod.rs new file mode 100644 index 0000000..ad2e743 --- /dev/null +++ b/bzipper/src/buffer/mod.rs @@ -0,0 +1,126 @@ +// 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/>. + +#[cfg(test)] +mod test; + +use crate::{Deserialise, Result, Serialise}; + +use alloc::vec; +use alloc::boxed::Box; +use core::fmt::{Debug, Formatter}; +use core::marker::PhantomData; +use core::ops::{Deref, DerefMut}; + +/// Typed (de)serialisation buffer. +/// +/// This structure is intended as a lightweight wrapper around byte buffers for specific (de)serialisations of specific types. +/// +/// The methods [`write`](Self::write) and [`read`](Self::read) can be used to <interpreting> the internal buffer. +/// Other methods exist for accessing the internal buffer directly. +#[cfg_attr(doc, doc(cfg(feature = "alloc")))] +#[derive(Clone, Eq, PartialEq)] +pub struct Buffer<T: Serialise> { + buf: Box<[u8]>, + + _phanton: PhantomData<T> +} + +impl<T: Serialise> Buffer<T> { + /// Allocates a new buffer suitable for (de)serialisation. + #[must_use] + pub fn new() -> Self { Self { buf: vec![0x00; T::SERIALISED_SIZE].into(), _phanton: PhantomData } } + + /// Serialises into the contained buffer. + #[inline(always)] + pub fn write(&mut self, value: &T) -> Result<()> { value.serialise(&mut self.buf) } + + /// Retrieves a pointer to the first byte. + #[inline(always)] + #[must_use] + pub const fn as_ptr(&self) -> *const u8 { self.buf.as_ptr() } + + /// Retrieves a mutable pointer to the first byte. + #[inline(always)] + #[must_use] + pub fn as_mut_ptr(&mut self) -> *mut u8 { self.buf.as_mut_ptr() } + + /// Gets a slice of the intenral buffer. + #[inline(always)] + #[must_use] + pub const fn as_slice(&self) -> &[u8] { unsafe { core::slice::from_raw_parts(self.as_ptr(), self.len()) } } + + /// Gets a mutable slice of the intenral buffer. + #[inline(always)] + #[must_use] + pub fn as_mut_slice(&mut self) -> &mut [u8] { &mut self.buf } + + /// Gets the length of the buffer. + /// + /// This is defined as (and therefore always equal to) the value of [SERIALISED_SIZE](Serialise::SERIALISED_SIZE) as specified by `T`. + #[allow(clippy::len_without_is_empty)] + #[inline(always)] + #[must_use] + pub const fn len(&self) -> usize { T::SERIALISED_SIZE } +} + +impl<T: Deserialise> Buffer<T> { + /// Deserialises from the contained buffer. + #[inline(always)] + pub fn read(&self) -> Result<T> { T::deserialise(&self.buf) } +} + +impl<T: Serialise> AsMut<[u8]> for Buffer<T> { + #[inline(always)] + fn as_mut(&mut self) -> &mut [u8] { self.as_mut_slice() } +} + +impl<T: Serialise> AsRef<[u8]> for Buffer<T> { + #[inline(always)] + fn as_ref(&self) -> &[u8] { self.as_slice() } +} + +impl<T: Serialise> Debug for Buffer<T> { + #[inline(always)] + fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { write!(f, "{:?}", self.as_slice()) } +} + +impl<T: Serialise> Default for Buffer<T> { + #[inline(always)] + fn default() -> Self { Self::new() } +} + +impl<T: Serialise> Deref for Buffer<T> { + type Target = [u8]; + + #[inline(always)] + fn deref(&self) -> &Self::Target { self.as_slice() } +} + +impl<T: Serialise> DerefMut for Buffer<T> { + #[inline(always)] + fn deref_mut(&mut self) -> &mut Self::Target { self.as_mut_slice() } +} + +impl<T: Serialise> PartialEq<&[u8]> for Buffer<T> { + #[inline(always)] + fn eq(&self, other: &&[u8]) -> bool { self.as_slice() == *other } +} diff --git a/bzipper/src/buffer/test.rs b/bzipper/src/buffer/test.rs new file mode 100644 index 0000000..4f24e45 --- /dev/null +++ b/bzipper/src/buffer/test.rs @@ -0,0 +1,36 @@ +// 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::{Buffer, Error}; + +#[test] +fn test_buffer() { + let mut buf = Buffer::<char>::new(); + + buf.write(&'\u{1F44D}').unwrap(); + assert_eq!(buf, [0x00, 0x01, 0xF4, 0x4D].as_slice()); + + buf.as_mut_slice().copy_from_slice(&[0x00, 0x00, 0xD8, 0x00]); + assert!(matches!(buf.read(), Err(Error::InvalidCodePoint { value: 0xD800 }))); + + buf.as_mut_slice().copy_from_slice(&[0x00, 0x00, 0xFF, 0x3A]); + assert_eq!(buf.read().unwrap(), '\u{FF3A}'); +} |