From 84613b26b047d5dcd8bd36ba7479d3aa89349619 Mon Sep 17 00:00:00 2001 From: Lewin Bormann Date: Mon, 11 Sep 2023 18:12:02 +0200 Subject: [PATCH] for #32: conditionally add Send requirement for AsyncRead/Write --- Cargo.toml | 7 +++++-- src/reader.rs | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 24341c9..e708041 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,6 @@ bencher = "~0.1" [[example]] name = "encode_varint_from_stdin" -required-features = ["tokio_async"] [[example]] name = "read_write_file" @@ -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"] diff --git a/src/reader.rs b/src/reader.rs index 0ed63ac..b0ea6af 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -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`. /// @@ -66,7 +76,7 @@ impl VarIntProcessor { #[cfg(any(feature = "tokio_async", feature = "futures_async"))] #[async_trait::async_trait(?Send)] -impl VarIntAsyncReader for AR { +impl VarIntAsyncReader for AR { async fn read_varint_async(&mut self) -> Result { let mut buf = [0_u8; 1]; let mut p = VarIntProcessor::new::(); @@ -131,7 +141,7 @@ pub trait FixedIntAsyncReader { #[cfg(any(feature = "tokio_async", feature = "futures_async"))] #[async_trait::async_trait(?Send)] -impl FixedIntAsyncReader for AR { +impl FixedIntAsyncReader for AR { async fn read_fixedint_async(&mut self) -> Result { let mut buf = [0_u8; 8]; self.read_exact(&mut buf[0..std::mem::size_of::()])