#[cfg(feature = "alloc")]
use core::convert::Infallible;
#[cfg(feature = "mem_dbg")]
use mem_dbg::{MemDbg, MemSize};
use crate::traits::*;
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "mem_dbg", derive(MemDbg, MemSize))]
pub struct MemWordWriterSlice<W: Word, B: AsMut<[W]>> {
data: B,
word_index: usize,
_marker: core::marker::PhantomData<W>,
}
impl<W: Word, B: AsMut<[W]> + AsRef<[W]>> MemWordWriterSlice<W, B> {
#[must_use]
pub fn new(data: B) -> Self {
Self {
data,
word_index: 0,
_marker: Default::default(),
}
}
pub fn len(&self) -> usize {
self.data.as_ref().len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn into_inner(self) -> B {
self.data
}
}
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "mem_dbg", derive(MemDbg, MemSize))]
#[cfg(feature = "alloc")]
pub struct MemWordWriterVec<W: Word, B: AsMut<alloc::vec::Vec<W>>> {
data: B,
word_index: usize,
_marker: core::marker::PhantomData<W>,
}
#[cfg(feature = "alloc")]
impl<W: Word, B: AsMut<alloc::vec::Vec<W>> + AsRef<alloc::vec::Vec<W>>> MemWordWriterVec<W, B> {
#[must_use]
pub fn new(data: B) -> Self {
Self {
data,
word_index: 0,
_marker: Default::default(),
}
}
pub fn len(&self) -> usize {
self.data.as_ref().len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn into_inner(self) -> B {
self.data
}
}
impl<W: Word, B: AsMut<[W]>> WordRead for MemWordWriterSlice<W, B> {
type Error = WordError;
type Word = W;
#[inline]
fn read_word(&mut self) -> Result<W, WordError> {
match self.data.as_mut().get(self.word_index) {
Some(word) => {
self.word_index += 1;
Ok(*word)
}
None => Err(WordError::UnexpectedEof {
word_pos: self.word_index,
}),
}
}
}
impl<W: Word, B: AsRef<[W]> + AsMut<[W]>> WordSeek for MemWordWriterSlice<W, B> {
type Error = WordError;
#[inline(always)]
fn word_pos(&mut self) -> Result<u64, WordError> {
Ok(self.word_index as u64)
}
#[inline(always)]
fn set_word_pos(&mut self, word_index: u64) -> Result<(), WordError> {
if word_index > self.data.as_ref().len() as u64 {
Err(WordError::UnexpectedEof {
word_pos: self.word_index,
})
} else {
self.word_index = word_index as usize;
Ok(())
}
}
}
impl<W: Word, B: AsMut<[W]>> WordWrite for MemWordWriterSlice<W, B> {
type Error = WordError;
type Word = W;
#[inline]
fn write_word(&mut self, word: W) -> Result<(), WordError> {
match self.data.as_mut().get_mut(self.word_index) {
Some(word_ref) => {
self.word_index += 1;
*word_ref = word;
Ok(())
}
None => Err(WordError::UnexpectedEof {
word_pos: self.word_index,
}),
}
}
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
#[cfg(feature = "alloc")]
impl<W: Word, B: AsMut<alloc::vec::Vec<W>>> WordWrite for MemWordWriterVec<W, B> {
type Error = Infallible;
type Word = W;
#[inline]
fn write_word(&mut self, word: W) -> Result<(), Infallible> {
if self.word_index >= self.data.as_mut().len() {
self.data.as_mut().resize(self.word_index + 1, W::ZERO);
}
self.data.as_mut()[self.word_index] = word;
self.word_index += 1;
Ok(())
}
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
#[cfg(feature = "alloc")]
impl<W: Word, B: AsMut<alloc::vec::Vec<W>>> WordRead for MemWordWriterVec<W, B> {
type Error = WordError;
type Word = W;
#[inline]
fn read_word(&mut self) -> Result<W, WordError> {
match self.data.as_mut().get(self.word_index) {
Some(word) => {
self.word_index += 1;
Ok(*word)
}
None => Err(WordError::UnexpectedEof {
word_pos: self.word_index,
}),
}
}
}
#[cfg(feature = "alloc")]
impl<W: Word, B: AsMut<alloc::vec::Vec<W>> + AsRef<alloc::vec::Vec<W>>> WordSeek
for MemWordWriterVec<W, B>
{
type Error = WordError;
#[inline(always)]
fn word_pos(&mut self) -> Result<u64, WordError> {
Ok(self.word_index as u64)
}
#[inline(always)]
fn set_word_pos(&mut self, word_index: u64) -> Result<(), WordError> {
if word_index > self.data.as_ref().len() as u64 {
Err(WordError::UnexpectedEof {
word_pos: self.word_index,
})
} else {
self.word_index = word_index as usize;
Ok(())
}
}
}