mirror of
https://github.com/async-rs/async-std.git
synced 2025-04-24 01:06:46 +00:00
avoid boxing futures
This commit is contained in:
parent
b96afc41dc
commit
f5fa0d7e4e
1 changed files with 15 additions and 24 deletions
|
@ -4,6 +4,7 @@ use std::sync::Arc;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use kv_log_macro::trace;
|
use kv_log_macro::trace;
|
||||||
|
use pin_project_lite::pin_project;
|
||||||
|
|
||||||
use crate::io;
|
use crate::io;
|
||||||
use crate::task::{JoinHandle, Task, TaskLocalsWrapper};
|
use crate::task::{JoinHandle, Task, TaskLocalsWrapper};
|
||||||
|
@ -42,10 +43,7 @@ impl Builder {
|
||||||
let tag = TaskLocalsWrapper::new(task.clone());
|
let tag = TaskLocalsWrapper::new(task.clone());
|
||||||
|
|
||||||
// FIXME: do not require all futures to be boxed.
|
// FIXME: do not require all futures to be boxed.
|
||||||
SupportTaskLocals {
|
SupportTaskLocals { tag, future }
|
||||||
tag,
|
|
||||||
future: Box::pin(future),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Spawns a task with the configured settings.
|
/// Spawns a task with the configured settings.
|
||||||
|
@ -56,12 +54,6 @@ impl Builder {
|
||||||
{
|
{
|
||||||
let wrapped = self.build(future);
|
let wrapped = self.build(future);
|
||||||
|
|
||||||
// Log this `spawn` operation.
|
|
||||||
trace!("spawn", {
|
|
||||||
task_id: wrapped.tag.id().0,
|
|
||||||
parent_task_id: TaskLocalsWrapper::get_current(|t| t.id().0).unwrap_or(0),
|
|
||||||
});
|
|
||||||
|
|
||||||
let task = wrapped.tag.task().clone();
|
let task = wrapped.tag.task().clone();
|
||||||
let smol_task = smol::Task::spawn(wrapped).detach();
|
let smol_task = smol::Task::spawn(wrapped).detach();
|
||||||
|
|
||||||
|
@ -86,25 +78,24 @@ impl Builder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wrapper to add support for task locals.
|
pin_project! {
|
||||||
struct SupportTaskLocals<F> {
|
/// Wrapper to add support for task locals.
|
||||||
tag: TaskLocalsWrapper,
|
struct SupportTaskLocals<F> {
|
||||||
future: Pin<Box<F>>,
|
tag: TaskLocalsWrapper,
|
||||||
}
|
#[pin]
|
||||||
|
future: F,
|
||||||
impl<F> Drop for SupportTaskLocals<F> {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
// Log completion on exit.
|
|
||||||
trace!("completed", {
|
|
||||||
task_id: self.tag.id().0,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Future> Future for SupportTaskLocals<F> {
|
impl<F: Future> Future for SupportTaskLocals<F> {
|
||||||
type Output = F::Output;
|
type Output = F::Output;
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
unsafe { TaskLocalsWrapper::set_current(&self.tag, || Pin::new(&mut self.future).poll(cx)) }
|
unsafe {
|
||||||
|
TaskLocalsWrapper::set_current(&self.tag, || {
|
||||||
|
let this = self.project();
|
||||||
|
this.future.poll(cx)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue