Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #62 from raviqqe/raviqqe
Browse files Browse the repository at this point in the history
Make boxed generators compatible with futures-cpupool
  • Loading branch information
alexcrichton authored Feb 8, 2018
2 parents 8cb3098 + 3eb4f75 commit c02cb0a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ futures-await-await-macro = { path = "futures-await-await-macro", version = "0.1
futures = "0.1"

[dev-dependencies]
futures-cpupool = "0.1"
tokio-core = "0.1"
tokio-io = "0.1"
21 changes: 13 additions & 8 deletions futures-await-async-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,26 +225,31 @@ where F: FnOnce(&Type) -> proc_macro2::TokenStream
#[proc_macro_attribute]
pub fn async(attribute: TokenStream, function: TokenStream) -> TokenStream {
// Handle arguments to the #[async] attribute, if any
let attribute = attribute.to_string();
let boxed = if attribute == "( boxed )" {
true
} else if attribute == "" {
false
} else {
panic!("the #[async] attribute currently only takes `boxed` as an arg");
let (boxed, send) = match &attribute.to_string() as &str {
"( boxed )" => (true, false),
"( boxed_send )" => (true, true),
"" => (false, false),
_ => panic!("the #[async] attribute currently only takes `boxed` as an arg"),
};

async_inner(boxed, function, quote_cs! { ::futures::__rt::gen }, |output| {
// TODO: can we lift the restriction that `futures` must be at the root of
// the crate?
let output_span = first_last(&output);
let return_ty = if boxed {
let return_ty = if boxed && !send {
quote_cs! {
::futures::__rt::std::boxed::Box<::futures::Future<
Item = <! as ::futures::__rt::IsResult>::Ok,
Error = <! as ::futures::__rt::IsResult>::Err,
>>
}
} else if boxed && send {
quote_cs! {
::futures::__rt::std::boxed::Box<::futures::Future<
Item = <! as ::futures::__rt::IsResult>::Ok,
Error = <! as ::futures::__rt::IsResult>::Err,
> + Send>
}
} else {
// Dunno why this is buggy, hits weird typecheck errors in tests
//
Expand Down
13 changes: 13 additions & 0 deletions tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
#![feature(proc_macro, conservative_impl_trait, generators)]

extern crate futures_await as futures;
extern crate futures_cpupool;

use std::io;

use futures::prelude::*;
use futures_cpupool::CpuPool;

#[async]
fn foo() -> Result<i32, i32> {
Expand Down Expand Up @@ -58,6 +60,11 @@ fn _foo8(a: i32, b: i32) -> Result<i32, i32> {
return Ok(a + b)
}

#[async(boxed_send)]
fn _foo9() -> Result<(), ()> {
Ok(())
}

#[async]
fn _bar() -> Result<i32, i32> {
await!(foo())
Expand Down Expand Up @@ -234,3 +241,9 @@ fn poll_stream_after_error() {
assert_eq!(s.poll(), Err(()));
assert_eq!(s.poll(), Ok(Async::Ready(None)));
}

#[test]
fn run_boxed_future_in_cpu_pool() {
let pool = CpuPool::new_num_cpus();
pool.spawn(_foo9()).wait().unwrap();
}

0 comments on commit c02cb0a

Please sign in to comment.