Skip to main content

dope_core/client/
mod.rs

1//! Runtime-scoped client capabilities issued by an exact provider.
2
3mod bound;
4mod context;
5pub mod provider;
6
7pub use bound::Bound;
8pub use context::Context;
9
10use std::marker;
11
12use crate::{driver, topology::lending};
13
14/// Produces a client from the exact selected provider.
15/// The provider type carries its storage loans; each client requires those loans
16/// and its driver domain to outlive the issued application scope.
17pub trait Provider<'d> {
18    type Client<'app>: 'app
19    where
20        Self: 'app,
21        'd: 'app;
22
23    fn provide<'app>(context: Context<'_, 'app, 'd, Self>) -> Self::Client<'app>
24    where
25        Self: 'app,
26        'd: 'app;
27}
28
29/// Owns two providers and issues their clients in the same lending scope.
30/// Both pinned roots remain owned until their dependent application is released.
31#[pin_project::pin_project]
32#[derive(dope_gen::Application)]
33#[dispatcher(core = crate, driver = 'd)]
34pub struct Pair<'d, A: lending::Root<'d>, B: lending::Root<'d>> {
35    #[dispatcher(state)]
36    scope: marker::PhantomData<(
37        driver::Reference<'d>,
38        *const A::Provider,
39        *const B::Provider,
40    )>,
41    #[pin]
42    #[app]
43    first: A,
44    #[pin]
45    #[app]
46    second: B,
47}
48
49impl<'d, A: lending::Root<'d>, B: lending::Root<'d>> Pair<'d, A, B> {
50    pub const fn new(first: A, second: B) -> Self {
51        Self {
52            scope: marker::PhantomData,
53            first,
54            second,
55        }
56    }
57}
58
59impl<'d, A: lending::Root<'d>, B: lending::Root<'d>> Provider<'d> for Pair<'d, A, B> {
60    type Client<'app>
61        = (
62        <A::Provider as Provider<'d>>::Client<'app>,
63        <B::Provider as Provider<'d>>::Client<'app>,
64    )
65    where
66        Self: 'app,
67        'd: 'app;
68
69    fn provide<'app>(context: Context<'_, 'app, 'd, Self>) -> Self::Client<'app>
70    where
71        Self: 'app,
72        'd: 'app,
73    {
74        let (pair, _) = context.into_parts();
75        let pair = pair.project_ref();
76        (
77            Provider::provide(Context::new(lending::Root::provider(pair.first))),
78            Provider::provide(Context::new(lending::Root::provider(pair.second))),
79        )
80    }
81}