Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for #32: conditionally add Send requirement for AsyncRead/Write #33

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
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"]

32 changes: 27 additions & 5 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,32 @@
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<T> 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};

Check warning on line 25 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (tokio_async, ubuntu-latest)

unused imports: `AsyncReadExt`, `AsyncWrite`

Check warning on line 25 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (tokio_async, ubuntu-latest)

unused imports: `AsyncReadExt`, `AsyncWrite`

Check warning on line 25 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (tokio_async, ubuntu-latest)

unused imports: `AsyncReadExt`, `AsyncWrite`

Check warning on line 25 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (tokio_async, ubuntu-latest)

unused imports: `AsyncReadExt`, `AsyncWrite`

Check warning on line 25 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (tokio_async, macos-latest)

unused imports: `AsyncReadExt`, `AsyncWrite`

Check warning on line 25 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (tokio_async, macos-latest)

unused imports: `AsyncReadExt`, `AsyncWrite`

Check warning on line 25 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (tokio_async, macos-latest)

unused imports: `AsyncReadExt`, `AsyncWrite`

Check warning on line 25 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (tokio_async, macos-latest)

unused imports: `AsyncReadExt`, `AsyncWrite`

Check warning on line 25 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (tokio_async, windows-latest)

unused imports: `AsyncReadExt`, `AsyncWrite`

Check warning on line 25 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (tokio_async, windows-latest)

unused imports: `AsyncReadExt`, `AsyncWrite`

Check warning on line 25 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (tokio_async, windows-latest)

unused imports: `AsyncReadExt`, `AsyncWrite`

Check warning on line 25 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (tokio_async, windows-latest)

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

Check warning on line 27 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, ubuntu-latest)

unused imports: `AsyncWrite`, `io::AsyncReadExt`

Check warning on line 27 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, ubuntu-latest)

unused imports: `AsyncWrite`, `io::AsyncReadExt`

Check warning on line 27 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, ubuntu-latest)

unused imports: `AsyncWrite`, `io::AsyncReadExt`

Check warning on line 27 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, ubuntu-latest)

unused imports: `AsyncWrite`, `io::AsyncReadExt`

Check warning on line 27 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, macos-latest)

unused imports: `AsyncWrite`, `io::AsyncReadExt`

Check warning on line 27 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, macos-latest)

unused imports: `AsyncWrite`, `io::AsyncReadExt`

Check warning on line 27 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, macos-latest)

unused imports: `AsyncWrite`, `io::AsyncReadExt`

Check warning on line 27 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, macos-latest)

unused imports: `AsyncWrite`, `io::AsyncReadExt`

Check warning on line 27 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, windows-latest)

unused imports: `AsyncWrite`, `io::AsyncReadExt`

Check warning on line 27 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, windows-latest)

unused imports: `AsyncWrite`, `io::AsyncReadExt`

Check warning on line 27 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, windows-latest)

unused imports: `AsyncWrite`, `io::AsyncReadExt`

Check warning on line 27 in src/reader.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, windows-latest)

unused imports: `AsyncWrite`, `io::AsyncReadExt`
pub(crate) trait AsyncReader: AsyncRead + Unpin {}
impl<T> 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`.
///
Expand Down Expand Up @@ -66,7 +88,7 @@

#[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 +153,7 @@

#[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
29 changes: 26 additions & 3 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@
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};

Check warning on line 10 in src/writer.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, ubuntu-latest)

unused import: `io::AsyncWrite`

Check warning on line 10 in src/writer.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, ubuntu-latest)

unused import: `io::AsyncWrite`

Check warning on line 10 in src/writer.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, ubuntu-latest)

unused import: `io::AsyncWrite`

Check warning on line 10 in src/writer.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, ubuntu-latest)

unused import: `io::AsyncWrite`

Check warning on line 10 in src/writer.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, macos-latest)

unused import: `io::AsyncWrite`

Check warning on line 10 in src/writer.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, macos-latest)

unused import: `io::AsyncWrite`

Check warning on line 10 in src/writer.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, macos-latest)

unused import: `io::AsyncWrite`

Check warning on line 10 in src/writer.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, macos-latest)

unused import: `io::AsyncWrite`

Check warning on line 10 in src/writer.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, windows-latest)

unused import: `io::AsyncWrite`

Check warning on line 10 in src/writer.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, windows-latest)

unused import: `io::AsyncWrite`

Check warning on line 10 in src/writer.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, windows-latest)

unused import: `io::AsyncWrite`

Check warning on line 10 in src/writer.rs

View workflow job for this annotation

GitHub Actions / integer-encoding-rs (futures_async, windows-latest)

unused import: `io::AsyncWrite`

#[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<T> 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<T> 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 {
Expand All @@ -25,7 +48,7 @@

#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
#[async_trait::async_trait(?Send)]
impl<AW: AsyncWrite + Unpin> VarIntAsyncWriter for AW {
impl<AW: AsyncWriter> VarIntAsyncWriter for AW {
async fn write_varint_async<VI: VarInt>(&mut self, n: VI) -> Result<usize> {
let mut buf = [0_u8; 10];
let b = n.encode_var(&mut buf);
Expand Down Expand Up @@ -57,7 +80,7 @@

#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
#[async_trait::async_trait(?Send)]
impl<AW: AsyncWrite + Unpin> FixedIntAsyncWriter for AW {
impl<AW: AsyncWriter> FixedIntAsyncWriter for AW {
async fn write_fixedint_async<FI: FixedInt>(&mut self, n: FI) -> Result<usize> {
let mut buf = [0_u8; 8];
n.encode_fixed(&mut buf[..std::mem::size_of::<FI>()]);
Expand Down
Loading