aboutsummaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2025-11-25 09:47:08 -0800
committerMike Buland <mike@xagasoft.com>2025-11-25 09:47:08 -0800
commit1680dc7c18135548d1a0e237d5b4a8d95a79cd68 (patch)
tree7a05089ad0a8b737dad4d5d36d1a98c8bcfbaca0 /rust
parent8c5b9963e3467434777eeb104111478e39190384 (diff)
downloadlibgats-1680dc7c18135548d1a0e237d5b4a8d95a79cd68.tar.gz
libgats-1680dc7c18135548d1a0e237d5b4a8d95a79cd68.tar.bz2
libgats-1680dc7c18135548d1a0e237d5b4a8d95a79cd68.tar.xz
libgats-1680dc7c18135548d1a0e237d5b4a8d95a79cd68.zip
First stab at adding async/tokio support.
Diffstat (limited to '')
-rw-r--r--rust/Cargo.lock25
-rw-r--r--rust/Cargo.toml4
-rw-r--r--rust/src/lib.rs49
3 files changed, 78 insertions, 0 deletions
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 013989c..3c89b5e 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -3,5 +3,30 @@
3version = 4 3version = 4
4 4
5[[package]] 5[[package]]
6name = "bytes"
7version = "1.11.0"
8source = "registry+https://github.com/rust-lang/crates.io-index"
9checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3"
10
11[[package]]
6name = "gats" 12name = "gats"
7version = "0.1.0" 13version = "0.1.0"
14dependencies = [
15 "tokio",
16]
17
18[[package]]
19name = "pin-project-lite"
20version = "0.2.16"
21source = "registry+https://github.com/rust-lang/crates.io-index"
22checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
23
24[[package]]
25name = "tokio"
26version = "1.48.0"
27source = "registry+https://github.com/rust-lang/crates.io-index"
28checksum = "ff360e02eab121e0bc37a2d3b4d4dc622e6eda3a8e5253d5435ecf5bd4c68408"
29dependencies = [
30 "bytes",
31 "pin-project-lite",
32]
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"
4edition = "2024" 4edition = "2024"
5 5
6[dependencies] 6[dependencies]
7tokio = { version = "1.48.0", optional = true, features = ["io-util"] }
8
9[features]
10tokio = ["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;
28use std::sync::LazyLock; 28use std::sync::LazyLock;
29use std::vec::Vec; 29use std::vec::Vec;
30 30
31#[cfg(feature = "tokio")]
32use tokio::io::{AsyncReadExt,AsyncWriteExt,AsyncRead,AsyncWrite};
33
31/// Represents a value in a Gats structure. 34/// Represents a value in a Gats structure.
32#[derive(PartialEq)] 35#[derive(PartialEq)]
33pub enum Value { 36pub enum Value {
@@ -395,6 +398,17 @@ impl Value {
395 Err(e) => Err(e), 398 Err(e) => Err(e),
396 } 399 }
397 } 400 }
401
402 #[cfg(feature = "tokio")]
403 pub async fn write_packet_async<W>(&self, w: &mut W) -> Result<(), Error>
404 where
405 W: AsyncWrite + Unpin
406 {
407 match self.to_packet() {
408 Ok(body) => w.write_all(&body).await,
409 Err(e) => Err(e),
410 }
411 }
398 412
399 pub fn bytes_to_read(r: &[u8]) -> Result<BytesToRead, Error> { 413 pub fn bytes_to_read(r: &[u8]) -> Result<BytesToRead, Error> {
400 if r.len() < 5 { 414 if r.len() < 5 {
@@ -468,6 +482,35 @@ impl Value {
468 } 482 }
469 } 483 }
470 } 484 }
485
486 #[cfg(feature = "tokio")]
487 pub async fn read_packet_async<R>(r: &mut R) -> Result<Value, Error>
488 where
489 R: AsyncRead + Unpin
490 {
491 let mut buf = Vec::<u8>::new();
492 let mut fill = 0usize;
493 loop {
494 match Value::bytes_to_read(&buf) {
495 Ok(BytesToRead::Estimate(size)) => {
496 buf.resize(size, 0u8);
497 let amount = r.read(&mut buf[fill..]).await?;
498 fill += amount;
499 }
500 Ok(BytesToRead::Final(size)) => {
501 buf.resize(size, 0u8);
502 let amount = r.read(&mut buf[fill..]).await?;
503 fill += amount;
504 if fill == size {
505 return Value::from_packet(&buf);
506 }
507 }
508 Err(e) => {
509 return Err(e);
510 }
511 }
512 }
513 }
471} 514}
472 515
473impl From<i64> for Value { 516impl From<i64> for Value {
@@ -649,4 +692,10 @@ mod tests {
649 assert!(v == v2); 692 assert!(v == v2);
650 Ok(()) 693 Ok(())
651 } 694 }
695
696 #[cfg(feature = "tokio")]
697 #[test]
698 fn async_test() -> Result<(), Box<dyn Error>> {
699 Ok(())
700 }
652} 701}