Skip to content

Commit

Permalink
fix!: Fixed limited readers & writers, README, prelude and #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0X8 committed Nov 13, 2024
1 parent d0f38d6 commit ec405af
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 70 deletions.
11 changes: 10 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dh"
version = "0.7.2"
version = "0.8.0"
edition = "2021"
description = "Data handling in Rust, made easy."
license = "MIT"
Expand All @@ -12,3 +12,6 @@ keywords = [ "data", "files", "read", "write", "rw" ]
categories = [ "data-structures", "encoding", "filesystem", "parsing" ]

[dependencies]

[dev-dependencies]
murmur3 = "0.5.2"
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use dh::recommended::*;
fn main() {
let mut file = dh::file::open_r("data.txt").unwrap();
let size = file.size().unwrap();
assert_eq!(file.read_utf8(size), "Hello, world!\n");
assert_eq!(file.read_utf8(size).unwrap(), "Hello, world!\n");
}
```

Expand All @@ -62,8 +62,8 @@ use dh::recommended::*;

fn main() {
let mut file = dh::file::open_w("data.txt").unwrap();
file.write_utf8_at("Hello, world!\n", 0);
file.close(); // optional, but recommended
file.write_utf8_at("Hello, world!\n", 0).unwrap();
file.close().unwrap(); // optional, but recommended
}
```

Expand All @@ -74,9 +74,10 @@ use dh::recommended::*;

fn main() {
let mut file = dh::file::open_rw("data.txt").unwrap();
file.write_utf8_at("Hello, world!\n", 0);
file.rewind();
assert_eq!(file.read_utf8(file.size()), "Hello, world!\n");
file.write_utf8_at("Hello, world!\n", 0).unwrap();
file.rewind().unwrap();
let size = file.size().unwrap();
assert_eq!(file.read_utf8(size).unwrap(), "Hello, world!\n");
}
```

Expand All @@ -92,7 +93,7 @@ use dh::recommended::*;
fn main() {
let mut data = vec![31u8; 1];
let mut rw = dh::data::read_ref(&data);
assert_eq!(rw.read_u8(), 31);
assert_eq!(rw.read_u8().unwrap(), 31);
}
```

Expand All @@ -104,9 +105,9 @@ use dh::recommended::*;
fn main() {
let mut data = vec![0u8; 1];
let mut rw = dh::data::rw_ref(&mut data);
rw.write_u8(31);
rw.rewind();
assert_eq!(rw.read_u8(), 31);
rw.write_u8(31).unwrap();
rw.rewind().unwrap();
assert_eq!(rw.read_u8().unwrap(), 31);
}
```

Expand All @@ -118,11 +119,11 @@ use dh::recommended::*;
fn main() {
let data = vec![0u8; 1];
let mut rw = dh::data::rw(data);
rw.write_u8(31);
rw.rewind();
assert_eq!(rw.read_u8(), 31);
rw.write_u8(31).unwrap();
rw.rewind().unwrap();
assert_eq!(rw.read_u8().unwrap(), 31);

let data = dh::data::close(rw);
let data = dh::data::close(rw).unwrap();
assert_eq!(data, vec![31]);
}
```
Expand Down Expand Up @@ -152,8 +153,8 @@ use dh::recommended::*;

