Skip to content

Commit

Permalink
Rename reader trait
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKickliter committed Nov 2, 2023
1 parent c023468 commit 23e8aec
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/disktree/iter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
cell::CellStack,
disktree::{dptr, tree::HDR_SZ, Deserializer},
disktree::{dptr, tree::HDR_SZ, ReadVal},
error::Result,
};
use byteorder::ReadBytesExt;
Expand Down Expand Up @@ -120,9 +120,9 @@ where
impl<'a, R, F> Iterator for Iter<'a, R, F>
where
R: Read + Seek,
F: Deserializer<R>,
F: ReadVal<R>,
{
type Item = <F as Deserializer<R>>::T;
type Item = <F as ReadVal<R>>::T;

fn next(&mut self) -> Option<Self::Item> {
while self.curr.is_none() {
Expand Down
4 changes: 2 additions & 2 deletions src/disktree/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! An on-disk hextree.
pub use tree::DiskTree;
pub use value_read::Deserializer;
pub use value::ReadVal;

mod dptr;
mod iter;
mod tree;
mod value_read;
mod value;
mod writer;

#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions src/disktree/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
disktree::{
dptr::{self, DPTR_NULL, DPTR_SZ},
iter::Iter,
Deserializer,
ReadVal,
},
error::{Error, Result},
Cell,
Expand Down Expand Up @@ -87,9 +87,9 @@ impl<R: Read + Seek> DiskTree<R> {
pub fn iter<'a, F>(
&'a mut self,
f: F,
) -> Result<impl Iterator<Item = <F as Deserializer<R>>::T> + 'a>
) -> Result<impl Iterator<Item = <F as ReadVal<R>>::T> + 'a>
where
F: Deserializer<R> + 'a,
F: ReadVal<R> + 'a,
{
Iter::new(&mut self.0, f)
}
Expand Down
37 changes: 37 additions & 0 deletions src/disktree/value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::{error::Result, Cell};
use std::io::Read;

/// The `ReadVal` trait defines the contract for reading concrete
/// types from a disk tree.
pub trait ReadVal<R> {
/// The associated type `T` represents the result of
/// deserialization, which may be the deserialized type or an
/// error result, depending on fallibility.
type T;

/// Reads data from the provided reader and returns the
/// deserialized result.
///
/// # Arguments
///
/// * `rdr` - A `Result` containing a `Cell` and a mutable
/// reference to the reader.
///
/// # Returns
///
/// The deserialized result, which can be the deserialized type or
/// an error.
fn read(&self, rdr: Result<(Cell, &mut R)>) -> Self::T;
}

impl<R, T, F> ReadVal<R> for F
where
R: Read,
F: Fn(Result<(Cell, &mut R)>) -> T,
{
type T = T;

fn read(&self, rdr: Result<(Cell, &mut R)>) -> T {
self(rdr)
}
}
33 changes: 0 additions & 33 deletions src/disktree/value_read.rs

This file was deleted.

0 comments on commit 23e8aec

Please sign in to comment.