mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-07 00:00:09 +02:00
tokio: replace some futures with poll_fn (#7895)
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
use bytes::BufMut;
|
||||
use std::future::Future;
|
||||
use std::future::poll_fn;
|
||||
use std::io;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
use tokio::io::AsyncRead;
|
||||
|
||||
/// Read data from an `AsyncRead` into an implementer of the [`BufMut`] trait.
|
||||
@@ -46,20 +45,5 @@ where
|
||||
R: AsyncRead + Unpin,
|
||||
B: BufMut,
|
||||
{
|
||||
return ReadBufFn(read, buf).await;
|
||||
|
||||
struct ReadBufFn<'a, R, B>(&'a mut R, &'a mut B);
|
||||
|
||||
impl<'a, R, B> Future for ReadBufFn<'a, R, B>
|
||||
where
|
||||
R: AsyncRead + Unpin,
|
||||
B: BufMut,
|
||||
{
|
||||
type Output = io::Result<usize>;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let this = &mut *self;
|
||||
crate::util::poll_read_buf(Pin::new(this.0), cx, this.1)
|
||||
}
|
||||
}
|
||||
poll_fn(|cx| crate::util::poll_read_buf(Pin::new(read), cx, buf)).await
|
||||
}
|
||||
|
||||
+2
-17
@@ -559,10 +559,6 @@ cfg_time! {
|
||||
}
|
||||
|
||||
mod trace {
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
cfg_taskdump! {
|
||||
pub(crate) use crate::runtime::task::trace::trace_leaf;
|
||||
}
|
||||
@@ -576,19 +572,8 @@ mod trace {
|
||||
}
|
||||
|
||||
#[cfg_attr(not(feature = "sync"), allow(dead_code))]
|
||||
pub(crate) fn async_trace_leaf() -> impl Future<Output = ()> {
|
||||
struct Trace;
|
||||
|
||||
impl Future for Trace {
|
||||
type Output = ();
|
||||
|
||||
#[inline(always)]
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
|
||||
trace_leaf(cx)
|
||||
}
|
||||
}
|
||||
|
||||
Trace
|
||||
pub(crate) async fn async_trace_leaf() {
|
||||
std::future::poll_fn(trace_leaf).await
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+14
-24
@@ -1,8 +1,7 @@
|
||||
use crate::runtime::context;
|
||||
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::task::{ready, Context, Poll};
|
||||
use std::future::poll_fn;
|
||||
use std::task::{ready, Poll};
|
||||
|
||||
/// Yields execution back to the Tokio runtime.
|
||||
///
|
||||
@@ -37,28 +36,19 @@ use std::task::{ready, Context, Poll};
|
||||
/// [`tokio::select!`]: macro@crate::select
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "rt")))]
|
||||
pub async fn yield_now() {
|
||||
/// Yield implementation
|
||||
struct YieldNow {
|
||||
yielded: bool,
|
||||
}
|
||||
let mut yielded = false;
|
||||
poll_fn(|cx| {
|
||||
ready!(crate::trace::trace_leaf(cx));
|
||||
|
||||
impl Future for YieldNow {
|
||||
type Output = ();
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
|
||||
ready!(crate::trace::trace_leaf(cx));
|
||||
|
||||
if self.yielded {
|
||||
return Poll::Ready(());
|
||||
}
|
||||
|
||||
self.yielded = true;
|
||||
|
||||
context::defer(cx.waker());
|
||||
|
||||
Poll::Pending
|
||||
if yielded {
|
||||
return Poll::Ready(());
|
||||
}
|
||||
}
|
||||
|
||||
YieldNow { yielded: false }.await;
|
||||
yielded = true;
|
||||
|
||||
context::defer(cx.waker());
|
||||
|
||||
Poll::Pending
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user