fn main() {
let mut file = dh::file::open_r("data.txt").unwrap();
let mut limited = file.limit(0, 5);
assert_eq!(limited.read_utf8(5), "Hello");
let mut limited = file.limit(0, 5).unwrap();
assert_eq!(limited.read_utf8(5).unwrap(), "Hello");
}
```

Expand Down
54 changes: 27 additions & 27 deletions src/data/ref.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{DataType, Readable, Rw, Seekable, Source, Writable};
use std::io::{Error, ErrorKind, Read, Result, Seek, SeekFrom, Write};

/// A [`Vec<u8>`] reference reader.
/// A [`[u8]`] reference reader.
pub struct RRefData<'a> {
data: &'a Vec<u8>,
data: &'a [u8],
pos: usize,
}

Expand Down Expand Up @@ -51,17 +51,17 @@ impl<'a> Readable<'a> for RRefData<'a> {
}

fn close(self) -> Result<Option<DataType<'a>>> {
Ok(Some(DataType::VecRef(self.data)))
Ok(Some(DataType::Ref(self.data)))
}

fn source(&mut self) -> Source {
Source::VecRef(self.data)
Source::Ref(self.data)
}
}

/// A [`Vec<u8>`] reference writer.
/// A [`[u8]`] reference writer.
pub struct WRefData<'a> {
data: &'a mut Vec<u8>,
data: &'a mut [u8],
pos: usize,
}

Expand Down Expand Up @@ -113,17 +113,17 @@ impl<'a> Writable<'a> for WRefData<'a> {
}

fn close(self) -> Result<Option<DataType<'a>>> {
Ok(Some(DataType::VecMut(self.data)))
Ok(Some(DataType::Mut(self.data)))
}

fn source(&mut self) -> Source {
Source::Vec(self.data)
Source::Mut(self.data)
}
}

/// A [`Vec<u8>`] reference reader and writer.
/// A [`[u8]`] reference reader and writer.
pub struct RwRefData<'a> {
data: &'a mut Vec<u8>,
data: &'a mut [u8],
pos: usize,
}

Expand Down Expand Up @@ -192,11 +192,11 @@ impl<'a> Readable<'a> for RwRefData<'a> {
}

fn close(self) -> Result<Option<DataType<'a>>> {
Ok(Some(DataType::VecMut(self.data)))
Ok(Some(DataType::Mut(self.data)))
}

fn source(&mut self) -> Source {
Source::Vec(self.data)
Source::Mut(self.data)
}
}

Expand All @@ -206,11 +206,11 @@ impl<'a> Writable<'a> for RwRefData<'a> {
}

fn close(self) -> Result<Option<DataType<'a>>> {
Ok(Some(DataType::VecMut(self.data)))
Ok(Some(DataType::Mut(self.data)))
}

fn source(&mut self) -> Source {
Source::Vec(self.data)
Source::Mut(self.data)
}
}

Expand All @@ -220,11 +220,11 @@ impl<'a> Rw<'a> for RwRefData<'a> {
}

fn rw_close(self) -> Result<Option<DataType<'a>>> {
Ok(Some(DataType::VecMut(self.data)))
Ok(Some(DataType::Mut(self.data)))
}

fn rw_source(&mut self) -> Source {
Source::Vec(self.data)
Source::Mut(self.data)
}
}

Expand Down Expand Up @@ -271,7 +271,7 @@ impl<'a> From<RwRefData<'a>> for ClosableMutData<'a> {
}
}

/// Creates a new reader from a [`Vec<u8>`] reference.
/// Creates a new reader from a [`[u8]`] reference.
///
/// ### Example
///
Expand All @@ -283,11 +283,11 @@ impl<'a> From<RwRefData<'a>> for ClosableMutData<'a> {
///
/// assert_eq!(reader.read_u8_at(0).unwrap(), 0);
/// ```
pub fn read(data: &Vec<u8>) -> RRefData {
pub fn read(data: &[u8]) -> RRefData {
RRefData { data, pos: 0 }
}

/// Creates a new writer from a [`Vec<u8>`] reference.
/// Creates a new writer from a [`[u8]`] reference.
///
/// ### Example
/// ```rust
Expand All @@ -301,11 +301,11 @@ pub fn read(data: &Vec<u8>) -> RRefData {
///
/// assert_eq!(data, "Hello, Rust!!".as_bytes());
/// ```
pub fn write(data: &mut Vec<u8>) -> WRefData {
pub fn write(data: &mut [u8]) -> WRefData {
WRefData { data, pos: 0 }
}

/// Creates a new reader and writer from a [`Vec<u8>`] reference.
/// Creates a new reader and writer from a [`[u8]`] reference.
///
/// ### Example
/// ```rust
Expand All @@ -319,7 +319,7 @@ pub fn write(data: &mut Vec<u8>) -> WRefData {
///
/// rw.rw_close().unwrap(); // removes the mutable reference
/// ```
pub fn rw(data: &mut Vec<u8>) -> RwRefData {
pub fn rw(data: &mut [u8]) -> RwRefData {
RwRefData { data, pos: 0 }
}

Expand All @@ -342,15 +342,15 @@ pub fn rw(data: &mut Vec<u8>) -> RwRefData {
///
/// assert_eq!(dh::data::close_ref(reader), &data);
/// ```
pub fn close<'a, T: Into<ClosableRefData<'a>>>(closable: T) -> &'a Vec<u8> {
pub fn close<'a, T: Into<ClosableRefData<'a>>>(closable: T) -> &'a [u8] {
// these unwraps are safe because the data is always returned
match match closable.into() {
ClosableRefData::R(r) => r.close().unwrap().unwrap(),
ClosableRefData::W(w) => w.close().unwrap().unwrap(),
ClosableRefData::Rw(rw) => rw.rw_close().unwrap().unwrap(),
} {
DataType::VecRef(data) => data,
DataType::VecMut(data) => data,
DataType::Ref(data) => data,
DataType::Mut(data) => data,
_ => unreachable!(),
}
}
Expand All @@ -374,13 +374,13 @@ pub fn close<'a, T: Into<ClosableRefData<'a>>>(closable: T) -> &'a Vec<u8> {
///
/// assert_eq!(data_ref, &mut "Hello, Rust!!".as_bytes().to_vec());
/// ```
pub fn close_mut<'a, T: Into<ClosableMutData<'a>>>(closable: T) -> &'a mut Vec<u8> {
pub fn close_mut<'a, T: Into<ClosableMutData<'a>>>(closable: T) -> &'a mut [u8] {
// these unwraps are safe because the data is always returned
match match closable.into() {
ClosableMutData::W(w) => w.close().unwrap().unwrap(),
ClosableMutData::Rw(rw) => rw.rw_close().unwrap().unwrap(),
} {
DataType::VecMut(data) => data,
DataType::Mut(data) => data,
_ => unreachable!(),
}
}
9 changes: 8 additions & 1 deletion src/limited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ impl<'a> Read for RLimited<'a> {
let start_pos = self.data.pos()?;
let end_pos = start_pos + buf.len() as u64;

if start_pos < self.start || end_pos > self.end {
if start_pos < self.start {
return Err(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"reading out of bounds",
));
}

if end_pos > self.end {
let read_len = (self.end - start_pos) as usize;
#[allow(clippy::unused_io_amount)]
self.data.read(&mut buf[..read_len])?;
return Ok(read_len);
}

self.data.read(buf)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub use crate::{
self as dh, Readable as DhCrateReadable, Rw as DhCrateRw, Seekable as DhCrateSeekable,
Writable as DhCrateWritable,
};
pub use std::io::Seek as StdIoSeek;
20 changes: 0 additions & 20 deletions src/seek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,6 @@ use std::io::{Result, Seek, SeekFrom};
/// includes any type that implements [`Readable`][crate::Readable],
/// [`Writable`][crate::Writable] and [`Rw`][crate::Rw] as all these traits require [`Seek`] to be implemented.
pub trait Seekable: Seek {
/// Sets the stream position to the beginning.
/// This is the same as calling [`to`][Seekable::to] with `0`.
///
/// ### Example
///
/// ```rust
/// use dh::recommended::*;
///
/// let mut writer = dh::data::write_new(2);
///
/// writer.write_u8(0x80).unwrap(); // reads the first byte
/// assert_eq!(writer.pos().unwrap(), 1);
///
/// writer.rewind().unwrap(); // sets the position to the beginning
/// assert_eq!(writer.pos().unwrap(), 0);
/// ```
fn rewind(&mut self) -> Result<u64> {
self.seek(SeekFrom::Start(0))
}

/// Sets the stream position to the end. It is not recommended to read anything after this because it would result in an EOF error.
///
/// ### Example
Expand Down
8 changes: 4 additions & 4 deletions src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::fs::File;
/// **This data type is only intended for internal usage.**
pub enum DataType<'a> {
Vec(Vec<u8>),
VecRef(&'a Vec<u8>),
VecMut(&'a mut Vec<u8>),
Ref(&'a [u8]),
Mut(&'a mut [u8]),
}

/// A helper enum to allow getting direct access on the data source.
Expand All @@ -15,6 +15,6 @@ pub enum DataType<'a> {
pub enum Source<'a> {
File(&'a mut File),
Vec(&'a mut Vec<u8>),
VecRef(&'a Vec<u8>),
VecMut(&'a mut Vec<u8>),
Ref(&'a [u8]),
Mut(&'a mut [u8]),
}
18 changes: 18 additions & 0 deletions tests/limited.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use dh::recommended::*;
use murmur3::murmur3_32;

#[test]
fn r000() {
Expand Down Expand Up @@ -46,6 +47,23 @@ fn r001() {
assert_eq!(reader.pos().unwrap(), 12);
}

#[test]
fn r002() {
let data = "Hello, world!".as_bytes().to_vec();

let mut reader = dh::data::read(data);

let mut limited = reader.limit(0, 5).unwrap();

let hash = murmur3_32(&mut limited, 0).unwrap();
assert_eq!(hash, 316307400);

let mut reader = limited.unlimit();
reader.rewind().unwrap();
let hash = murmur3_32(&mut reader, 0).unwrap();
assert_eq!(hash, 3224780355);
}

#[test]
fn w000() {
let mut data = "Hello, world!".as_bytes().to_vec();
Expand Down

0 comments on commit ec405af

Please sign in to comment.