Skip to content

Commit

Permalink
fix: Fixed this "Sized" bs
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0X8 committed Oct 10, 2024
1 parent 12feb12 commit 8c77a36
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dh"
version = "0.5.0"
version = "0.6.0"
edition = "2021"
description = "Data handling in Rust, made easy."
license = "MIT"
Expand Down
20 changes: 20 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ impl Seek for RData {
impl Seekable for RData {}

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

fn lock(&mut self, _: bool) -> Result<()> {
Ok(())
}
Expand Down Expand Up @@ -143,6 +147,10 @@ impl Seek for WData {
impl Seekable for WData {}

impl<'a> Writable<'a> for WData {
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(())
Expand Down Expand Up @@ -238,6 +246,10 @@ impl Seek for RwData {
impl Seekable for RwData {}

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

fn lock(&mut self, _: bool) -> Result<()> {
Ok(())
}
Expand All @@ -252,6 +264,10 @@ impl<'a> Readable<'a> for RwData {
}

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(())
Expand All @@ -274,6 +290,10 @@ impl<'a> Writable<'a> for RwData {
}

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

fn rw_close(self) -> Result<Option<DataType<'a>>> {
Ok(Some(DataType::Vec(self.data)))
}
Expand Down
20 changes: 20 additions & 0 deletions src/data/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl Seek for RRefData<'_> {
impl Seekable for RRefData<'_> {}

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

fn lock(&mut self, _: bool) -> Result<()> {
Ok(())
}
Expand Down Expand Up @@ -108,6 +112,10 @@ impl Seek for WRefData<'_> {
impl Seekable for WRefData<'_> {}

impl<'a> Writable<'a> for WRefData<'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 {
Expand Down Expand Up @@ -195,6 +203,10 @@ impl Seek for RwRefData<'_> {
impl Seekable for RwRefData<'_> {}

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

fn lock(&mut self, _: bool) -> Result<()> {
Ok(())
}
Expand All @@ -209,6 +221,10 @@ impl<'a> Readable<'a> for RwRefData<'a> {
}

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 {
Expand All @@ -231,6 +247,10 @@ impl<'a> Writable<'a> for RwRefData<'a> {
}

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

fn rw_close(self) -> Result<Option<DataType<'a>>> {
Ok(Some(DataType::VecMut(self.data)))
}
Expand Down
20 changes: 20 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ impl Drop for RFile {
impl Seekable for RFile {}

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

fn lock(&mut self, block: bool) -> Result<()> {
if block {
self.file.lock_exclusive()
Expand Down Expand Up @@ -114,6 +118,10 @@ impl Drop for WFile {
impl Seekable for WFile {}

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

fn alloc(&mut self, len: u64) -> Result<()> {
match self.file.allocate(len) {
Ok(_) => Ok(()),
Expand Down Expand Up @@ -208,6 +216,10 @@ impl Drop for RwFile {
impl Seekable for RwFile {}

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

fn lock(&mut self, block: bool) -> Result<()> {
if block {
self.file.lock_exclusive()
Expand All @@ -228,6 +240,10 @@ impl<'a> Readable<'a> for RwFile {
}

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

fn alloc(&mut self, len: u64) -> Result<()> {
match self.file.allocate(len) {
Ok(_) => Ok(()),
Expand Down Expand Up @@ -258,6 +274,10 @@ impl<'a> Writable<'a> for RwFile {
}

impl<'a> Rw<'a> for RwFile {
fn rw_as_trait(&mut self) -> &mut dyn Rw<'a> {
self
}

fn rw_close(self) -> Result<Option<DataType<'a>>> {
self.file.unlock()?;
self.file.sync_all()?;
Expand Down
20 changes: 20 additions & 0 deletions src/limited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ impl<'a> Seek for RLimited<'a> {
impl<'a> Seekable for RLimited<'a> {}

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

fn lock(&mut self, block: bool) -> Result<()> {
self.data.lock(block)
}
Expand Down Expand Up @@ -132,6 +136,10 @@ impl<'a> Seek for WLimited<'a> {
impl<'a> Seekable for WLimited<'a> {}

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

fn alloc(&mut self, _: u64) -> Result<()> {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
Expand Down Expand Up @@ -235,6 +243,10 @@ impl<'a> Seek for RwLimited<'a> {
impl<'a> Seekable for RwLimited<'a> {}

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

fn lock(&mut self, block: bool) -> Result<()> {
Readable::lock(self.data, block)
}
Expand All @@ -249,6 +261,10 @@ impl<'a> Readable<'a> for RwLimited<'a> {
}

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

fn alloc(&mut self, _: u64) -> Result<()> {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
Expand All @@ -270,6 +286,10 @@ impl<'a> Writable<'a> for RwLimited<'a> {
}

impl<'a> Rw<'a> for RwLimited<'a> {
fn rw_as_trait(&mut self) -> &mut dyn Rw<'a> {
self
}

fn rw_close(self) -> Result<Option<crate::DataType<'a>>> {
Ok(None)
}
Expand Down
19 changes: 14 additions & 5 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ fn parse_vuxr(read_ux: &mut dyn FnMut(u8) -> Result<u128>, size: u8) -> Result<(
/// Although the trait can be implemented for any type that implements [`Read`] and [`Seekable`],
/// for most cases the [internal implementations](#implementors) are recommended.
pub trait Readable<'a>: Read + Seekable {
/// An internal method to get the reader as a trait object.
/// Yes, this is kinda nonsense, but Rust forces me into that.
///
/// ### How you implement it
///
/// ```ignore
/// fn as_trait(&mut self) -> &mut dyn Readable<'a> {
/// self
/// }
/// ```
fn as_trait(&mut self) -> &mut dyn Readable<'a>;

/// Locks the source exclusively for the current process.
/// This only has an effect on some sources, like files.
///
Expand Down Expand Up @@ -148,11 +160,8 @@ pub trait Readable<'a>: Read + Seekable {
/// let size = limited.size().unwrap();
/// assert_eq!(limited.read_bytes(size).unwrap(), vec![2, 3, 4, 5]);
/// ```
fn limit(&'a mut self, pos: u64, length: u64) -> Result<RLimited<'a>>
where
Self: Sized,
{
limit_r(self, pos, length)
fn limit(&'a mut self, pos: u64, length: u64) -> Result<RLimited<'a>> {
limit_r(self.as_trait(), pos, length)
}

/// Copies data from the current position to a target at a specific position.
Expand Down
19 changes: 14 additions & 5 deletions src/rw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ pub trait Rw<'a>
where
Self: Readable<'a> + Writable<'a>,
{
/// An internal method to get the reader as a trait object.
/// Yes, this is kinda nonsense, but Rust forces me into that.
///
/// ### How you implement it
///
/// ```ignore
/// fn rw_as_trait(&mut self) -> &mut dyn Rw<'a> {
/// self
/// }
/// ```
fn rw_as_trait(&mut self) -> &mut dyn Rw<'a>;

/// Closes the R/W stream and can return the target if it was moved or references it.
///
/// Use this instead of [`Readable::close`] or [`Writable::close`] for cleaner code as you avoid the naming conflict.
Expand All @@ -32,10 +44,7 @@ where
/// limited.rewind().unwrap();
/// assert_eq!(limited.read_u16be().unwrap(), 0x0104);
/// ```
fn rw_limit(&'a mut self, start: u64, length: u64) -> Result<RwLimited<'a>>
where
Self: Sized,
{
limit_rw(self, start, length)
fn rw_limit(&'a mut self, start: u64, length: u64) -> Result<RwLimited<'a>> {
limit_rw(self.rw_as_trait(), start, length)
}
}
19 changes: 14 additions & 5 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ pub trait Writable<'a>
where
Self: Write + Seekable,
{
/// An internal method to get the reader as a trait object.
/// Yes, this is kinda nonsense, but Rust forces me into that.
///
/// ### How you implement it
///
/// ```ignore
/// fn as_trait(&mut self) -> &mut dyn Writable<'a> {
/// self
/// }
/// ```
fn as_trait(&mut self) -> &mut dyn Writable<'a>;

/// Pre-allocates space in the data stream.
/// This is useful when you know the size of the data you are going to write and want to avoid reallocations for performance reasons.
/// Also, you can guarantee that the data will fit in the stream.
Expand Down Expand Up @@ -144,11 +156,8 @@ where
/// let data = dh::data::close(writer);
/// assert_eq!(data, vec![0, 1, 5, 4, 3, 2, 6, 7]);
/// ```
fn limit(&'a mut self, pos: u64, length: u64) -> Result<WLimited<'a>>
where
Self: Sized,
{
limit_w(self, pos, length)
fn limit(&'a mut self, pos: u64, length: u64) -> Result<WLimited<'a>> {
limit_w(self.as_trait(), pos, length)
}

/// Writes bytes at a specific position.
Expand Down

0 comments on commit 8c77a36

Please sign in to comment.