Skip to main content

dope_core/executor/task/future/
raw.rs

1use core::{future::Future, marker, mem, pin, ptr, task};
2
3use o3::base::marker::ThreadBound;
4
5use crate::{
6    executor::task::context::{self, Context},
7    executor::task::{Fiber, future},
8};
9
10pub(super) struct Frame<'call, 'turn, 'd: 'turn, I> {
11    context: &'call mut Context<'turn, 'd>,
12    first: bool,
13    scope: marker::PhantomData<fn(&mut I) -> &mut I>,
14}
15
16const _: () = {
17    assert!(mem::size_of::<Frame<'static, 'static, 'static, ()>>() == 2 * mem::size_of::<usize>());
18};
19
20impl<'call, 'turn, 'd: 'turn, I> Frame<'call, 'turn, 'd, I> {
21    pub(super) fn new(context: &'call mut Context<'turn, 'd>) -> Self {
22        Self {
23            context,
24            first: true,
25            scope: marker::PhantomData,
26        }
27    }
28}
29
30impl<'call, 'turn, 'd: 'turn, I> Frame<'call, 'turn, 'd, I> {
31    pub(super) fn poll<F, const REPEAT: bool>(
32        &mut self,
33        fiber: pin::Pin<&mut F>,
34    ) -> task::Poll<F::Output>
35    where
36        F: Fiber<'d>,
37    {
38        let first = mem::replace(&mut self.first, false);
39        if REPEAT && !first {
40            let context::Continue::Permit(_permit) = self.context.try_continue() else {
41                return task::Poll::Pending;
42            };
43        }
44        F::poll(context::PollCall {
45            fiber,
46            context: &mut *self.context,
47        })
48    }
49}
50
51pub struct Brand<'d, I> {
52    scope: marker::PhantomData<fn(&'d mut I) -> &'d mut I>,
53    thread: marker::PhantomData<ThreadBound>,
54}
55
56impl<'d, I> Brand<'d, I> {
57    /// # Safety
58    /// This brand must remain affine inside one generated state machine.
59    pub const unsafe fn scope() -> Self {
60        Self {
61            scope: marker::PhantomData,
62            thread: marker::PhantomData,
63        }
64    }
65
66    pub fn awaitable<'a, F, const REPEAT: bool>(
67        fiber: F,
68        _: &'a mut Self,
69    ) -> future::Awaitable<'a, 'd, I, F, REPEAT> {
70        future::Awaitable {
71            fiber,
72            marker: marker::PhantomData,
73        }
74    }
75}
76
77impl<'d, I, F> FiberAdapter<'d, I, F> {
78    pub fn input<'a, S, A, T, M>(_: &'a S, maker: M) -> M
79    where
80        M: FnOnce((A, &'a S, T)) -> F,
81    {
82        maker
83    }
84
85    /// # Safety
86    /// `future` must be the generated machine for this scope and must not retain or expose its poll waker.
87    /// It may await only its affine `Brand` or immediately awaited lexical async scopes obeying this rule.
88    pub unsafe fn new(future: F) -> Self {
89        Self {
90            future,
91            scope: marker::PhantomData,
92            thread: marker::PhantomData,
93        }
94    }
95}
96
97static BORROWED_VTABLE: task::RawWakerVTable =
98    task::RawWakerVTable::new(clone_inert, noop, noop, noop);
99/// # Safety
100/// `data` borrows one live adapter poll frame; this callback neither owns nor dereferences it.
101/// Cloning preserves the identical `(data, vtable)` pair required for interchangeable wakers.
102unsafe fn clone_inert(data: *const ()) -> task::RawWaker {
103    task::RawWaker::new(data, &BORROWED_VTABLE)
104}
105
106unsafe fn noop(_: *const ()) {}
107
108#[pin_project::pin_project]
109#[repr(transparent)]
110#[doc(hidden)]
111#[must_use = "a fiber does nothing unless it is driven"]
112pub struct FiberAdapter<'d, I, F> {
113    #[pin]
114    future: F,
115    scope: marker::PhantomData<fn(&'d mut I) -> &'d mut I>,
116    thread: marker::PhantomData<ThreadBound>,
117}
118
119impl<'d, I, F> Fiber<'d> for FiberAdapter<'d, I, F>
120where
121    F: Future,
122{
123    type Output = F::Output;
124
125    fn poll(call: context::PollCall<'_, '_, 'd, Self>) -> task::Poll<Self::Output> {
126        use core::task::{RawWaker, Waker};
127
128        let (this, context) = call.into_parts();
129
130        let mut frame = Frame::<I>::new(context);
131        let data = ptr::from_mut(&mut frame).cast_const().cast();
132        let raw = RawWaker::new(data, &BORROWED_VTABLE);
133        let waker = mem::ManuallyDrop::new(unsafe { Waker::from_raw(raw) });
134        let mut cx = task::Context::from_waker(&waker);
135        Future::poll(this.project().future, &mut cx)
136    }
137}