dsi_bitstream/impls/
mem_word_writer.rs1#[cfg(feature = "alloc")]
10use core::convert::Infallible;
11#[cfg(feature = "mem_dbg")]
12use mem_dbg::{MemDbg, MemSize};
13
14use crate::traits::*;
15
16#[derive(Debug, PartialEq)]
62#[cfg_attr(feature = "mem_dbg", derive(MemDbg, MemSize))]
63pub struct MemWordWriterSlice<W: Word, B: AsMut<[W]>> {
64 data: B,
65 word_index: usize,
66 _marker: core::marker::PhantomData<W>,
67}
68
69impl<W: Word, B: AsMut<[W]> + AsRef<[W]>> MemWordWriterSlice<W, B> {
70 #[must_use]
72 pub fn new(data: B) -> Self {
73 Self {
74 data,
75 word_index: 0,
76 _marker: Default::default(),
77 }
78 }
79
80 pub fn len(&self) -> usize {
81 self.data.as_ref().len()
82 }
83
84 pub fn is_empty(&self) -> bool {
85 self.len() == 0
86 }
87
88 pub fn into_inner(self) -> B {
89 self.data
90 }
91}
92
93#[derive(Debug, PartialEq)]
119#[cfg_attr(feature = "mem_dbg", derive(MemDbg, MemSize))]
120#[cfg(feature = "alloc")]
121pub struct MemWordWriterVec<W: Word, B: AsMut<alloc::vec::Vec<W>>> {
122 data: B,
123 word_index: usize,
124 _marker: core::marker::PhantomData<W>,
125}
126
127#[cfg(feature = "alloc")]
128impl<W: Word, B: AsMut<alloc::vec::Vec<W>> + AsRef<alloc::vec::Vec<W>>> MemWordWriterVec<W, B> {
129 #[must_use]
131 pub fn new(data: B) -> Self {
132 Self {
133 data,
134 word_index: 0,
135 _marker: Default::default(),
136 }
137 }
138
139 pub fn len(&self) -> usize {
140 self.data.as_ref().len()
141 }
142
143 pub fn is_empty(&self) -> bool {
144 self.len() == 0
145 }
146
147 pub fn into_inner(self) -> B {
148 self.data
149 }
150}
151
152impl<W: Word, B: AsMut<[W]>> WordRead for MemWordWriterSlice<W, B> {
153 type Error = WordError;
154 type Word = W;
155
156 #[inline]
157 fn read_word(&mut self) -> Result<W, WordError> {
158 match self.data.as_mut().get(self.word_index) {
159 Some(word) => {
160 self.word_index += 1;
161 Ok(*word)
162 }
163 None => Err(WordError::UnexpectedEof {
164 word_pos: self.word_index,
165 }),
166 }
167 }
168}
169
170impl<W: Word, B: AsRef<[W]> + AsMut<[W]>> WordSeek for MemWordWriterSlice<W, B> {
171 type Error = WordError;
172
173 #[inline(always)]
174 fn word_pos(&mut self) -> Result<u64, WordError> {
175 Ok(self.word_index as u64)
176 }
177
178 #[inline(always)]
179 fn set_word_pos(&mut self, word_index: u64) -> Result<(), WordError> {
180 if word_index > self.data.as_ref().len() as u64 {
181 Err(WordError::UnexpectedEof {
182 word_pos: self.word_index,
183 })
184 } else {
185 self.word_index = word_index as usize;
186 Ok(())
187 }
188 }
189}
190
191impl<W: Word, B: AsMut<[W]>> WordWrite for MemWordWriterSlice<W, B> {
192 type Error = WordError;
193 type Word = W;
194
195 #[inline]
196 fn write_word(&mut self, word: W) -> Result<(), WordError> {
197 match self.data.as_mut().get_mut(self.word_index) {
198 Some(word_ref) => {
199 self.word_index += 1;
200 *word_ref = word;
201 Ok(())
202 }
203 None => Err(WordError::UnexpectedEof {
204 word_pos: self.word_index,
205 }),
206 }
207 }
208
209 fn flush(&mut self) -> Result<(), Self::Error> {
210 Ok(())
211 }
212}
213
214#[cfg(feature = "alloc")]
215impl<W: Word, B: AsMut<alloc::vec::Vec<W>>> WordWrite for MemWordWriterVec<W, B> {
216 type Error = Infallible;
217 type Word = W;
218
219 #[inline]
220 fn write_word(&mut self, word: W) -> Result<(), Infallible> {
221 if self.word_index >= self.data.as_mut().len() {
222 self.data.as_mut().resize(self.word_index + 1, W::ZERO);
223 }
224 self.data.as_mut()[self.word_index] = word;
225 self.word_index += 1;
226 Ok(())
227 }
228
229 fn flush(&mut self) -> Result<(), Self::Error> {
230 Ok(())
231 }
232}
233
234#[cfg(feature = "alloc")]
235impl<W: Word, B: AsMut<alloc::vec::Vec<W>>> WordRead for MemWordWriterVec<W, B> {
236 type Error = WordError;
237 type Word = W;
238
239 #[inline]
240 fn read_word(&mut self) -> Result<W, WordError> {
241 match self.data.as_mut().get(self.word_index) {
242 Some(word) => {
243 self.word_index += 1;
244 Ok(*word)
245 }
246 None => Err(WordError::UnexpectedEof {
247 word_pos: self.word_index,
248 }),
249 }
250 }
251}
252
253#[cfg(feature = "alloc")]
254impl<W: Word, B: AsMut<alloc::vec::Vec<W>> + AsRef<alloc::vec::Vec<W>>> WordSeek
255 for MemWordWriterVec<W, B>
256{
257 type Error = WordError;
258
259 #[inline(always)]
260 fn word_pos(&mut self) -> Result<u64, WordError> {
261 Ok(self.word_index as u64)
262 }
263
264 #[inline(always)]
265 fn set_word_pos(&mut self, word_index: u64) -> Result<(), WordError> {
266 if word_index > self.data.as_ref().len() as u64 {
267 Err(WordError::UnexpectedEof {
268 word_pos: self.word_index,
269 })
270 } else {
271 self.word_index = word_index as usize;
272 Ok(())
273 }
274 }
275}