Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand All @@ -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"]
6 changes: 2 additions & 4 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<VI: VarInt>(&mut self) -> Result<VI>;
}
Expand Down Expand Up @@ -65,7 +65,6 @@ impl VarIntProcessor {
}

#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
#[async_trait::async_trait(?Send)]
impl<AR: AsyncRead + Unpin> VarIntAsyncReader for AR {
async fn read_varint_async<VI: VarInt>(&mut self) -> Result<VI> {
let mut buf = [0_u8; 1];
Expand Down Expand Up @@ -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<FI: FixedInt>(&mut self) -> Result<FI>;
}

#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
#[async_trait::async_trait(?Send)]
impl<AR: AsyncRead + Unpin> FixedIntAsyncReader for AR {
async fn read_fixedint_async<FI: FixedInt>(&mut self) -> Result<FI> {
let mut buf = [0_u8; 8];
Expand Down
6 changes: 2 additions & 4 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<VI: VarInt>(&mut self, n: VI) -> Result<usize>;
}

#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
#[async_trait::async_trait(?Send)]
impl<AW: AsyncWrite + Unpin> VarIntAsyncWriter for AW {
async fn write_varint_async<VI: VarInt>(&mut self, n: VI) -> Result<usize> {
let mut buf = [0_u8; 10];
Expand All @@ -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<FI: FixedInt>(&mut self, n: FI) -> Result<usize>;
}

#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
#[async_trait::async_trait(?Send)]
impl<AW: AsyncWrite + Unpin> FixedIntAsyncWriter for AW {
async fn write_fixedint_async<FI: FixedInt>(&mut self, n: FI) -> Result<usize> {
let mut buf = [0_u8; 8];
Expand Down
53 changes: 53 additions & 0 deletions tests/async_send.rs
Original file line number Diff line number Diff line change
@@ -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: Send>(_: F) {}

// Generic helpers in the style of ref-fvm's CAR code, which needed `Send` futures.
async fn read_len<R: AsyncRead + Unpin>(mut reader: &mut R) -> Result<usize> {
VarIntAsyncReader::read_varint_async(&mut reader).await
}

async fn write_len<W: AsyncWrite + Unpin>(writer: &mut W, bytes: &[u8]) -> Result<usize> {
writer.write_varint_async(bytes.len()).await?;
writer.write_fixedint_async(bytes.len() as u32).await
}

fn generic<R, W, I>(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::<I>());
assert_send(reader.read_fixedint_async::<I>());
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::<u64>());
assert_send(reader.read_fixedint_async::<u64>());
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);
}