Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add byte counter #51

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/byte_counter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2021, The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::io;

#[derive(Debug, Clone, Default)]
pub struct ByteCounter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add brief struct- and module-level documentation to indicate the intent? In this case it's pretty obvious from inspection, but nice to have for things like IDEs that provide it.

count: usize,
}

impl ByteCounter {
pub fn new() -> Self {
Default::default()
}

pub fn get(&self) -> usize {
self.count
}
}

impl io::Write for ByteCounter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let len = buf.len();
self.count += len;
Ok(len)
}

fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}

#[cfg(test)]
mod test {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another useful test could be to do multiple calls to write, in order to assert that the stored total length updates properly (and doesn't, for example, reset by mistake or something).

use std::io::Write;

use super::*;

#[test]
fn write_test() {
let mut byte_counter = ByteCounter::new();
let buf = [0u8, 1u8, 2u8, 3u8];
let new_count = byte_counter.write(&buf).unwrap();
assert_eq!(byte_counter.get(), new_count);
stringhandler marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(byte_counter.get(), buf.len());
}

#[test]
fn flush_test() {
let mut byte_counter = ByteCounter::new();
let buf = [0u8, 1u8, 2u8, 3u8];
let _count_bytes = byte_counter.write(&buf).unwrap();
// test passes if the following method does not return an error
byte_counter.flush().unwrap();
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
//! A set of useful and commonly used utilities that are used in several places in the Tari project.
pub mod bit;
pub mod byte_array;
pub mod byte_counter;
pub mod convert;
pub mod encoding;
pub mod epoch_time;
pub mod fixed_set;
pub mod hash;
pub mod hex;
pub mod limited_reader;
#[macro_use]
pub mod locks;
pub mod hidden;
Expand Down
74 changes: 74 additions & 0 deletions src/limited_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2022, The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{io, io::Read};

pub struct LimitedBytesReader<R> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add brief struct- and module-level documentation to indicate the intent? In this case it's pretty obvious from inspection, but nice to have for things like IDEs that provide it.

byte_limit: usize,
num_read: usize,
inner: R,
}

impl<R: Read> LimitedBytesReader<R> {
pub fn new(byte_limit: usize, reader: R) -> Self {
Self {
byte_limit,
num_read: 0,
inner: reader,
}
}
}
impl<R: Read> Read for LimitedBytesReader<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let read = self.inner.read(buf)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will (attempt to) fill the buffer even in the case where too many bytes are read. Is this the desired behavior? I would have thought that if an error is raised, the caller might expect the buffer to remain untouched.

self.num_read += read;
if self.num_read > self.byte_limit {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Read more bytes than the maximum ({})", self.byte_limit),
));
}
Ok(read)
}
}

#[cfg(test)]
mod test {
use std::io::Read;

use super::*;

#[test]
fn read_test() {
// read should work fine in the case of a buffer whose length is within byte_limit
let inner: &[u8] = &[0u8, 1u8, 2u8, 3u8, 4u8];
let mut reader = LimitedBytesReader::new(3, inner);
let mut buf = [0u8; 3];
let output = reader.read(&mut buf).unwrap();
assert_eq!(output, buf.len());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually check that the buffer was filled (or filled with the expected data), which would be useful to assert.


// in case of buffer with length strictly bigger than reader byte_limit, the code should throw an error
let mut new_buf = [0u8; 4];
let output = reader.read(&mut new_buf);
assert!(output.is_err());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the above comment about buffers and error handling is adopted, would be useful to assert that the buffer is unchanged.

}
}