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

Add support for tokio v1 #122

Merged
merged 1 commit into from
Dec 23, 2020
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](https://semver.org).

## [Unreleased]

* Add support for `tokio1::{AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead}`.

## [0.7.10] - 2020-11-15

* Documentation improvements.
Expand Down
17 changes: 10 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ futures = ["auto_enums_derive/futures"]
rayon = ["auto_enums_derive/rayon"]
# https://docs.rs/serde/1
serde = ["auto_enums_derive/serde"]
# https://docs.rs/tokio/1
tokio1 = ["auto_enums_derive/tokio1"]
# https://docs.rs/tokio/0.3
tokio03 = ["auto_enums_derive/tokio03"]
# https://docs.rs/tokio/0.2
Expand Down Expand Up @@ -87,13 +89,14 @@ rustversion = "1"
trybuild = "1"

# for `#[enum_derive]`
futures03_crate = { version = "0.3", package = "futures", default-features = false, features = ["std"] }
futures01_crate = { version = "0.1", package = "futures" }
tokio03_crate = { version = "0.3", package = "tokio" }
tokio02_crate = { version = "0.2", package = "tokio" }
tokio01_crate = { version = "0.1", package = "tokio" }
rayon_crate = { version = "1", package = "rayon" }
serde_crate = { version = "1", package = "serde" }
futures03_crate = { package = "futures", version = "0.3", default-features = false, features = ["std"] }
futures01_crate = { package = "futures", version = "0.1" }
tokio1_crate = { package = "tokio", version = "1" }
tokio03_crate = { package = "tokio", version = "0.3" }
tokio02_crate = { package = "tokio", version = "0.2" }
tokio01_crate = { package = "tokio", version = "0.1" }
rayon_crate = { package = "rayon", version = "1" }
serde_crate = { package = "serde", version = "1" }

# for benches
rand = "0.8"
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ traits to `#[derive]`.

`#[enum_derive]` supports many of the standard library traits and some popular
third-party libraries traits such as [rayon], [futures][futures03],
[tokio][tokio03]. **See [documentation](https://docs.rs/auto_enums/0.7/auto_enums/#supported-traits) for a complete list of supported traits.**
[tokio][tokio1]. **See [documentation](https://docs.rs/auto_enums/0.7/auto_enums/#supported-traits) for a complete list of supported traits.**

If you want to use traits that are not supported by `#[enum_derive]`, you
can use another crate that provides [derives macros][proc-macro-derive], or
Expand Down Expand Up @@ -140,19 +140,21 @@ enum Foo<A, B> {
* **`transpose_methods`**
* Enable to use `transpose*` methods.
* **`futures03`**
* Enable to use [futures 0.3][futures03] traits.
* Enable to use [futures v0.3][futures03] traits.
* **`futures01`**
* Enable to use [futures 0.1][futures01] traits.
* Enable to use [futures v0.1][futures01] traits.
* **`rayon`**
* Enable to use [rayon] traits.
* **`serde`**
* Enable to use [serde] traits.
* **`tokio1`**
* Enable to use [tokio v1][tokio1] traits.
* **`tokio03`**
* Enable to use [tokio 0.3][tokio03] traits.
* Enable to use [tokio v0.3][tokio03] traits.
* **`tokio02`**
* Enable to use [tokio 0.2][tokio02] traits.
* Enable to use [tokio v0.2][tokio02] traits.
* **`tokio01`**
* Enable to use [tokio 0.1][tokio01] traits.
* Enable to use [tokio v0.1][tokio01] traits.
* **`generator_trait`**
* Enable to use `[std|core]::ops::Generator` trait.
* Note that this feature is unstable and may cause incompatible changes between patch versions.
Expand Down Expand Up @@ -209,6 +211,7 @@ Please be careful if you return another traits with the same name.
[tokio01]: https://docs.rs/tokio/0.1
[tokio02]: https://docs.rs/tokio/0.2
[tokio03]: https://docs.rs/tokio/0.3
[tokio1]: https://docs.rs/tokio/1

## Related Projects

Expand Down
2 changes: 1 addition & 1 deletion benches/vs_boxed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![feature(test)]
#![warn(rust_2018_idioms, single_use_lifetimes)]
#![feature(test)]

extern crate test;

Expand Down
2 changes: 2 additions & 0 deletions derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ futures = ["futures03"]
rayon = []
# https://docs.rs/serde/1
serde = []
# https://docs.rs/tokio/1
tokio1 = []
# https://docs.rs/tokio/0.3
tokio03 = []
# https://docs.rs/tokio/0.2
Expand Down
3 changes: 3 additions & 0 deletions derive/src/derive/external/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ pub(crate) mod tokio02;
// https://docs.rs/tokio/0.3
#[cfg(feature = "tokio03")]
pub(crate) mod tokio03;
// https://docs.rs/tokio/1
#[cfg(feature = "tokio1")]
pub(crate) mod tokio1;
88 changes: 88 additions & 0 deletions derive/src/derive/external/tokio1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
pub(crate) mod async_buf_read {
use crate::derive::*;

pub(crate) const NAME: &[&str] = &["tokio1::AsyncBufRead"];

pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
Ok(derive_trait(data, parse_quote!(::tokio::io::AsyncBufRead), None, parse_quote! {
trait AsyncBufRead {
fn poll_fill_buf<'__a>(
self: ::core::pin::Pin<&'__a mut Self>,
cx: &mut ::core::task::Context<'_>,
) -> ::core::task::Poll<::std::io::Result<&'__a [u8]>>;
fn consume(self: ::core::pin::Pin<&mut Self>, amt: usize);
}
}))
}
}

pub(crate) mod async_read {
use crate::derive::*;

pub(crate) const NAME: &[&str] = &["tokio1::AsyncRead"];

pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
Ok(derive_trait(data, parse_quote!(::tokio::io::AsyncRead), None, parse_quote! {
trait AsyncRead {
fn poll_read(
self: ::core::pin::Pin<&mut Self>,
cx: &mut ::core::task::Context<'_>,
buf: &mut ::tokio::io::ReadBuf<'_>,
) -> ::core::task::Poll<::std::io::Result<()>>;
}
}))
}
}

pub(crate) mod async_seek {
use crate::derive::*;

pub(crate) const NAME: &[&str] = &["tokio1::AsyncSeek"];

pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
Ok(derive_trait(data, parse_quote!(::tokio::io::AsyncSeek), None, parse_quote! {
trait AsyncSeek {
fn start_seek(
self: ::core::pin::Pin<&mut Self>,
pos: ::std::io::SeekFrom,
) -> ::std::io::Result<()>;
fn poll_complete(
self: ::core::pin::Pin<&mut Self>,
cx: &mut ::core::task::Context<'_>,
) -> ::core::task::Poll<::std::io::Result<u64>>;
}
}))
}
}

