changeset 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 abcb586566e6
files utils/src/bitstream.rs utils/src/lib.rs utils/src/lzss.rs
diffstat 3 files changed, 6 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/utils/src/bitstream.rs
+++ b/utils/src/bitstream.rs
@@ -72,11 +72,11 @@ impl<R: io::Read + io::Seek> BitStream<R
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::util::SeekableSlice;
+    use std::io::Cursor;
 
     #[test]
     fn bit_by_bit() {
-        let data = SeekableSlice::new(&[1, 2, 3]);
+        let data = Cursor::new(vec![1, 2, 3]);
         let mut bitstream = BitStream::new(data);
 
         // 1
@@ -115,7 +115,7 @@ mod tests {
 
     #[test]
     fn byte_by_byte() {
-        let data = SeekableSlice::new(&[1, 2, 3]);
+        let data = Cursor::new(vec![1, 2, 3]);
         let mut bitstream = BitStream::new(data);
 
         assert_eq!(bitstream.read(8).unwrap(), 1);
@@ -128,7 +128,7 @@ mod tests {
 
     #[test]
     fn unaligned_bytes() {
-        let data = SeekableSlice::new(&[0, 129, 1, 128]);
+        let data = Cursor::new(vec![0, 129, 1, 128]);
         let mut bitstream = BitStream::new(data);
 
         assert_eq!(bitstream.read_bit().unwrap(), false);
--- a/utils/src/lib.rs
+++ b/utils/src/lib.rs
@@ -4,49 +4,3 @@ pub mod bitstream;
 pub mod lzss;
 pub mod math;
 pub mod prng;
-
-#[cfg(test)]
-use std::io;
-
-#[cfg(test)]
-pub struct SeekableSlice<'a> {
-    slice: &'a [u8],
-    cursor: usize,
-}
-
-#[cfg(test)]
-impl SeekableSlice<'_> {
-    pub fn new(slice: &[u8]) -> SeekableSlice {
-        SeekableSlice {
-            slice,
-            cursor: 0,
-        }
-    }
-}
-
-#[cfg(test)]
-impl io::Read for SeekableSlice<'_> {
-    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
-        let length = (&self.slice[self.cursor..]).read(buf)?;
-        self.cursor += length;
-        Ok(length)
-    }
-}
-
-#[cfg(test)]
-impl io::Seek for SeekableSlice<'_> {
-    fn seek(&mut self, seek_from: io::SeekFrom) -> io::Result<u64> {
-        match seek_from {
-            io::SeekFrom::Start(offset) => {
-                self.cursor = offset as usize;
-            }
-            io::SeekFrom::End(offset) => {
-                self.cursor = (self.slice.len() as i64 + offset) as usize;
-            }
-            io::SeekFrom::Current(offset) => {
-                self.cursor = (self.cursor as i64 + offset) as usize;
-            }
-        }
-        Ok(self.cursor as u64)
-    }
-}
--- a/utils/src/lzss.rs
+++ b/utils/src/lzss.rs
@@ -44,13 +44,13 @@ pub fn decompress<R: io::Read + io::Seek
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::util::SeekableSlice;
+    use std::io::Cursor;
 
     #[test]
     #[ignore]
     fn bit_by_bit() {
         // TODO: find actual lzss data.
-        let data = SeekableSlice::new(&[0, 0, 0]);
+        let data = Cursor::new(vec![0, 0, 0]);
         let mut bitstream = BitStream::new(data);
         decompress(&mut bitstream, 3, 0x2000, 13, 4, 3).unwrap();
     }