Diffstat (limited to 'src/protocol.rs')
| -rw-r--r-- | src/protocol.rs | 138 |
1 files changed, 25 insertions, 113 deletions
diff --git a/src/protocol.rs b/src/protocol.rs index 4ad6d8d..605551a 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -1,3 +1,22 @@ +//! A collection of built in protocols between walkers and visitors. +//! +//! Treaty has not set data model. Instead, this module contains a set of basic protocols +//! walkers and visitors can use to exchange information. +//! +//! | Rust Type (`T`) | Protocol | +//! |-----------|----------| +//! | `bool` | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] | +//! | `i8`, `i16`, `i32`, `i64`, `i128`, `isize` | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] | +//! | `u8`, `u16`, `u32`, `u64`, `u128`, `usize` | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] | +//! | `f32`, `f64` | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] | +//! | `char` | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] | +//! | `String` | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] | +//! | `Vec<u8>` | [`dyn Value<'_, OwnedStatic<T>, _>`][visitor::Value] | +//! | `&'ctx str` | [`dyn Value<'ctx, BorrowedStatic<'ctx, T>, _>`][visitor::Value] | +//! | `&'ctx [u8]` | [`dyn Value<'ctx, BorrowedStatic<'ctx, T>, _>`][visitor::Value] | +//! +//! +//! //! Interface for interfaces. //! //! ## Design @@ -41,8 +60,11 @@ use core::{ task::{Context, Poll, RawWaker, RawWakerVTable, Waker}, }; -use crate::any::AnyTrait; use crate::hkt::{hkt, type_class}; +use crate::{ + any::AnyTrait, + effect::{as_obj, EffectAnyTrait}, +}; pub mod visitor; pub mod walker; @@ -51,115 +73,5 @@ pub mod walker; use alloc::boxed::Box; pub type Visitor<'a, 'ctx, Effect> = - as_obj::T<'a, 'ctx, <Effect as self::EffectAnyTrait<'ctx>>::AnyTrait>; -pub type Walker<'a, 'ctx, Effect> = - as_obj::T<'a, 'ctx, <Effect as self::EffectAnyTrait<'ctx>>::AnyTrait>; - -pub trait AsObj<'a, 'ctx: 'a> { - 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> AsObj<'a, 'ctx> for &'a mut (dyn AnyTrait<'ctx> + 'a) { - 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> AsObj<'a, 'ctx> for &'a mut (dyn AnyTrait<'ctx> + Send + 'a) { - 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 - } -} - -type_class!(for<'lt, 'ctx> pub as_obj: AsObj<'lt, 'ctx>); -type_class!(for<'lt, 'ctx> pub any_t); - -pub trait EffectAnyTrait<'ctx>: 'static { - /// The `dyn AnyTrait<'ctx>` for the effect. - /// - /// this allows adding extra bounds to the trait object. - type AnyTrait: as_obj::Hkt<'ctx>; -} - -/// Trait for effects. -pub trait Effect<'ctx, T>: EffectAnyTrait<'ctx> { - /// The type functions return for this effect. - /// - /// This type should resolve into a `T`. - type Yield: any_t::Hkt<'ctx>; -} - -pub type Yield<'a, 'ctx, T, E> = any_t::T<'a, 'ctx, <E as Effect<'ctx, T>>::Yield>; - -pub enum SyncEffect {} - -hkt!((as_obj): for<'a, 'ctx> pub AnyTraitSendObj => &'a mut (dyn AnyTrait<'ctx> + Send + 'a)); -hkt!((as_obj): for<'a, 'ctx> pub AnyTraitObj => &'a mut (dyn AnyTrait<'ctx> + 'a)); - -hkt!((any_t): for<'a, 'ctx> pub SyncYield[T] => T); - -impl<'ctx, T> Effect<'ctx, T> for SyncEffect { - type Yield = SyncYield<'ctx, T>; -} - -impl<'ctx> EffectAnyTrait<'ctx> for SyncEffect { - type AnyTrait = AnyTraitObj<'ctx>; -} - -#[cfg(feature = "alloc")] -hkt!((any_t): for<'a, 'ctx> pub AsyncSendYield[T] => - core::pin::Pin< - Box<dyn core::future::Future<Output = T> + Send + 'a>, - > -); - -#[cfg(feature = "alloc")] -pub enum AsyncSendEffect {} - -#[cfg(feature = "alloc")] -impl<'ctx, T> Effect<'ctx, T> for AsyncSendEffect { - type Yield = AsyncSendYield<'ctx, T>; -} - -#[cfg(feature = "alloc")] -impl<'ctx> EffectAnyTrait<'ctx> for AsyncSendEffect { - type AnyTrait = AnyTraitSendObj<'ctx>; -} - -#[cfg(feature = "alloc")] -hkt!((any_t): for<'a, 'ctx> pub AsyncYield[T] => - core::pin::Pin< - Box<dyn core::future::Future<Output = T> + 'a>, - > -); - -#[cfg(feature = "alloc")] -pub enum AsyncEffect {} - -#[cfg(feature = "alloc")] -impl<'ctx, T> Effect<'ctx, T> for AsyncEffect { - type Yield = AsyncYield<'ctx, T>; -} - -#[cfg(feature = "alloc")] -impl<'ctx> EffectAnyTrait<'ctx> for AsyncEffect { - type AnyTrait = AnyTraitObj<'ctx>; -} + as_obj::T<'a, 'ctx, <Effect as EffectAnyTrait<'ctx>>::AnyTrait>; +pub type Walker<'a, 'ctx, Effect> = as_obj::T<'a, 'ctx, <Effect as EffectAnyTrait<'ctx>>::AnyTrait>; |