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: 'turn> 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    /// Transfers the existing credit to one child stream and returns the same
36    /// context. End drops the child; later admitted calls return `Ready(None)`
37    /// without polling it again.
38    pub fn poll_next<S: task::Stream<'d>>(
39        self,
40        stream: pin::Pin<&mut task::Task<S>>,
41    ) -> (Poll<Option<S::Item>>, &'call mut Context<'turn, 'd>) {
42        let result = stream.poll_next(&mut *self.context);
43        (result, self.context)
44    }
45}
46
47impl<'call, 'turn, 'd: 'turn> Admission<'call, 'turn, 'd> {
48    /// # Safety
49    /// Own the unfinished pinned fiber and drop it before exposing its Ready output.
50    /// Delegation may return only to the enclosing owner that performs that drop.
51    pub unsafe fn invoke<F: task::Fiber<'d>>(
52        self,
53        fiber: pin::Pin<&mut F>,
54    ) -> (Poll<F::Output>, &'call mut Context<'turn, 'd>) {
55        let result = F::poll(PollCall {
56            fiber,
57            context: &mut *self.context,
58        });
59        (result, self.context)
60    }
61
62    /// # Safety
63    /// Retain the original pinned stream until EOF, then end polling and drop it.
64    pub unsafe fn advance<S: task::Stream<'d>>(
65        self,
66        stream: pin::Pin<&mut S>,
67    ) -> Poll<Option<S::Item>> {
68        S::poll_next(PollCall {
69            fiber: stream,
70            context: self.context,
71        })
72    }
73
74    /// # Safety
75    /// Own the unfinished final Pin and drop it on Ready before output use.
76    /// Delegation may return only to the enclosing owner that performs that drop.
77    pub unsafe fn budgeted<F: task::Fiber<'d>>(
78        fiber: pin::Pin<&mut F>,
79        context: &'call mut Context<'turn, 'd>,
80    ) -> Poll<F::Output> {
81        let Continue::Permit(_permit) = context.try_continue() else {
82            return Poll::Pending;
83        };
84        F::poll(PollCall { fiber, context })
85    }
86}