comparison src/util/mod.rs @ 637:afa012bb8021

Hello Rust!
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 03 Jul 2019 16:27:12 +0200
parents
children 01849ffd0180
comparison
equal deleted inserted replaced
636:4fa0a8e7d941 637:afa012bb8021
1 //! Module containing a bunch of helper modules.
2
3 pub mod bitstream;
4 pub mod lzss;
5
6 #[cfg(test)]
7 use std::io;
8
9 #[cfg(test)]
10 pub struct SeekableSlice<'a> {
11 slice: &'a [u8],
12 cursor: usize,
13 }
14
15 #[cfg(test)]
16 impl SeekableSlice<'_> {
17 pub fn new(slice: &[u8]) -> SeekableSlice {
18 SeekableSlice {
19 slice,
20 cursor: 0,
21 }
22 }
23 }
24
25 #[cfg(test)]
26 impl io::Read for SeekableSlice<'_> {
27 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
28 let length = (&self.slice[self.cursor..]).read(buf)?;
29 self.cursor += length;
30 Ok(length)
31 }
32 }
33
34 #[cfg(test)]
35 impl io::Seek for SeekableSlice<'_> {
36 fn seek(&mut self, seek_from: io::SeekFrom) -> io::Result<u64> {
37 match seek_from {
38 io::SeekFrom::Start(offset) => {
39 self.cursor = offset as usize;
40 }
41 io::SeekFrom::End(offset) => {
42 self.cursor = (self.slice.len() as i64 + offset) as usize;
43 }
44 io::SeekFrom::Current(offset) => {
45 self.cursor = (self.cursor as i64 + offset) as usize;
46 }
47 }
48 Ok(self.cursor as u64)
49 }
50 }