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..b788354 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -5,10 +5,32 @@ use crate::fixed::FixedInt; use crate::varint::{VarInt, VarIntMaxSize, MSB}; #[cfg(feature = "tokio_async")] -use tokio::io::{AsyncRead, AsyncReadExt}; - +use tokio::io::AsyncReadExt; #[cfg(feature = "futures_async")] -use futures_util::{io::AsyncRead, io::AsyncReadExt}; +use futures_util::io::AsyncReadExt; + +#[cfg(all(any(feature = "tokio_async", feature = "futures_async"), feature = "sendable_io_traits"))] +mod helper_traits { + #[cfg(feature = "tokio_async")] + use tokio::io::AsyncRead; + #[cfg(feature = "futures_async")] + use futures_util::io::AsyncRead; + pub(crate) trait AsyncReader: AsyncRead + Unpin + Send {} + impl AsyncReader for T where T : AsyncRead + Unpin + Send {} +} + +#[cfg(all(any(feature = "tokio_async", feature = "futures_async"), not(feature = "sendable_io_traits")))] +mod helper_traits { + #[cfg(feature = "tokio_async")] + use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite}; + #[cfg(feature = "futures_async")] + use futures_util::{io::AsyncRead, io::AsyncReadExt, AsyncWrite}; + pub(crate) trait AsyncReader: AsyncRead + Unpin {} + impl AsyncReader for T where T : AsyncRead + Unpin {} +} + +#[cfg(any(feature = "tokio_async", feature = "futures_async"))] +use helper_traits::AsyncReader; /// A trait for reading VarInts from any other `Reader`. /// @@ -66,7 +88,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 +153,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::()]) diff --git a/src/writer.rs b/src/writer.rs index 3671980..15c63f9 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -4,11 +4,34 @@ use crate::fixed::FixedInt; use crate::varint::VarInt; #[cfg(feature = "tokio_async")] -use tokio::io::{AsyncWrite, AsyncWriteExt}; +use tokio::io::AsyncWriteExt; #[cfg(feature = "futures_async")] use futures_util::{io::AsyncWrite, io::AsyncWriteExt}; +#[cfg(all(any(feature = "tokio_async", feature = "futures_async"), feature = "sendable_io_traits"))] +mod helper_traits { + #[cfg(feature = "tokio_async")] + use tokio::io::AsyncWrite; + #[cfg(feature = "futures_async")] + use futures_util::AsyncWrite; + pub(crate) trait AsyncWriter: AsyncWrite + Unpin + Send {} + impl AsyncWriter for T where T : AsyncWrite + Unpin + Send {} +} + +#[cfg(all(any(feature = "tokio_async", feature = "futures_async"), not(feature = "sendable_io_traits")))] +mod helper_traits { + #[cfg(feature = "tokio_async")] + use tokio::io::AsyncWrite; + #[cfg(feature = "futures_async")] + use futures_util::AsyncWrite; + pub(crate) trait AsyncWriter: AsyncWrite + Unpin {} + impl AsyncWriter for T where T : AsyncWrite + Unpin {} +} + +#[cfg(any(feature = "tokio_async", feature = "futures_async"))] +use helper_traits::AsyncWriter; + /// A trait for writing integers in VarInt encoding to any `Write` type. This packs encoding and /// writing into one step. pub trait VarIntWriter { @@ -25,7 +48,7 @@ pub trait VarIntAsyncWriter { #[cfg(any(feature = "tokio_async", feature = "futures_async"))] #[async_trait::async_trait(?Send)] -impl VarIntAsyncWriter for AW { +impl VarIntAsyncWriter for AW { async fn write_varint_async(&mut self, n: VI) -> Result { let mut buf = [0_u8; 10]; let b = n.encode_var(&mut buf); @@ -57,7 +80,7 @@ pub trait FixedIntAsyncWriter { #[cfg(any(feature = "tokio_async", feature = "futures_async"))] #[async_trait::async_trait(?Send)] -impl FixedIntAsyncWriter for AW { +impl FixedIntAsyncWriter for AW { async fn write_fixedint_async(&mut self, n: FI) -> Result { let mut buf = [0_u8; 8]; n.encode_fixed(&mut buf[..std::mem::size_of::()]);