Skip to content

Commit

Permalink
for #32: conditionally add Send requirement for AsyncRead/Write
Browse files Browse the repository at this point in the history
  • Loading branch information
dermesser committed Sep 11, 2023
1 parent 4f57046 commit 84613b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Expand Up @@ -20,7 +20,6 @@ bencher = "~0.1"

[[example]]
name = "encode_varint_from_stdin"
required-features = ["tokio_async"]

[[example]]
name = "read_write_file"
Expand All @@ -31,11 +30,15 @@ name = "main"
harness = false

[features]
default = ["sendable_io_traits"]

# Requires that AsyncRead and AsyncWrite implementors are Send.
sendable_io_traits = []
# Enable one of these features if you want to use the AsyncRead/AsyncWrite traits from
# the futures crate instead of those from tokio.
tokio_async = ["tokio", "async-trait"]
futures_async = ["futures-util", "async-trait"]

[package.metadata.docs.rs]
features = ["tokio_async"]
features = ["tokio_async", "sendable_io_traits"]

18 changes: 14 additions & 4 deletions src/reader.rs
Expand Up @@ -5,10 +5,20 @@ use crate::fixed::FixedInt;
use crate::varint::{VarInt, VarIntMaxSize, MSB};

#[cfg(feature = "tokio_async")]
use tokio::io::{AsyncRead, AsyncReadExt};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite};

#[cfg(feature = "futures_async")]
use futures_util::{io::AsyncRead, io::AsyncReadExt};
use futures_util::{io::AsyncRead, io::AsyncReadExt, AsyncWrite};

#[cfg(all(any(feature = "tokio_async", feature = "futures_async"), feature = "sendable_io_traits"))]
trait AsyncReader : AsyncRead + Unpin + Send {}
#[cfg(all(any(feature = "tokio_async", feature = "futures_async"), feature = "sendable_io_traits"))]
trait AsyncWriter : AsyncWrite + Unpin + Send {}

#[cfg(all(any(feature = "tokio_async", feature = "futures_async"), not(feature = "sendable_io_traits")))]
trait AsyncReader : AsyncRead + Unpin + Send {}
#[cfg(all(any(feature = "tokio_async", feature = "futures_async"), not(feature = "sendable_io_traits")))]
trait AsyncWriter : AsyncWrite + Unpin + Send {}

/// A trait for reading VarInts from any other `Reader`.
///
Expand Down Expand Up @@ -66,7 +76,7 @@ impl VarIntProcessor {

#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
#[async_trait::async_trait(?Send)]
impl<AR: AsyncRead + Unpin> VarIntAsyncReader for AR {
impl<AR: AsyncReader> VarIntAsyncReader for AR {
async fn read_varint_async<VI: VarInt>(&mut self) -> Result<VI> {
let mut buf = [0_u8; 1];
let mut p = VarIntProcessor::new::<VI>();
Expand Down Expand Up @@ -131,7 +141,7 @@ pub trait FixedIntAsyncReader {

#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
#[async_trait::async_trait(?Send)]
impl<AR: AsyncRead + Unpin> FixedIntAsyncReader for AR {
impl<AR: AsyncReader> FixedIntAsyncReader for AR {
async fn read_fixedint_async<FI: FixedInt>(&mut self) -> Result<FI> {
let mut buf = [0_u8; 8];
self.read_exact(&mut buf[0..std::mem::size_of::<FI>()])
Expand Down

0 comments on commit 84613b2

Please sign in to comment.