diff --git a/Cargo.toml b/Cargo.toml index 907e42e..713e6c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,6 @@ keywords = ["integer", "varint", "zigzag", "protobuf", "serialize"] edition = "2018" [dependencies] -async-trait = { version = "0.1", optional = true } tokio = { version = "1.0", features = ["io-util"], optional = true } futures-util = { version = "0.3", optional = true, features = ["io"] } @@ -33,8 +32,8 @@ harness = false [features] # 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"] +tokio_async = ["tokio"] +futures_async = ["futures-util"] [package.metadata.docs.rs] features = ["tokio_async"] diff --git a/src/reader.rs b/src/reader.rs index bcdce34..9fb99ba 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -25,7 +25,7 @@ pub trait VarIntReader { #[cfg(any(feature = "tokio_async", feature = "futures_async"))] /// Like a VarIntReader, but returns a future. -#[async_trait::async_trait(?Send)] +#[allow(async_fn_in_trait)] pub trait VarIntAsyncReader { async fn read_varint_async(&mut self) -> Result; } @@ -65,7 +65,6 @@ impl VarIntProcessor { } #[cfg(any(feature = "tokio_async", feature = "futures_async"))] -#[async_trait::async_trait(?Send)] impl VarIntAsyncReader for AR { async fn read_varint_async(&mut self) -> Result { let mut buf = [0_u8; 1]; @@ -124,13 +123,12 @@ pub trait FixedIntReader { /// Like FixedIntReader, but returns a future. #[cfg(any(feature = "tokio_async", feature = "futures_async"))] -#[async_trait::async_trait(?Send)] +#[allow(async_fn_in_trait)] pub trait FixedIntAsyncReader { async fn read_fixedint_async(&mut self) -> Result; } #[cfg(any(feature = "tokio_async", feature = "futures_async"))] -#[async_trait::async_trait(?Send)] impl FixedIntAsyncReader for AR { async fn read_fixedint_async(&mut self) -> Result { let mut buf = [0_u8; 8]; diff --git a/src/writer.rs b/src/writer.rs index e84c6d0..5b313f8 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -17,14 +17,13 @@ pub trait VarIntWriter { /// Like VarIntWriter, but asynchronous. #[cfg(any(feature = "tokio_async", feature = "futures_async"))] -#[async_trait::async_trait(?Send)] +#[allow(async_fn_in_trait)] pub trait VarIntAsyncWriter { /// Write a VarInt integer to an asynchronous writer. async fn write_varint_async(&mut self, n: VI) -> Result; } #[cfg(any(feature = "tokio_async", feature = "futures_async"))] -#[async_trait::async_trait(?Send)] impl VarIntAsyncWriter for AW { async fn write_varint_async(&mut self, n: VI) -> Result { let mut buf = [0_u8; 10]; @@ -50,13 +49,12 @@ pub trait FixedIntWriter { } #[cfg(any(feature = "tokio_async", feature = "futures_async"))] -#[async_trait::async_trait(?Send)] +#[allow(async_fn_in_trait)] pub trait FixedIntAsyncWriter { async fn write_fixedint_async(&mut self, n: FI) -> Result; } #[cfg(any(feature = "tokio_async", feature = "futures_async"))] -#[async_trait::async_trait(?Send)] impl FixedIntAsyncWriter for AW { async fn write_fixedint_async(&mut self, n: FI) -> Result { let mut buf = [0_u8; 8]; diff --git a/tests/async_send.rs b/tests/async_send.rs new file mode 100644 index 0000000..20e29b4 --- /dev/null +++ b/tests/async_send.rs @@ -0,0 +1,53 @@ +//! The futures returned by the async traits must be `Send` whenever the reader or writer is. +#![cfg(any(feature = "tokio_async", feature = "futures_async"))] + +use std::io::Result; + +#[cfg(feature = "tokio_async")] +use tokio::io::{AsyncRead, AsyncWrite}; + +#[cfg(feature = "futures_async")] +use futures_util::io::{AsyncRead, AsyncWrite}; + +use integer_encoding::*; + +fn assert_send(_: F) {} + +// Generic helpers in the style of ref-fvm's CAR code, which needed `Send` futures. +async fn read_len(mut reader: &mut R) -> Result { + VarIntAsyncReader::read_varint_async(&mut reader).await +} + +async fn write_len(writer: &mut W, bytes: &[u8]) -> Result { + writer.write_varint_async(bytes.len()).await?; + writer.write_fixedint_async(bytes.len() as u32).await +} + +fn generic(reader: &mut R, writer: &mut W, n: I) +where + R: AsyncRead + Unpin + Send, + W: AsyncWrite + Unpin + Send, + I: VarInt + FixedInt + Send, +{ + assert_send(reader.read_varint_async::()); + assert_send(reader.read_fixedint_async::()); + assert_send(writer.write_varint_async(n)); + assert_send(writer.write_fixedint_async(n)); + assert_send(read_len(reader)); + assert_send(write_len(writer, &[1, 2, 3])); +} + +#[test] +fn test_async_futures_are_send() { + let mut reader: &[u8] = &[]; + let mut writer = Vec::new(); + + assert_send(reader.read_varint_async::()); + assert_send(reader.read_fixedint_async::()); + assert_send(writer.write_varint_async(1_u64)); + assert_send(writer.write_fixedint_async(1_u64)); + assert_send(read_len(&mut reader)); + assert_send(write_len(&mut writer, &[1, 2, 3])); + + generic(&mut reader, &mut writer, 1_u64); +}