Skip to content
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
7 changes: 5 additions & 2 deletions futures-async-stream-macro/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub(super) fn make_gen_body(
#[allow(unreachable_code)]
{
return #ret_value;
loop { yield ::futures_async_stream::reexport::task::Poll::Pending }
loop { __task_context = yield ::futures_async_stream::reexport::task::Poll::Pending }
}
};
let mut gen_body = TokenStream::new();
Expand All @@ -299,7 +299,10 @@ pub(super) fn make_gen_body(
});

quote! {
#gen_function(static move || -> #ret_ty #gen_body)
#gen_function(
static move |mut __task_context: ::futures_async_stream::future::ResumeTy| -> #ret_ty
#gen_body
)
}
}

Expand Down
13 changes: 0 additions & 13 deletions futures-async-stream-macro/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,6 @@ where
*this = f(mem::replace(this, Expr::Verbatim(TokenStream::new())));
}

pub(super) fn replace_boxed_expr<F>(expr: &mut Option<Box<Expr>>, f: F)
where
F: FnOnce(Expr) -> Expr,
{
if expr.is_none() {
expr.replace(Box::new(unit()));
}

if let Some(expr) = expr {
replace_expr(&mut **expr, f);
}
}

macro_rules! error {
($span:expr, $msg:expr) => {
syn::Error::new_spanned($span, $msg)
Expand Down
36 changes: 21 additions & 15 deletions futures-async-stream-macro/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use syn::{

use crate::{
async_stream_block, async_try_stream_block,
utils::{expr_compile_error, replace_boxed_expr, replace_expr},
utils::{expr_compile_error, replace_expr, unit},
};

pub(crate) use Scope::{Closure, Future, Stream, TryStream};
Expand Down Expand Up @@ -88,17 +88,18 @@ impl Visitor {
}
Stream | TryStream => {
quote! {
match ::futures_async_stream::stream::poll_next_with_tls_context(
match unsafe { ::futures_async_stream::stream::poll_next_with_context(
::futures_async_stream::reexport::pin::Pin::as_mut(&mut __pinned),
) {
__task_context,
) } {
::futures_async_stream::reexport::task::Poll::Ready(
::futures_async_stream::reexport::option::Option::Some(e),
) => e,
::futures_async_stream::reexport::task::Poll::Ready(
::futures_async_stream::reexport::option::Option::None,
) => break,
::futures_async_stream::reexport::task::Poll::Pending => {
yield ::futures_async_stream::reexport::task::Poll::Pending;
__task_context = yield ::futures_async_stream::reexport::task::Poll::Pending;
continue
}
}
Expand Down Expand Up @@ -129,17 +130,21 @@ impl Visitor {
}

/// Visits `yield <expr>` in `async_stream` scope.
fn visit_yield(&self, expr: &mut ExprYield) {
fn visit_yield(&self, expr: &mut Expr) {
if !self.scope.is_stream() {
return;
}

// Desugar `yield <expr>` into `yield Poll::Ready(<expr>)`.
replace_boxed_expr(&mut expr.expr, |expr| {
syn::parse_quote! {
::futures_async_stream::reexport::task::Poll::Ready(#expr)
// Desugar `yield <expr>` into `__task_context = yield Poll::Ready(<expr>)`.
if let Expr::Yield(ExprYield { yield_token, expr: e, .. }) = expr {
if e.is_none() {
e.replace(Box::new(unit()));
}
});

*expr = syn::parse_quote!(
__task_context = #yield_token ::futures_async_stream::reexport::task::Poll::Ready(#e)
);
}
}

/// Visits `async_stream_block!` macro.
Expand Down Expand Up @@ -176,14 +181,15 @@ impl Visitor {
let mut __pinned = #base;
loop {
if let ::futures_async_stream::reexport::task::Poll::Ready(x) =
::futures_async_stream::future::poll_with_tls_context(unsafe {
::futures_async_stream::reexport::pin::Pin::new_unchecked(&mut __pinned)
})
unsafe { ::futures_async_stream::future::poll_with_context(
::futures_async_stream::reexport::pin::Pin::new_unchecked(&mut __pinned),
__task_context,
)}
{
break x;
}

yield ::futures_async_stream::reexport::task::Poll::Pending
__task_context = yield ::futures_async_stream::reexport::task::Poll::Pending;
}
}})
.unwrap();
Expand Down Expand Up @@ -211,7 +217,7 @@ impl VisitMut for Visitor {

visit_mut::visit_expr_mut(self, expr);
match expr {
Expr::Yield(expr) => self.visit_yield(expr),
Expr::Yield(_) => self.visit_yield(expr),
Expr::Await(_) => self.visit_await(expr),
Expr::ForLoop(_) => self.visit_for_loop(expr),
Expr::Macro(_) => self.visit_macro(expr),
Expand Down
Loading