-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing source file for
decoder::ioslice
- Loading branch information
Showing
2 changed files
with
76 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use std::io::Error as IoError; | ||
use std::io::Read; | ||
use std::io::Seek; | ||
use std::io::SeekFrom; | ||
use std::rc::Rc; | ||
use std::sync::RwLock; | ||
|
||
pub struct IoSlice<R: Read + Seek> { | ||
reader: Rc<RwLock<R>>, | ||
pos: u64, | ||
start: u64, | ||
end: u64, | ||
} | ||
|
||
impl<R: Read + Seek> IoSlice<R> { | ||
pub fn new(reader: Rc<RwLock<R>>, start: u64, end: u64) -> Self { | ||
Self { | ||
reader, | ||
start, | ||
end, | ||
pos: start, | ||
} | ||
} | ||
} | ||
|
||
impl<R: Read + Seek> Read for IoSlice<R> { | ||
fn read(&mut self, mut buffer: &mut [u8]) -> Result<usize, IoError> { | ||
let mut reader = self.reader.write().unwrap(); | ||
reader.seek(SeekFrom::Start(self.pos))?; | ||
|
||
let mut remaining = (self.end - self.pos) as usize; | ||
Check warning on line 31 in src/decoder/ioslice.rs GitHub Actions / Test (nightly)
Check warning on line 31 in src/decoder/ioslice.rs GitHub Actions / Test (beta)
|
||
if buffer.len() > remaining { | ||
buffer = &mut buffer[..remaining]; | ||
} | ||
|
||
let n = reader.read(buffer)?; | ||
self.pos += n as u64; | ||
|
||
Ok(n) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use std::io::Cursor; | ||
|
||
#[test] | ||
fn read() { | ||
let data = b"ABCDEF"; | ||
let cursor = Cursor::new(&data); | ||
let f = Rc::new(RwLock::new(cursor)); | ||
|
||
let mut s1 = IoSlice::new(f.clone(), 0, 2); | ||
let mut s2 = IoSlice::new(f.clone(), 2, 4); | ||
let mut s3 = IoSlice::new(f.clone(), 4, 6); | ||
|
||
let mut x1 = String::new(); | ||
let mut x2 = String::new(); | ||
let mut x3 = String::new(); | ||
|
||
s2.read_to_string(&mut x2); | ||
assert_eq!(x2, "CD"); | ||
|
||
s1.read_to_string(&mut x1); | ||
assert_eq!(x1, "AB"); | ||
|
||
s3.read_to_string(&mut x3); | ||
assert_eq!(x3, "EF"); | ||
|
||
x2.clear(); | ||
s2.read_to_string(&mut x2); | ||
assert_eq!(x2, ""); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.