use std::{marker, pin};
use crate::executor::session;
#[repr(transparent)]
pub struct Anchor<'app, T> {
root: pin::Pin<&'app mut T>,
}
impl<'app, T> Anchor<'app, T> {
pub(crate) fn new(root: pin::Pin<&'app mut T>) -> Self {
Self { root }
}
#[doc(hidden)]
pub fn as_mut(self: pin::Pin<&mut Self>) -> pin::Pin<&mut T> {
self.get_mut().root.as_mut()
}
#[doc(hidden)]
pub fn as_ref(self: pin::Pin<&Self>) -> pin::Pin<&T> {
self.get_ref().root.as_ref()
}
}
pub trait Composition<'scope, 'd: 'scope, S, Q, O, P>
where
P: Provider<'d>,
{
type Output;
fn compose<'app>(
self,
client: P::Client<'app>,
root: Anchor<'app, O>,
session: &mut session::Session<'scope, 'd, S, Q>,
) -> Self::Output
where
'd: 'app;
}
pub struct Lease<'app, 'd: 'app> {
app: marker::PhantomData<*mut &'app ()>,
driver: marker::PhantomData<*mut &'d ()>,
}
impl<'app, 'd: 'app> Lease<'app, 'd> {
const fn new() -> Self {
Self {
app: marker::PhantomData,
driver: marker::PhantomData,
}
}
}
impl Copy for Lease<'_, '_> {}
impl Clone for Lease<'_, '_> {
fn clone(&self) -> Self {
*self
}
}
pub struct Scope<'app, 'd: 'app, P: ?Sized> {
app: marker::PhantomData<*mut &'app ()>,
driver: marker::PhantomData<*mut &'d ()>,
provider: marker::PhantomData<*mut P>,
}
impl<'app, 'd: 'app, P: ?Sized> Scope<'app, 'd, P> {
pub(crate) const fn new() -> Self {
Self {
app: marker::PhantomData,
driver: marker::PhantomData,
provider: marker::PhantomData,
}
}
pub const fn lease(self) -> Lease<'app, 'd> {
Lease::new()
}
pub const fn reborrow<'short>(&'short self) -> Scope<'short, 'd, P>
where
'app: 'short,
{
Scope {
app: marker::PhantomData,
driver: marker::PhantomData,
provider: marker::PhantomData,
}
}
}
impl<P: ?Sized> Copy for Scope<'_, '_, P> {}
impl<P: ?Sized> Clone for Scope<'_, '_, P> {
fn clone(&self) -> Self {
*self
}
}
pub trait Provider<'d> {
type Client<'app>: 'app
where
'd: 'app;
fn provide<'app>(self: pin::Pin<&Self>, scope: Scope<'app, 'd, Self>) -> Self::Client<'app>
where
'd: 'app;
}