comparison utils/src/lib.rs @ 757:21b186be2590

Split the Rust version into multiple crates.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 05 Jan 2021 02:16:32 +0100
parents src/util/mod.rs@7d92730bf543
children daa23a4ff24d
comparison
equal deleted inserted replaced
756:4d91790cf8ab 757:21b186be2590
1 //! Module containing a bunch of helper modules.
2
3 pub mod bitstream;
4 pub mod lzss;
5 pub mod math;
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 }