-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87971c7
commit 30f16a4
Showing
59 changed files
with
570 additions
and
965 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 was deleted.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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 @@ | ||
use std::io; | ||
|
||
use bytes::Bytes; | ||
|
||
/// A helper trait to implement zero copy reads on a `Cursor<Bytes>` type. | ||
pub trait BytesCursor { | ||
/// Returns the remaining bytes in the cursor. | ||
fn remaining(&self) -> usize; | ||
|
||
/// Extracts the remaining bytes from the cursor returning. | ||
/// | ||
/// This does not do a copy of the bytes, and is O(1) time. | ||
/// | ||
/// This is the same as `BytesCursor::extract_bytes(self.remaining())`. | ||
fn extract_remaining(&mut self) -> Bytes; | ||
|
||
/// Extracts a bytes from the cursor. | ||
/// | ||
/// This does not do a copy of the bytes, and is O(1) time. | ||
/// Returns an error if the size is greater than the remaining bytes. | ||
fn extract_bytes(&mut self, size: usize) -> io::Result<Bytes>; | ||
} | ||
|
||
impl BytesCursor for io::Cursor<Bytes> { | ||
fn remaining(&self) -> usize { | ||
self.get_ref().len() - self.position() as usize | ||
} | ||
|
||
fn extract_remaining(&mut self) -> Bytes { | ||
self.extract_bytes(self.remaining()) | ||
.expect("somehow we read past the end of the file") | ||
} | ||
|
||
fn extract_bytes(&mut self, size: usize) -> io::Result<Bytes> { | ||
let position = self.position() as usize; | ||
if position + size > self.get_ref().len() { | ||
return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "not enough bytes")); | ||
} | ||
|
||
let slice = self.get_ref().slice(position..position + size); | ||
self.set_position((position + size) as u64); | ||
|
||
Ok(slice) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_bytes_cursor() { | ||
let mut cursor = io::Cursor::new(Bytes::from_static(&[1, 2, 3, 4, 5])); | ||
let remaining = cursor.extract_remaining(); | ||
assert_eq!(remaining, Bytes::from_static(&[1, 2, 3, 4, 5])); | ||
} | ||
} |
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,9 @@ | ||
#![cfg_attr(all(coverage_nightly, test), feature(coverage_attribute))] | ||
|
||
mod bit_read; | ||
mod bit_write; | ||
mod bytes_cursor; | ||
|
||
pub use bit_read::BitReader; | ||
pub use bit_write::BitWriter; | ||
pub use bytes_cursor::BytesCursor; |
Oops, something went wrong.