summaryrefslogtreecommitdiff
path: root/core/ops.rs
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-06-14 16:22:54 -0600
committerGitHub <noreply@github.com>2023-06-14 16:22:54 -0600
commit48c6f7178703d448da229a5baf19efb403416da0 (patch)
tree176fb4b767cfe71d18d2f24d9d8ec0fa0f4bf171 /core/ops.rs
parentfc4e4c3e93c337ae2b549cf618f69c87a9647a4f (diff)
refactor(core): Remove MaybeDone from ops to eventually remove the box (#19508)
This removes MaybeDone from op resolution. While it would be nice to avoid the box, most of the work for that future task is done here.
Diffstat (limited to 'core/ops.rs')
-rw-r--r--core/ops.rs46
1 files changed, 8 insertions, 38 deletions
diff --git a/core/ops.rs b/core/ops.rs
index 372ffe5b2..a5c76e412 100644
--- a/core/ops.rs
+++ b/core/ops.rs
@@ -9,16 +9,13 @@ use crate::runtime::JsRuntimeState;
use crate::OpDecl;
use crate::OpsTracker;
use anyhow::Error;
-use futures::future::MaybeDone;
use futures::task::AtomicWaker;
use futures::Future;
-use futures::FutureExt;
use pin_project::pin_project;
use serde::Serialize;
use std::cell::RefCell;
use std::ops::Deref;
use std::ops::DerefMut;
-use std::pin::Pin;
use std::ptr::NonNull;
use std::rc::Rc;
use std::rc::Weak;
@@ -30,40 +27,26 @@ pub type PromiseId = i32;
pub type OpId = u16;
#[pin_project]
-pub struct OpCall {
+pub struct OpCall<F: Future<Output = OpResult>> {
promise_id: PromiseId,
op_id: OpId,
/// Future is not necessarily Unpin, so we need to pin_project.
#[pin]
- fut: MaybeDone<Pin<Box<dyn Future<Output = OpResult>>>>,
+ fut: F,
}
-impl OpCall {
+impl<F: Future<Output = OpResult>> OpCall<F> {
/// Wraps a future; the inner future is polled the usual way (lazily).
- pub fn pending(
- op_ctx: &OpCtx,
- promise_id: PromiseId,
- fut: Pin<Box<dyn Future<Output = OpResult> + 'static>>,
- ) -> Self {
- Self {
- op_id: op_ctx.id,
- promise_id,
- fut: MaybeDone::Future(fut),
- }
- }
-
- /// Create a future by specifying its output. This is basically the same as
- /// `async { value }` or `futures::future::ready(value)`.
- pub fn ready(op_ctx: &OpCtx, promise_id: PromiseId, value: OpResult) -> Self {
+ pub fn new(op_ctx: &OpCtx, promise_id: PromiseId, fut: F) -> Self {
Self {
op_id: op_ctx.id,
promise_id,
- fut: MaybeDone::Done(value),
+ fut,
}
}
}
-impl Future for OpCall {
+impl<F: Future<Output = OpResult>> Future for OpCall<F> {
type Output = (PromiseId, OpId, OpResult);
fn poll(
@@ -72,21 +55,8 @@ impl Future for OpCall {
) -> std::task::Poll<Self::Output> {
let promise_id = self.promise_id;
let op_id = self.op_id;
- let fut = &mut *self.project().fut;
- match fut {
- MaybeDone::Done(_) => {
- // Let's avoid using take_output as it keeps our Pin::box
- let res = std::mem::replace(fut, MaybeDone::Gone);
- let MaybeDone::Done(res) = res
- else {
- unreachable!()
- };
- std::task::Poll::Ready(res)
- }
- MaybeDone::Future(f) => f.poll_unpin(cx),
- MaybeDone::Gone => std::task::Poll::Pending,
- }
- .map(move |res| (promise_id, op_id, res))
+ let fut = self.project().fut;
+ fut.poll(cx).map(move |res| (promise_id, op_id, res))
}
}