Skip to content

Commit aa96f7e

Browse files
committed
feat: add tokio support for async decoding and optimize library functions with inline attributes
1 parent d314b54 commit aa96f7e

4 files changed

Lines changed: 385 additions & 6 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ serde = ["dep:serde"]
5454
zero-copy = ["bincode_derive-next?/zero-copy"]
5555
static-size = ["bincode_derive-next?/static-size"]
5656
async-fiber = ["dep:futures-io", "dep:libc", "std"]
57+
tokio = ["dep:tokio", "async-fiber"]
5758
# Remember to run 'MIRIFLAGS="-Zmiri-disable-isolation" cargo +nightly miri test --all-features --no-fail-fast'
5859
# And remeber 'sudo chrt -f 99 cargo +nightly bench --bench complex' also.
5960
# And for loom: 'RUSTFLAGS="--cfg loom" cargo test --test async_fiber_loom --features "async-fiber, tokio/full" --release'
@@ -67,6 +68,7 @@ pastey = "0.2"
6768
futures-io = { version = "0.3", optional = true }
6869
libc = { version = "0.2", optional = true }
6970
panic-halt = { version = "1.0", optional = true }
71+
tokio = { version = "1.50", default-features = false, optional = true }
7072

7173
# Used for tests
7274
[dev-dependencies]

src/de/async_fiber.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,3 +1038,24 @@ impl<R, F, T> Drop for BridgeFuture<R, F, T> {
10381038
}
10391039
}
10401040
}
1041+
1042+
/// A lightweight adapter to use `tokio::io::AsyncRead` with `AsyncFiberBridge`.
1043+
#[cfg(all(feature = "tokio", feature = "async-fiber"))]
1044+
pub struct TokioReader<R>(pub R);
1045+
1046+
#[cfg(all(feature = "tokio", feature = "async-fiber"))]
1047+
impl<R: tokio::io::AsyncRead + Unpin> futures_io::AsyncRead for TokioReader<R> {
1048+
#[inline]
1049+
fn poll_read(
1050+
mut self: Pin<&mut Self>,
1051+
cx: &mut Context<'_>,
1052+
buf: &mut [u8],
1053+
) -> Poll<std::io::Result<usize>> {
1054+
let mut read_buf = tokio::io::ReadBuf::new(buf);
1055+
match Pin::new(&mut self.0).poll_read(cx, &mut read_buf) {
1056+
| Poll::Ready(Ok(())) => Poll::Ready(Ok(read_buf.filled().len())),
1057+
| Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
1058+
| Poll::Pending => Poll::Pending,
1059+
}
1060+
}
1061+
}

src/features/serde/de_owned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::features::IoReader;
2222

2323
/// Serde decoder encapsulating an owned reader.
2424
pub struct OwnedSerdeDecoder<DE: Decoder> {
25-
pub(super) de: DE,
25+
pub(crate) de: DE,
2626
}
2727

2828
impl<DE: Decoder> OwnedSerdeDecoder<DE> {

0 commit comments

Comments
 (0)