From 7db3dae7a5637ee0b0fb2fc5196e90bf1f5bd5ea Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Wed, 28 Jun 2023 18:42:48 +0200 Subject: [PATCH] refactor: switch to unsigned-varint drops integer-encoding, avoiding the dep and using the same dep that multihash already uses --- Cargo.lock | 22 +--------------------- Cargo.toml | 5 +---- src/util.rs | 46 ++++++++++++++++++++++++++++++++++++---------- src/writer.rs | 7 +++---- 4 files changed, 41 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2e2cf24..1dc5cf4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,17 +35,6 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" -[[package]] -name = "async-trait" -version = "0.1.68" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9ccdd8f2a161be9bd5c023df56f1b2a0bd1d83872ae53b71a84a12c9bf6e842" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.22", -] - [[package]] name = "autocfg" version = "1.1.0" @@ -351,15 +340,6 @@ version = "0.27.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" -[[package]] -name = "integer-encoding" -version = "3.0.4" -source = "git+https://github.com/dignifiedquire/integer-encoding-rs?branch=feat-no-send-bound#6a86e902358480e61c410bc51610c34436ae2253" -dependencies = [ - "async-trait", - "tokio", -] - [[package]] name = "iroh-car" version = "0.2.1" @@ -367,11 +347,11 @@ dependencies = [ "anyhow", "cid", "futures", - "integer-encoding", "libipld", "multihash", "thiserror", "tokio", + "unsigned-varint", "wasm-bindgen-test", ] diff --git a/Cargo.toml b/Cargo.toml index 7a186f8..9e33832 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" } \ No newline at end of file diff --git a/src/util.rs b/src/util.rs index d39c2b0..0e65564 100644 --- a/src/util.rs +++ b/src/util.rs @@ -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; @@ -12,16 +11,13 @@ pub(crate) async fn ld_read(mut reader: R, buf: &mut Vec) -> Result 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)); } @@ -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( + mut reader: R, +) -> Result, 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( + 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( buf_reader: &mut R, buf: &mut Vec, @@ -56,7 +83,6 @@ where #[cfg(test)] mod tests { - use integer_encoding::VarIntAsyncWriter; use tokio::io::{AsyncWrite, AsyncWriteExt}; use super::*; @@ -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(()) diff --git a/src/writer.rs b/src/writer.rs index f3a28d8..28a378e 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -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 { @@ -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; } @@ -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?;