Skip to content

Commit 86d6917

Browse files
bors[bot]taiki-e
andauthored
Merge #37
37: Remove poll*with_context functions r=taiki-e a=taiki-e Based on rust-lang/rust#70831 Co-authored-by: Taiki Endo <[email protected]>
2 parents 4d5dd70 + 40cad72 commit 86d6917

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

futures-async-stream-macro/src/visitor.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ impl Visitor {
107107
}
108108
Stream | TryStream => {
109109
quote! {
110-
match unsafe { ::futures_async_stream::stream::poll_next_with_context(
110+
match unsafe { ::futures_async_stream::stream::Stream::poll_next(
111111
::futures_async_stream::reexport::pin::Pin::as_mut(&mut __pinned),
112-
__task_context,
112+
::futures_async_stream::future::get_context(__task_context),
113113
) } {
114114
::futures_async_stream::reexport::task::Poll::Ready(
115115
::futures_async_stream::reexport::option::Option::Some(e),
@@ -192,19 +192,31 @@ impl Visitor {
192192
return;
193193
}
194194

195+
// Desugar `<base>.await` into:
196+
//
197+
// {
198+
// let mut __pinned = <base>;
199+
// loop {
200+
// if let Poll::Ready(result) = unsafe { Future::poll(
201+
// Pin::new_unchecked(&mut __pinned),
202+
// get_context(__task_context),
203+
// ) } {
204+
// break result;
205+
// }
206+
// __task_context = yield Poll::Pending;
207+
// }
208+
// }
195209
if let Expr::Await(ExprAwait { base, await_token, .. }) = expr {
196210
*expr = syn::parse2(quote_spanned! { await_token.span() => {
197211
let mut __pinned = #base;
198212
loop {
199-
if let ::futures_async_stream::reexport::task::Poll::Ready(x) =
200-
unsafe { ::futures_async_stream::future::poll_with_context(
201-
::futures_async_stream::reexport::pin::Pin::new_unchecked(&mut __pinned),
202-
__task_context,
203-
)}
204-
{
205-
break x;
213+
if let ::futures_async_stream::reexport::task::Poll::Ready(result) =
214+
unsafe { ::futures_async_stream::future::Future::poll(
215+
::futures_async_stream::reexport::pin::Pin::new_unchecked(&mut __pinned),
216+
::futures_async_stream::future::get_context(__task_context),
217+
) } {
218+
break result;
206219
}
207-
208220
__task_context = yield ::futures_async_stream::reexport::task::Poll::Pending;
209221
}
210222
}})

src/lib.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,16 @@ pub use futures_async_stream_macro::async_try_stream_block;
234234
#[doc(hidden)]
235235
pub mod future {
236236
use core::{
237-
future::Future,
238237
ops::{Generator, GeneratorState},
239238
pin::Pin,
240239
ptr::NonNull,
241240
task::{Context, Poll},
242241
};
243242
use pin_project::pin_project;
244243

244+
#[doc(hidden)]
245+
pub use core::future::Future;
246+
245247
/// This type is needed because:
246248
///
247249
/// a) Generators cannot implement `for<'a, 'b> Generator<&'a mut Context<'b>>`, so we need to pass
@@ -293,11 +295,8 @@ pub mod future {
293295
}
294296

295297
#[doc(hidden)]
296-
pub unsafe fn poll_with_context<F>(f: Pin<&mut F>, mut cx: ResumeTy) -> Poll<F::Output>
297-
where
298-
F: Future,
299-
{
300-
F::poll(f, cx.0.as_mut())
298+
pub unsafe fn get_context<'a, 'b>(cx: ResumeTy) -> &'a mut Context<'b> {
299+
&mut *cx.0.as_ptr().cast()
301300
}
302301
}
303302

@@ -312,11 +311,13 @@ pub mod stream {
312311
ptr::NonNull,
313312
task::{Context, Poll},
314313
};
315-
use futures_core::stream::Stream;
316314
use pin_project::pin_project;
317315

318316
use super::future::ResumeTy;
319317

318+
#[doc(hidden)]
319+
pub use futures_core::stream::Stream;
320+
320321
/// Wrap a generator in a stream.
321322
///
322323
/// This function returns a `GenStream` underneath, but hides it in `impl Trait` to give
@@ -352,17 +353,6 @@ pub mod stream {
352353
}
353354
}
354355

355-
#[doc(hidden)]
356-
pub unsafe fn poll_next_with_context<S>(
357-
s: Pin<&mut S>,
358-
mut cx: ResumeTy,
359-
) -> Poll<Option<S::Item>>
360-
where
361-
S: Stream,
362-
{
363-
S::poll_next(s, cx.0.as_mut())
364-
}
365-
366356
// This is equivalent to the `futures::stream::StreamExt::next` method.
367357
// But we want to make this crate dependency as small as possible, so we define our `next` function.
368358
#[doc(hidden)]

0 commit comments

Comments
 (0)