-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Split stream traits, bump major version. * Add test spec for slice stream. * Rename trait. * Implement SeekStream for BinaryReader/BinaryWriter. * Update test spec. Should improve the coverage. * Test write_char() and read_char(). * Use borrow trait in writer functions. * Assert on writer len().
- Loading branch information
Showing
7 changed files
with
274 additions
and
71 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
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
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
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
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,3 @@ | ||
pub(crate) mod file; | ||
pub(crate) mod memory; | ||
pub(crate) mod slice; |
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,57 @@ | ||
//! Stream that reads from a slice of bytes. | ||
use crate::{BinaryError, ReadStream, Result, SeekStream}; | ||
use std::io::{Error, ErrorKind, Read}; | ||
|
||
/// Stream that wraps a slice of bytes. | ||
pub struct SliceStream<'a> { | ||
buffer: &'a [u8], | ||
position: usize, | ||
} | ||
|
||
impl<'a> SliceStream<'a> { | ||
/// Create a slice stream. | ||
pub fn new(buffer: &'a [u8]) -> Self { | ||
Self { | ||
buffer, | ||
position: 0, | ||
} | ||
} | ||
} | ||
|
||
impl SeekStream for SliceStream<'_> { | ||
fn seek(&mut self, to: usize) -> Result<usize> { | ||
self.position = to; | ||
Ok(self.position) | ||
} | ||
|
||
fn tell(&mut self) -> Result<usize> { | ||
Ok(self.position) | ||
} | ||
|
||
fn len(&self) -> Result<usize> { | ||
Ok(self.buffer.len()) | ||
} | ||
} | ||
|
||
impl Read for SliceStream<'_> { | ||
fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> { | ||
if self.position + buffer.len() > self.buffer.len() { | ||
return Err(Error::new( | ||
ErrorKind::UnexpectedEof, | ||
BinaryError::ReadPastEof, | ||
)); | ||
} | ||
|
||
let mut idx = 0; | ||
for i in self.position..self.position + buffer.len() { | ||
buffer[idx] = self.buffer[i]; | ||
idx += 1; | ||
} | ||
|
||
self.position += buffer.len(); | ||
|
||
Ok(buffer.len()) | ||
} | ||
} | ||
|
||
impl ReadStream for SliceStream<'_> {} |
Oops, something went wrong.