//! Interface for interfaces.
//!
//! ## Design
//! The design of protocols is based on an idea found in the
//! [`gdbstub`](https://docs.rs/gdbstub/latest/gdbstub/target/ext/index.html) crate.
//! This idea is of so called inlinable dyn extension traits.
//! However, in the form given in `gdbstub` they can't be used for arbitrary interfaces.
//! The main trait still needs to know about all the possible protocols.
//! That is where this module comes in.
//!
//! This module implements a technique we name dynamic inlinable dyn extension traits (DIDETs).
//! DIDETs adds one more layer to IDETs. Instead of a trait that knows all the possible protocols,
//! we have a single trait [`Implementer`] that allows looking up an extension trait
//! using a type ID. This may seem like it defeats the purpose of IDETs, that being to
//! make them inlinable. However, it turns out LLVM (the optimizer) is able to see
//! through this style of runtime reflection. As such, we still gain the benefits of
//! IDETs but with more flexability.
//! Protocols can now be defined in *any* crate and used between arbitrary crates.
//!
//! A protocol is a special trait that can participate as a DIDET. The only thing needed
//! for a protocol is an associated trait object. Because we need to use the
//! [`TypeId`][core::any::TypeId] of a protocol to perform reflection, we can't just use
//! the trait object itself as the protocol type. Instead an uninhabited type is used
//! as a marker for the trait.
//!
//! We then "implement" a protocol for a type by using [`Implementation`]. This provides
//! a mapping from `T` to the protocol's trait object.
//! By itself, [`Implementation`] is not enough for DIDET. A type also needs to implement
//! [`Implementer`] which allows looking up a particular [`Implementation`] trait object
//! from a [`ProtocolId`].
//!
//! The implementation of DIDETs defined by this module allows [`Implementer`] to be object safe.
//! This is done via the help of the [`AnyImpl`] type. This is not required for the core
//! idea of DIDETs.
use core::{
future::Future,
pin::{pin, Pin},
ptr,
task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
marker::PhantomData,
};
use crate::any::AnyTrait;
use crate::hkt::{type_class, hkt};
pub mod visitor;
pub mod walker;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::boxed::Box;
pub type Visitor<'a, 'ctx, Effect> = any_trait_send_obj::T<'a, 'ctx, <Effect as self::Effect<'ctx>>::VisitorHkt>;
pub type Walker<'a, 'ctx, Effect> = any_trait_send_obj::T<'a, 'ctx, <Effect as self::Effect<'ctx>>::WalkerHkt>;
pub trait AnyTraitObj<'a, 'ctx: 'a>: AnyTraitSendObj<'a, 'ctx> {
fn from_obj(value: &'a mut (dyn AnyTrait<'ctx> + 'a)) -> Self;
}
pub trait AnyTraitSendObj<'a, 'ctx: 'a> {
fn from_obj_send(value: &'a mut (dyn AnyTrait<'ctx> + Send + 'a)) -> Self;
fn as_obj(&self) -> &dyn AnyTrait<'ctx>;
fn as_obj_mut(&mut self) -> &mut dyn AnyTrait<'ctx>;
fn into_obj(self) -> &'a mut dyn AnyTrait<'ctx>;
}
impl<'a, 'ctx: 'a> AnyTraitSendObj<'a, 'ctx> for &'a mut (dyn AnyTrait<'ctx> + 'a) {
fn from_obj_send(value: &'a mut (dyn AnyTrait<'ctx> + Send + 'a)) -> Self {
value
}
fn as_obj(&self) -> &dyn AnyTrait<'ctx> {
*self
}
fn as_obj_mut(&mut self) -> &mut dyn AnyTrait<'ctx> {
*self
}
fn into_obj(self) -> &'a mut dyn AnyTrait<'ctx> {
self
}
}
impl<'a, 'ctx: 'a> AnyTraitSendObj<'a, 'ctx> for &'a mut (dyn AnyTrait<'ctx> + Send + 'a) {
fn from_obj_send(value: &'a mut (dyn AnyTrait<'ctx> + Send + 'a)) -> Self {
value
}
fn as_obj(&self) -> &dyn AnyTrait<'ctx> {
*self
}
fn as_obj_mut(&mut self) -> &mut dyn AnyTrait<'ctx> {
*self
}
fn into_obj(self) -> &'a mut dyn AnyTrait<'ctx> {
self
}
}
impl<'a, 'ctx: 'a> AnyTraitObj<'a, 'ctx> for &'a mut (dyn AnyTrait<'ctx> + 'a) {
fn from_obj(value: &'a mut (dyn AnyTrait<'ctx> + 'a)) -> Self {
value
}
}
type_class!(for<'lt, 'ctx> pub any_trait_send_obj: AnyTraitSendObj<'lt, 'ctx>);
type_class!(for<'lt, 'ctx> pub any_t);
pub trait Effect<'ctx, C = (), B = ()>: 'static {
type VisitorHkt: any_trait_send_obj::Hkt<'ctx>;
type WalkerHkt: any_trait_send_obj::Hkt<'ctx>;
type ControlFlowHkt: any_t::Hkt<'ctx>;
}
pub type ControlFlowFor<'a, 'ctx, E = SyncEffect, C = (), B = ()> =
any_t::T<'a, 'ctx, <E as Effect<'ctx, C, B>>::ControlFlowHkt>;
pub enum SyncEffect {}
hkt!((any_trait_send_obj): for<'a, 'ctx> pub AnyTraitSendObjHkt => &'a mut (dyn AnyTrait<'ctx> + Send + 'a));
hkt!((any_trait_send_obj): for<'a, 'ctx> pub AnyTraitObjHkt => &'a mut (dyn AnyTrait<'ctx> + 'a));
hkt!((any_t): for<'a, 'ctx> pub SyncControlFlowHkt[C, B] => core::ops::ControlFlow<B, C>);
impl<'ctx, C, B> Effect<'ctx, C, B> for SyncEffect {
type ControlFlowHkt = SyncControlFlowHkt<'ctx, C, B>;
type VisitorHkt = AnyTraitObjHkt<'ctx>;
type WalkerHkt = AnyTraitObjHkt<'ctx>;
}
#[cfg(feature = "alloc")]
hkt!((any_t): for<'a, 'ctx> pub AsyncControlFlowHkt[C, B] =>
core::pin::Pin<
Box<dyn core::future::Future<Output = core::ops::ControlFlow<B, C>> + Send + 'a>,
>
);
#[cfg(feature = "alloc")]
pub enum AsyncEffect {}
#[cfg(feature = "alloc")]
impl<'ctx, C, B> Effect<'ctx, C, B> for AsyncEffect {
type VisitorHkt = AnyTraitSendObjHkt<'ctx>;
type WalkerHkt = AnyTraitSendObjHkt<'ctx>;
type ControlFlowHkt = AsyncControlFlowHkt<'ctx, C, B>;
}