Skip to content

Commit

Permalink
Support AsyncRead and AsyncWrite
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jan 5, 2019
1 parent f99a699 commit 5d39048
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ You can add support for external library by activating the each crate feature.

* [`futures::Stream`](https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.11/futures/stream/trait.Stream.html)
* [`futures::Sink`](https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.11/futures/sink/trait.Sink.html)
* [`futures::AsyncRead`](https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.11/futures/io/trait.AsyncRead.html)
* [`futures::AsyncWrite`](https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.11/futures/io/trait.AsyncWrite.html)

[`futures(v0.1)`](https://github.com/rust-lang-nursery/futures-rs) (*requires `"futures01"` crate feature*)

Expand Down
4 changes: 4 additions & 0 deletions derive/src/attribute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ lazy_static! {
external::futures::stream,
#[cfg(feature = "futures")]
external::futures::sink,
#[cfg(feature = "futures")]
external::futures::async_read,
#[cfg(feature = "futures")]
external::futures::async_write,
// futures01
#[cfg(feature = "futures01")]
external::futures01::future,
Expand Down
26 changes: 26 additions & 0 deletions derive/src/derive/external/futures/async_read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use proc_macro2::TokenStream;
use quote::quote;

use crate::utils::*;

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

pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
let root = std_root();
let io = quote!(::futures::io);

derive_trait!(
data,
parse_quote!(#io::AsyncRead)?,
parse_quote! {
trait AsyncRead {
#[inline]
unsafe fn initializer(&self) -> #io::Initializer;
#[inline]
fn poll_read(&mut self, lw: &#root::task::LocalWaker, buf: &mut [u8]) -> #root::task::Poll<#root::result::Result<usize, #io::Error>>;
#[inline]
fn poll_vectored_read(&mut self, lw: &#root::task::LocalWaker, vec: &mut [&mut #io::IoVec]) -> #root::task::Poll<#root::result::Result<usize, #io::Error>>;
}
}?,
)
}
28 changes: 28 additions & 0 deletions derive/src/derive/external/futures/async_write.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use proc_macro2::TokenStream;
use quote::quote;

use crate::utils::*;

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

pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
let root = std_root();
let io = quote!(::futures::io);

derive_trait!(
data,
parse_quote!(#io::AsyncWrite)?,
parse_quote! {
trait AsyncWrite {
#[inline]
fn poll_write(&mut self, lw: &#root::task::LocalWaker, buf: &[u8]) -> #root::task::Poll<#root::result::Result<usize, #io::Error>>;
#[inline]
fn poll_vectored_write(&mut self, lw: &#root::task::LocalWaker, vec: &[&#io::IoVec]) -> #root::task::Poll<#root::result::Result<usize, #io::Error>>;
#[inline]
fn poll_flush(&mut self, lw: &#root::task::LocalWaker) -> #root::task::Poll<#root::result::Result<(), #io::Error>>;
#[inline]
fn poll_close(&mut self, lw: &#root::task::LocalWaker) -> #root::task::Poll<#root::result::Result<(), #io::Error>>;
}
}?,
)
}
2 changes: 2 additions & 0 deletions derive/src/derive/external/futures/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub(crate) mod async_read;
pub(crate) mod async_write;
pub(crate) mod sink;
pub(crate) mod stream;
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,8 @@
//!
//! * [`futures::Stream`](https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.11/futures/stream/trait.Stream.html)
//! * [`futures::Sink`](https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.11/futures/sink/trait.Sink.html)
//! * [`futures::AsyncRead`](https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.11/futures/io/trait.AsyncRead.html)
//! * [`futures::AsyncWrite`](https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.11/futures/io/trait.AsyncWrite.html)
//!
//! [`futures(v0.1)`](https://github.com/rust-lang-nursery/futures-rs) (*requires `"futures01"` crate feature*)
//!
Expand Down
8 changes: 7 additions & 1 deletion test_suite/tests_2018/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ mod enum_derive {
#[cfg(feature = "unstable")]
#[test]
fn unstable() {
#[enum_derive(Future, futures::Stream, futures::Sink)]
#[enum_derive(
Future,
futures::Stream,
futures::Sink,
futures::AsyncRead,
futures::AsyncWrite
)]
enum Enum1<A, B> {
A(A),
B(B),
Expand Down

0 comments on commit 5d39048

Please sign in to comment.