pub(crate) mod async_write {
use crate::derive::*;

pub(crate) const NAME: &[&str] = &["tokio1::AsyncWrite"];

pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
Ok(derive_trait(data, parse_quote!(::tokio::io::AsyncWrite), None, parse_quote! {
trait AsyncWrite {
fn poll_write(
self: ::core::pin::Pin<&mut Self>,
cx: &mut ::core::task::Context<'_>,
buf: &[u8],
) -> ::core::task::Poll<::std::io::Result<usize>>;
fn poll_flush(
self: ::core::pin::Pin<&mut Self>,
cx: &mut ::core::task::Context<'_>,
) -> ::core::task::Poll<::std::io::Result<()>>;
fn poll_shutdown(
self: ::core::pin::Pin<&mut Self>,
cx: &mut ::core::task::Context<'_>,
) -> ::core::task::Poll<::std::io::Result<()>>;
fn poll_write_vectored(
self: ::core::pin::Pin<&mut Self>,
cx: &mut ::core::task::Context<'_>,
bufs: &[::std::io::IoSlice<'_>],
) -> ::core::task::Poll<::std::io::Result<usize>>;
fn is_write_vectored(&self) -> bool;
}
}))
}
}
11 changes: 9 additions & 2 deletions derive/src/enum_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ fn get_derive(s: &str) -> Option<DeriveFn> {
// serde
#[cfg(feature = "serde")]
external::serde::serialize,
// tokio1
#[cfg(feature = "tokio1")]
external::tokio1::async_read,
#[cfg(feature = "tokio1")]
external::tokio1::async_write,
#[cfg(feature = "tokio1")]
external::tokio1::async_seek,
#[cfg(feature = "tokio1")]
external::tokio1::async_buf_read,
// tokio03
#[cfg(feature = "tokio03")]
external::tokio03::async_read,
Expand Down Expand Up @@ -170,8 +179,6 @@ impl Parse for Args {
}
}

