From 1680dc7c18135548d1a0e237d5b4a8d95a79cd68 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 25 Nov 2025 09:47:08 -0800 Subject: First stab at adding async/tokio support. --- rust/Cargo.lock | 25 +++++++++++++++++++++++++ rust/Cargo.toml | 4 ++++ rust/src/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) (limited to 'rust') diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 013989c..3c89b5e 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -2,6 +2,31 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "bytes" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" + [[package]] name = "gats" version = "0.1.0" +dependencies = [ + "tokio", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "tokio" +version = "1.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff360e02eab121e0bc37a2d3b4d4dc622e6eda3a8e5253d5435ecf5bd4c68408" +dependencies = [ + "bytes", + "pin-project-lite", +] diff --git a/rust/Cargo.toml b/rust/Cargo.toml index d8b03fe..e945f0b 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -4,3 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] +tokio = { version = "1.48.0", optional = true, features = ["io-util"] } + +[features] +tokio = ["dep:tokio"] diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 2ccc2cf..6f52702 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -28,6 +28,9 @@ use std::num::FpCategory; use std::sync::LazyLock; use std::vec::Vec; +#[cfg(feature = "tokio")] +use tokio::io::{AsyncReadExt,AsyncWriteExt,AsyncRead,AsyncWrite}; + /// Represents a value in a Gats structure. #[derive(PartialEq)] pub enum Value { @@ -395,6 +398,17 @@ impl Value { Err(e) => Err(e), } } + + #[cfg(feature = "tokio")] + pub async fn write_packet_async(&self, w: &mut W) -> Result<(), Error> + where + W: AsyncWrite + Unpin + { + match self.to_packet() { + Ok(body) => w.write_all(&body).await, + Err(e) => Err(e), + } + } pub fn bytes_to_read(r: &[u8]) -> Result { if r.len() < 5 { @@ -468,6 +482,35 @@ impl Value { } } } + + #[cfg(feature = "tokio")] + pub async fn read_packet_async(r: &mut R) -> Result + where + R: AsyncRead + Unpin + { + let mut buf = Vec::::new(); + let mut fill = 0usize; + loop { + match Value::bytes_to_read(&buf) { + Ok(BytesToRead::Estimate(size)) => { + buf.resize(size, 0u8); + let amount = r.read(&mut buf[fill..]).await?; + fill += amount; + } + Ok(BytesToRead::Final(size)) => { + buf.resize(size, 0u8); + let amount = r.read(&mut buf[fill..]).await?; + fill += amount; + if fill == size { + return Value::from_packet(&buf); + } + } + Err(e) => { + return Err(e); + } + } + } + } } impl From for Value { @@ -649,4 +692,10 @@ mod tests { assert!(v == v2); Ok(()) } + + #[cfg(feature = "tokio")] + #[test] + fn async_test() -> Result<(), Box> { + Ok(()) + } } -- cgit v1.2.3