#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[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> {
data: B,
word_index: usize,
_marker: core::marker::PhantomData<W>,
}
impl<W: Word, B: AsRef<[W]>> MemWordWriterSlice<W, B> {
#[must_use]
pub const fn new(data: B) -> Self {
Self {
data,
word_index: 0,
_marker: core::marker::PhantomData,
}
}
#[must_use]
pub fn len(&self) -> usize {
self.data.as_ref().len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[must_use]
pub fn into_inner(self) -> B {
self.data
}
}
#[derive(Debug, Default, PartialEq)]
#[cfg_attr(feature = "mem_dbg", derive(MemDbg, MemSize))]
#[cfg(feature = "alloc")]
pub struct MemWordWriterVec<W: Word, B> {
data: B,
word_index: usize,
_marker: core::marker::PhantomData<W>,
}
#[cfg(feature = "alloc")]
impl<W: Word, B: AsRef<Vec<W>>> MemWordWriterVec<W, B> {
#[must_use]
pub const fn new(data: B) -> Self {
Self {
data,
word_index: 0,
_marker: core::marker::PhantomData,
}
}
#[must_use]
pub fn len(&self) -> usize {
self.data.as_ref().len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[must_use]
pub fn into_inner(self) -> B {
self.data
}
}
impl<W: Word, B: AsRef<[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_ref().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]>> 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<Vec<W>>> WordWrite for MemWordWriterVec<W, B> {
type Error = Infallible;
type Word = W;
#[inline]
fn write_word(&mut self, word: W) -> Result<(), Infallible> {
let data = self.data.as_mut();
if self.word_index >= data.len() {
data.resize(self.word_index + 1, W::ZERO);
}
data[self.word_index] = word;
self.word_index += 1;
Ok(())
}
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
#[cfg(feature = "alloc")]
impl<W: Word, B: AsRef<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_ref().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: AsRef<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(())
}
}
}