Skip to main content

dope_runtime/topology/lending/raw/
mod.rs

1use std::{marker, mem, pin, ptr};
2
3use crate::executor;
4use dope_core::driver;
5
6mod forward;
7mod owner;
8mod project;
9mod resume;
10
11pub use owner::Owner;
12
13/// Lifecycle storage borrowing the exact pinned provider.
14pub(crate) type Borrowed<'a, T> = Owner<pin::Pin<&'a mut T>, T>;
15pub use project::Project;
16pub use resume::Resume;
17
18// SAFETY: the owned pair forwards each lifecycle operation to both exact pinned
19// roots. Each root independently permits resume after stopped native quiescence;
20// the pair adds no operations or cleanup awaiting dispatch between them.
21unsafe impl<'d, A, B> Resume<'d> for crate::client::Pair<'d, A, B>
22where
23    A: crate::topology::lending::Root<'d> + Resume<'d>,
24    B: crate::topology::lending::Root<'d> + Resume<'d>,
25{
26}
27
28/// A dependent application retained by its pinned provider owner.
29#[pin_project::pin_project]
30#[derive(dope_gen::Application)]
31#[dispatcher(core = ::dope_core, manifold = crate, runtime = crate, driver = 'd, lending = true)]
32pub struct Bound<'app, 'd, H, T> {
33    #[dispatcher(state)]
34    driver: marker::PhantomData<driver::Reference<'d>>,
35    #[pin]
36    #[app]
37    head: Borrowed<'app, H>,
38    #[pin]
39    #[app]
40    #[drain]
41    tail: Owner<mem::ManuallyDrop<T>, T>,
42}
43
44impl<'app, 'd, H, T> Bound<'app, 'd, H, T> {
45    pub(crate) const fn new(head: pin::Pin<&'app mut H>, tail: T) -> Self {
46        const {
47            assert!(mem::size_of::<Self>() == mem::size_of::<(pin::Pin<&'app mut H>, T)>());
48            assert!(mem::align_of::<Self>() == mem::align_of::<(pin::Pin<&'app mut H>, T)>());
49            assert!(!mem::needs_drop::<Self>());
50        }
51        Self {
52            driver: marker::PhantomData,
53            head: Owner::new(head),
54            tail: Owner::new(mem::ManuallyDrop::new(tail)),
55        }
56    }
57
58    pub fn tail(self: pin::Pin<&Self>) -> pin::Pin<&T> {
59        Project::shared(self.project_ref().tail)
60    }
61}
62
63impl<'parent, 'd, H, T> Bound<'parent, 'd, H, T>
64where
65    H: executor::raw::App<'d>,
66    T: executor::raw::App<'d>,
67{
68    /// # Safety
69    /// Consume this exact native-quiesced owner; never project or finish its backing again.
70    pub(crate) unsafe fn finish_tail<'call, 'finish, 'owner, 'epoch>(
71        call: executor::raw::Finish<'call, 'finish, 'owner, 'epoch, 'd, Self>,
72        plan: <Self as executor::raw::Layout<'d>>::ParkPlan,
73    ) -> (
74        Borrowed<'parent, H>,
75        <H as executor::raw::Layout<'d>>::ParkPlan,
76    ) {
77        let (mut app, install, finish) = call.into_parts();
78        let tail = app.as_mut().project().tail;
79        let call = executor::raw::Finish::new(tail, install, finish);
80        let _ = <Owner<mem::ManuallyDrop<T>, T> as executor::raw::Finishes<'d>>::call(call);
81        let head = app.as_mut().project().head;
82        // SAFETY: the consumed owner makes its old backing inaccessible. Moving
83        // this Pin handle preserves the actual provider's address and full loan.
84        let head = unsafe { ptr::read(head.get_unchecked_mut()) };
85        (head, plan.3)
86    }
87}
88
89const _: () = {
90    assert!(mem::size_of::<Bound<'_, '_, (), ()>>() == mem::size_of::<&mut ()>());
91    assert!(mem::align_of::<Bound<'_, '_, (), ()>>() == mem::align_of::<&mut ()>());
92    assert!(!mem::needs_drop::<Bound<'_, '_, (), ()>>());
93};