Skip to content

Commit

Permalink
Allow access to underlying reader.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Cohen committed Dec 18, 2024
1 parent 7a69307 commit 71adfee
Showing 1 changed file with 6 additions and 32 deletions.
38 changes: 6 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ impl<R: Read> MessageStream<R> {
pub fn bytes_read(&self) -> usize {
self.bytes_read
}

/// Returns a reference to the underlying reader.
pub fn get_ref(&self) -> &R {
&self.reader
}
}

impl<R: Read> Iterator for MessageStream<R> {
Expand Down Expand Up @@ -210,36 +215,6 @@ impl<R: Read> Iterator for MessageStream<R> {
}
}

/// Trait for readers that can report their size.
pub trait KnownSizeReader {
/// Returns the size of the reader in bytes.
///
/// Note: This function is not guaranteed to be fast. Cache the result if you need to call it multiple times.
fn size(&self) -> Result<u64>;
}

impl KnownSizeReader for File {
fn size(&self) -> Result<u64> {
Ok(self.metadata()?.len())
}
}

impl KnownSizeReader for GzDecoder<File> {
fn size(&self) -> Result<u64> {
Ok(self.get_ref().metadata()?.len())
}
}

impl<R> MessageStream<R>
where
R: KnownSizeReader,
{
/// Returns the size of the underlying reader in bytes.
pub fn reader_size(&self) -> Result<u64> {
self.reader.size()
}
}

/// Opaque type representing a price to four decimal places
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -1101,8 +1076,7 @@ mod tests {
#[ignore]
fn test_full_parse() {
// Download sample data from ftp://emi.nasdaq.com/ITCH/
let mut stream = MessageStream::from_file("sample-data/20190830.PSX_ITCH_50").unwrap();
let stream_size = stream.reader_size().unwrap();
let stream_size = stream.get_ref().metadata().unwrap().len();

let mut ct = 0;
while let Some(msg) = stream.next() {
Expand Down

0 comments on commit 71adfee

Please sign in to comment.