diff options
Diffstat (limited to '')
| -rw-r--r-- | rust/Cargo.lock | 25 | ||||
| -rw-r--r-- | rust/Cargo.toml | 4 | ||||
| -rw-r--r-- | rust/src/lib.rs | 49 |
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 @@ | |||
| 3 | version = 4 | 3 | version = 4 |
| 4 | 4 | ||
| 5 | [[package]] | 5 | [[package]] |
| 6 | name = "bytes" | ||
| 7 | version = "1.11.0" | ||
| 8 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 9 | checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" | ||
| 10 | |||
| 11 | [[package]] | ||
| 6 | name = "gats" | 12 | name = "gats" |
| 7 | version = "0.1.0" | 13 | version = "0.1.0" |
| 14 | dependencies = [ | ||
| 15 | "tokio", | ||
| 16 | ] | ||
| 17 | |||
| 18 | [[package]] | ||
| 19 | name = "pin-project-lite" | ||
| 20 | version = "0.2.16" | ||
| 21 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 22 | checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" | ||
| 23 | |||
| 24 | [[package]] | ||
| 25 | name = "tokio" | ||
| 26 | version = "1.48.0" | ||
| 27 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 28 | checksum = "ff360e02eab121e0bc37a2d3b4d4dc622e6eda3a8e5253d5435ecf5bd4c68408" | ||
| 29 | dependencies = [ | ||
| 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" | |||
| 4 | edition = "2024" | 4 | edition = "2024" |
| 5 | 5 | ||
| 6 | [dependencies] | 6 | [dependencies] |
| 7 | tokio = { version = "1.48.0", optional = true, features = ["io-util"] } | ||
| 8 | |||
| 9 | [features] | ||
| 10 | 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; | |||
| 28 | use std::sync::LazyLock; | 28 | use std::sync::LazyLock; |
| 29 | use std::vec::Vec; | 29 | use std::vec::Vec; |
| 30 | 30 | ||
| 31 | #[cfg(feature = "tokio")] | ||
| 32 | use 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)] |
| 33 | pub enum Value { | 36 | pub 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 | ||
| 473 | impl From<i64> for Value { | 516 | impl 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 | } |