// https://github.com/rust-lang/rust-clippy/issues/6384
#[allow(clippy::unnecessary_wraps)]
fn get_trait_deps(s: &str) -> Option<&'static [&'static str]> {
Some(match s {
"Copy" => &["Clone"],
Expand Down
30 changes: 20 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@
//! }
//! ```
//!
//! ### [futures 0.3][futures03] *(requires `"futures03"` or `"futures"` crate feature)*
//! ### [futures v0.3][futures03] *(requires `"futures03"` or `"futures"` crate feature)*
//!
//! * [`futures03::Stream`](https://docs.rs/futures/0.3/futures/stream/trait.Stream.html) - [generated code](https://github.com/taiki-e/auto_enums/blob/master/docs/supported_traits/external/futures/stream.md)
//! * [`futures03::Sink`](https://docs.rs/futures/0.3/futures/sink/trait.Sink.html) - [generated code](https://github.com/taiki-e/auto_enums/blob/master/docs/supported_traits/external/futures/sink.md)
Expand All @@ -690,7 +690,7 @@
//!
//! *See also [futures-enum] crate.*
//!
//! ### [futures 0.1][futures01] *(requires `"futures01"` crate feature)*
//! ### [futures v0.1][futures01] *(requires `"futures01"` crate feature)*
//!
//! * [`futures01::Future`](https://docs.rs/futures/0.1/futures/future/trait.Future.html)
//! * [`futures01::Stream`](https://docs.rs/futures/0.1/futures/stream/trait.Stream.html)
Expand All @@ -706,21 +706,28 @@
//!
//! * [`serde::Serialize`](https://docs.rs/serde/1/serde/trait.Serialize.html) - [generated code](https://github.com/taiki-e/auto_enums/blob/master/docs/supported_traits/external/serde/serialize.md)
//!
//! ### [tokio 0.3][tokio03] *(requires `"tokio03"` crate feature)*
//! ### [tokio v1][tokio1] *(requires `"tokio1"` crate feature)*
//!
//! * [`tokio1::AsyncRead`](https://docs.rs/tokio/1/tokio/io/trait.AsyncRead.html)
//! * [`tokio1::AsyncWrite`](https://docs.rs/tokio/1/tokio/io/trait.AsyncWrite.html)
//! * [`tokio1::AsyncSeek`](https://docs.rs/tokio/1/tokio/io/trait.AsyncSeek.html)
//! * [`tokio1::AsyncBufRead`](https://docs.rs/tokio/1/tokio/io/trait.AsyncBufRead.html)
//!
//! ### [tokio v0.3][tokio03] *(requires `"tokio03"` crate feature)*
//!
//! * [`tokio03::AsyncRead`](https://docs.rs/tokio/0.3/tokio/io/trait.AsyncRead.html)
//! * [`tokio03::AsyncWrite`](https://docs.rs/tokio/0.3/tokio/io/trait.AsyncWrite.html)
//! * [`tokio03::AsyncSeek`](https://docs.rs/tokio/0.3/tokio/io/trait.AsyncSeek.html)
//! * [`tokio03::AsyncBufRead`](https://docs.rs/tokio/0.3/tokio/io/trait.AsyncBufRead.html)
//!
//! ### [tokio 0.2][tokio02] *(requires `"tokio02"` crate feature)*
//! ### [tokio v0.2][tokio02] *(requires `"tokio02"` crate feature)*
//!
//! * [`tokio02::AsyncRead`](https://docs.rs/tokio/0.2/tokio/io/trait.AsyncRead.html)
//! * [`tokio02::AsyncWrite`](https://docs.rs/tokio/0.2/tokio/io/trait.AsyncWrite.html)
//! * [`tokio02::AsyncSeek`](https://docs.rs/tokio/0.2/tokio/io/trait.AsyncSeek.html)
//! * [`tokio02::AsyncBufRead`](https://docs.rs/tokio/0.2/tokio/io/trait.AsyncBufRead.html)
//!
//! ### [tokio 0.1][tokio01] *(requires `"tokio01"` crate feature)*
//! ### [tokio v0.1][tokio01] *(requires `"tokio01"` crate feature)*
//!
//! * [`tokio01::AsyncRead`](https://docs.rs/tokio/0.1/tokio/io/trait.AsyncRead.html)
//! * [`tokio01::AsyncWrite`](https://docs.rs/tokio/0.1/tokio/io/trait.AsyncWrite.html)
Expand Down Expand Up @@ -767,19 +774,21 @@
//! * **`transpose_methods`**
//! * Enable to use `transpose*` methods.
//! * **`futures03`**
//! * Enable to use [futures 0.3][futures03] traits.
//! * Enable to use [futures v0.3][futures03] traits.
//! * **`futures01`**
//! * Enable to use [futures 0.1][futures01] traits.
//! * Enable to use [futures v0.1][futures01] traits.
//! * **`rayon`**
//! * Enable to use [rayon] traits.
//! * **`serde`**
//! * Enable to use [serde] traits.
//! * **`tokio1`**
//! * Enable to use [tokio v1][tokio1] traits.
//! * **`tokio03`**
//! * Enable to use [tokio 0.3][tokio03] traits.
//! * Enable to use [tokio v0.3][tokio03] traits.
//! * **`tokio02`**
//! * Enable to use [tokio 0.2][tokio02] traits.
//! * Enable to use [tokio v0.2][tokio02] traits.
//! * **`tokio01`**
//! * Enable to use [tokio 0.1][tokio01] traits.
//! * Enable to use [tokio v0.1][tokio01] traits.
//! * **`generator_trait`**
//! * Enable to use `[std|core]::ops::Generator` trait.
//! * Note that this feature is unstable and may cause incompatible changes between patch versions.
Expand Down Expand Up @@ -839,6 +848,7 @@
//! [tokio01]: https://docs.rs/tokio/0.1
//! [tokio02]: https://docs.rs/tokio/0.2
//! [tokio03]: https://docs.rs/tokio/0.3
//! [tokio1]: https://docs.rs/tokio/1

#![no_std]
#![doc(test(
Expand Down
1 change: 1 addition & 0 deletions tests/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
feature = "tokio01",
feature = "tokio02",
feature = "tokio03",
feature = "tokio1",
))]
#![warn(rust_2018_idioms, single_use_lifetimes)]

Expand Down
11 changes: 11 additions & 0 deletions tests/ui/external/tokio1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern crate tokio1_crate as tokio;

use auto_enums::enum_derive;

#[enum_derive(tokio1::AsyncRead, tokio1::AsyncWrite, tokio1::AsyncSeek, tokio1::AsyncBufRead)]
enum Tokio1<A, B> {
A(A),
B(B),
}

fn main() {}