Skip to content

Commit

Permalink
feat!: Removed dependencies, added direct source access
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0X8 committed Oct 29, 2024
1 parent 3d5c366 commit f29b4e0
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 421 deletions.
129 changes: 1 addition & 128 deletions Cargo.lock

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

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

[dependencies]
fs4 = "0.10.0"
64 changes: 21 additions & 43 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! - [`close`][crate::data::close] closes a reader, writer, or reader and writer and returns the data.

#![allow(rustdoc::broken_intra_doc_links)] // rustdoc has some issues with the links above
use crate::{DataType, Readable, Rw, Seekable, Writable};
use crate::{DataType, Readable, Rw, Seekable, Source, Writable};
use std::io::{Error, ErrorKind, Read, Result, Seek, SeekFrom, Write};

mod r#ref;
Expand Down Expand Up @@ -77,17 +77,13 @@ impl<'a> Readable<'a> for RData {
self
}

fn lock(&mut self, _: bool) -> Result<()> {
Ok(())
}

fn unlock(&mut self) -> Result<()> {
Ok(())
}

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

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

/// A [`Vec<u8>`] writer.
Expand Down Expand Up @@ -151,25 +147,16 @@ impl<'a> Writable<'a> for WData {
self
}

fn alloc(&mut self, len: u64) -> Result<()> {
self.data.resize(len as usize, 0);
Ok(())
}

fn lock(&mut self, _: bool) -> Result<()> {
Ok(())
}

fn unlock(&mut self) -> Result<()> {
Ok(())
}

fn close(self) -> Result<Option<DataType<'a>>>
where
Self: 'a,
{
Ok(Some(DataType::Vec(self.data)))
}

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

/// A [`Vec<u8>`] reader and writer.
Expand Down Expand Up @@ -250,43 +237,30 @@ impl<'a> Readable<'a> for RwData {
self
}

fn lock(&mut self, _: bool) -> Result<()> {
Ok(())
}

fn unlock(&mut self) -> Result<()> {
Ok(())
}

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

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

impl<'a> Writable<'a> for RwData {
fn as_trait(&mut self) -> &mut dyn Writable<'a> {
self
}

fn alloc(&mut self, len: u64) -> Result<()> {
self.data.resize(len as usize, 0);
Ok(())
}

fn lock(&mut self, _: bool) -> Result<()> {
Ok(())
}

fn unlock(&mut self) -> Result<()> {
Ok(())
}

fn close(self) -> Result<Option<DataType<'a>>>
where
Self: 'a,
{
Ok(Some(DataType::Vec(self.data)))
}

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

impl<'a> Rw<'a> for RwData {
Expand All @@ -297,6 +271,10 @@ impl<'a> Rw<'a> for RwData {
fn rw_close(self) -> Result<Option<DataType<'a>>> {
Ok(Some(DataType::Vec(self.data)))
}

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

/// Enumerates all the possible data types that can be passed into the [`close`] function.
Expand Down
70 changes: 21 additions & 49 deletions src/data/ref.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{DataType, Readable, Rw, Seekable, Writable};
use crate::{DataType, Readable, Rw, Seekable, Source, Writable};
use std::io::{Error, ErrorKind, Read, Result, Seek, SeekFrom, Write};

/// A [`Vec<u8>`] reference reader.
Expand Down Expand Up @@ -50,17 +50,13 @@ impl<'a> Readable<'a> for RRefData<'a> {
self
}

fn lock(&mut self, _: bool) -> Result<()> {
Ok(())
}

fn unlock(&mut self) -> Result<()> {
Ok(())
}

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

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

/// A [`Vec<u8>`] reference writer.
Expand Down Expand Up @@ -116,25 +112,13 @@ impl<'a> Writable<'a> for WRefData<'a> {
self
}

fn alloc(&mut self, len: u64) -> Result<()> {
let data_len = self.data.len();
if len > data_len as u64 {
self.data.resize(len as usize, 0);
}
Ok(())
}

fn lock(&mut self, _: bool) -> Result<()> {
Ok(())
}

fn unlock(&mut self) -> Result<()> {
Ok(())
}

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

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

/// A [`Vec<u8>`] reference reader and writer.
Expand Down Expand Up @@ -207,43 +191,27 @@ impl<'a> Readable<'a> for RwRefData<'a> {
self
}

fn lock(&mut self, _: bool) -> Result<()> {
Ok(())
}

fn unlock(&mut self) -> Result<()> {
Ok(())
}

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

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

impl<'a> Writable<'a> for RwRefData<'a> {
fn as_trait(&mut self) -> &mut dyn Writable<'a> {
self
}

fn alloc(&mut self, len: u64) -> Result<()> {
let data_len = self.data.len();
if len > data_len as u64 {
self.data.resize(len as usize, 0);
}
Ok(())
}

fn lock(&mut self, _: bool) -> Result<()> {
Ok(())
}

fn unlock(&mut self) -> Result<()> {
Ok(())
}

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

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

impl<'a> Rw<'a> for RwRefData<'a> {
Expand All @@ -254,6 +222,10 @@ impl<'a> Rw<'a> for RwRefData<'a> {
fn rw_close(self) -> Result<Option<DataType<'a>>> {
Ok(Some(DataType::VecMut(self.data)))
}

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

/// Enumerates all the possible data types that can be passed into the [`close_ref`][close] function.
Expand Down
Loading

0 comments on commit f29b4e0

Please sign in to comment.