Skip to main content

dope_core/executor/task/context/
raw.rs

1use core::{pin, task::Poll};
2
3use crate::executor::task::{
4    self,
5    context::{Context, Continue, PollCall},
6};
7
8/// One admitted child descent, retaining the original poll context.
9/// Consuming it transfers the parent's credit to a task that owns completion.
10pub struct Admission<'call, 'turn, 'd: 'turn> {
11    pub(super) context: &'call mut Context<'turn, 'd>,
12}
13
14impl<'call, 'turn, 'd> Admission<'call, 'turn, 'd> {
15    pub(crate) fn into_context(self) -> &'call mut Context<'turn, 'd> {
16        self.context
17    }
18
19    /// Borrows the context for setup before the admitted child descent.
20    pub fn context(&mut self) -> &mut Context<'turn, 'd> {
21        &mut *self.context
22    }
23
24    /// Transfers the existing credit exactly once and returns the context for
25    /// subsequent work. A completed task remains pending without repolling its
26    /// former fiber; additional child polls through the context pay a credit.
27    pub fn poll<F: task::Fiber<'d>>(
28        self,
29        fiber: pin::Pin<&mut task::Task<F>>,
30    ) -> (Poll<F::Output>, &'call mut Context<'turn, 'd>) {
31        let result = fiber.poll(&mut *self.context);
32        (result, self.context)
33    }
34
35    /// Polls the owner's child slot and drops a completed child before returning its output.
36    pub fn poll_slot<F: task::Fiber<'d>>(self, slot: pin::Pin<&mut Option<F>>) -> Poll<F::Output> {
37        task::Task::poll_slot(slot, self.context)
38    }
39
40    /// Transfers the existing credit to one child stream and returns the same
41    /// context. End drops the child; later admitted calls return `Ready(None)`
42    /// without polling it again.
43    pub fn poll_next<S: task::Stream<'d>>(
44        self,
45        stream: pin::Pin<&mut task::Task<S>>,
46    ) -> (Poll<Option<S::Item>>, &'call mut Context<'turn, 'd>) {
47        let result = stream.poll_next(&mut *self.context);
48        (result, self.context)
49    }
50}
51
52impl<'call, 'turn, 'd> Admission<'call, 'turn, 'd> {
53    /// # Safety
54    /// Own the unfinished pinned fiber and drop it before exposing its Ready output.
55    /// Delegation may return only to the enclosing owner that performs that drop.
56    pub unsafe fn invoke<F: task::Fiber<'d>>(
57        self,
58        fiber: pin::Pin<&mut F>,
59    ) -> (Poll<F::Output>, &'call mut Context<'turn, 'd>) {
60        let result = F::poll(PollCall {
61            fiber,
62            context: &mut *self.context,
63        });
64        (result, self.context)
65    }
66
67    /// # Safety
68    /// Retain the original pinned stream until EOF, then end polling and drop it.
69    pub unsafe fn advance<S: task::Stream<'d>>(
70        self,
71        stream: pin::Pin<&mut S>,
72    ) -> Poll<Option<S::Item>> {
73        S::poll_next(PollCall {
74            fiber: stream,
75            context: self.context,
76        })
77    }
78
79    /// # Safety
80    /// Own the unfinished final Pin and drop it on Ready before output use.
81    /// Delegation may return only to the enclosing owner that performs that drop.
82    pub unsafe fn budgeted<F: task::Fiber<'d>>(
83        fiber: pin::Pin<&mut F>,
84        context: &'call mut Context<'turn, 'd>,
85    ) -> Poll<F::Output> {
86        let Continue::Permit(_permit) = context.try_continue() else {
87            return Poll::Pending;
88        };
89        F::poll(PollCall { fiber, context })
90    }
91}