Skip to content

Commit

Permalink
refactor: switch to unsigned-varint
Browse files Browse the repository at this point in the history
drops integer-encoding, avoiding the dep and using the same dep that multihash already uses
  • Loading branch information
dignifiedquire committed Jun 28, 2023
1 parent 741e61b commit 7db3dae
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
22 changes: 1 addition & 21 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ readme="README.md"
anyhow = "1"
cid = "0.10"
futures = "0.3"
integer-encoding = { version = "3", features = ["tokio_async"] }
ipld = { package = "libipld", version = "0.16", features = ["dag-cbor", "derive"] }
thiserror = "1"
tokio = { version = "^1", features = ["io-util"] }
unsigned-varint = "0.7.1"

[dev-dependencies]
multihash = "0.18"
Expand All @@ -28,6 +28,3 @@ default = []

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = { version = "^0.3" }

[patch.crates-io]
integer-encoding = { git = "https://github.com/dignifiedquire/integer-encoding-rs", branch = "feat-no-send-bound" }
46 changes: 36 additions & 10 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::Result;
use cid::Cid;
use integer_encoding::VarIntAsyncReader;
use tokio::io::{AsyncRead, AsyncReadExt};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};

use super::error::Error;

Expand All @@ -12,16 +11,13 @@ pub(crate) async fn ld_read<R>(mut reader: R, buf: &mut Vec<u8>) -> Result<Optio
where
R: AsyncRead + Unpin,
{
let length: usize = match VarIntAsyncReader::read_varint_async(&mut reader).await {
Ok(len) => len,
let length: usize = match read_varint_usize(&mut reader).await {
Ok(Some(len)) => len,
Ok(None) => return Ok(None),
Err(e) => {
if e.kind() == std::io::ErrorKind::UnexpectedEof {
return Ok(None);
}
return Err(Error::Parsing(e.to_string()));
}
};

if length > MAX_ALLOC {
return Err(Error::LdReadTooLarge(length));
}
Expand All @@ -37,6 +33,37 @@ where
Ok(Some(&buf[..length]))
}

/// Read a varint from the provided reader. Returns `Ok(None)` on unexpected `EOF`.
pub async fn read_varint_usize<R: AsyncRead + Unpin>(
mut reader: R,
) -> Result<Option<usize>, unsigned_varint::io::ReadError> {
let mut b = unsigned_varint::encode::usize_buffer();
for i in 0..b.len() {
let n = reader.read(&mut b[i..i + 1]).await?;
if n == 0 {
return Ok(None);
}
if unsigned_varint::decode::is_last(b[i]) {
let slice = &b[..=i];
let (num, _) = unsigned_varint::decode::usize(slice)?;
return Ok(Some(num));
}
}
Err(unsigned_varint::decode::Error::Overflow)?
}

/// Write the given number as varint to the provided writer.
pub async fn write_varint_usize<W: AsyncWrite + Unpin>(
num: usize,
mut writer: W,
) -> std::io::Result<()> {
let mut buffer = unsigned_varint::encode::usize_buffer();
let to_write = unsigned_varint::encode::usize(num, &mut buffer);
writer.write_all(to_write).await?;

Ok(())
}

pub(crate) async fn read_node<R>(
buf_reader: &mut R,
buf: &mut Vec<u8>,
Expand All @@ -56,7 +83,6 @@ where

#[cfg(test)]
mod tests {
use integer_encoding::VarIntAsyncWriter;
use tokio::io::{AsyncWrite, AsyncWriteExt};

use super::*;
Expand All @@ -71,7 +97,7 @@ mod tests {
where
W: AsyncWrite + Send + Unpin,
{
writer.write_varint_async(bytes.len()).await?;
write_varint_usize(bytes.len(), &mut *writer).await?;
writer.write_all(bytes).await?;
writer.flush().await?;
Ok(())
Expand Down
7 changes: 3 additions & 4 deletions src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use cid::Cid;
use integer_encoding::VarIntAsyncWriter;
use tokio::io::{AsyncWrite, AsyncWriteExt};

use crate::{error::Error, header::CarHeader};
use crate::{error::Error, header::CarHeader, util::write_varint_usize};

#[derive(Debug)]
pub struct CarWriter<W> {
Expand All @@ -29,7 +28,7 @@ where
if !self.is_header_written {
// Write header bytes
let header_bytes = self.header.encode()?;
self.writer.write_varint_async(header_bytes.len()).await?;
write_varint_usize(header_bytes.len(), &mut self.writer).await?;
self.writer.write_all(&header_bytes).await?;
self.is_header_written = true;
}
Expand All @@ -50,7 +49,7 @@ where
let data = data.as_ref();
let len = self.cid_buffer.len() + data.len();

self.writer.write_varint_async(len).await?;
write_varint_usize(len, &mut self.writer).await?;
self.writer.write_all(&self.cid_buffer).await?;
self.writer.write_all(data).await?;

Expand Down

0 comments on commit 7db3dae

Please sign in to comment.