comparison utils/src/lib.rs @ 758:daa23a4ff24d

utils: Replace custom SeekableSlice struct with std::io::Cursor.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 05 Jan 2021 04:11:18 +0100
parents 21b186be2590
children
comparison
equal deleted inserted replaced
757:21b186be2590 758:daa23a4ff24d
2 2
3 pub mod bitstream; 3 pub mod bitstream;
4 pub mod lzss; 4 pub mod lzss;
5 pub mod math; 5 pub mod math;
6 pub mod prng; 6 pub mod prng;
7
8 #[cfg(test)]
9 use std::io;
10
11 #[cfg(test)]
12 pub struct SeekableSlice<'a> {
13 slice: &'a [u8],
14 cursor: usize,
15 }
16
17 #[cfg(test)]
18 impl SeekableSlice<'_> {
19 pub fn new(slice: &[u8]) -> SeekableSlice {
20 SeekableSlice {
21 slice,
22 cursor: 0,
23 }
24 }
25 }
26
27 #[cfg(test)]
28 impl io::Read for SeekableSlice<'_> {
29 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
30 let length = (&self.slice[self.cursor..]).read(buf)?;
31 self.cursor += length;
32 Ok(length)
33 }
34 }
35
36 #[cfg(test)]
37 impl io::Seek for SeekableSlice<'_> {
38 fn seek(&mut self, seek_from: io::SeekFrom) -> io::Result<u64> {
39 match seek_from {
40 io::SeekFrom::Start(offset) => {
41 self.cursor = offset as usize;
42 }
43 io::SeekFrom::End(offset) => {
44 self.cursor = (self.slice.len() as i64 + offset) as usize;
45 }
46 io::SeekFrom::Current(offset) => {
47 self.cursor = (self.cursor as i64 + offset) as usize;
48 }
49 }
50 Ok(self.cursor as u64)
51 }
52 }