Context via FunctorContext

This commit is contained in:
AF 2023-05-30 15:15:45 +00:00
parent f4a3220548
commit 277bea3eb3
7 changed files with 32 additions and 16 deletions

View File

@ -16,6 +16,7 @@ mod slice_deserializer;
use std::{error::Error, rc::Rc}; use std::{error::Error, rc::Rc};
use crate::func::context::*;
use crate::func::*; use crate::func::*;
pub use self::addresses::Addresses; pub use self::addresses::Addresses;
@ -32,8 +33,8 @@ pub use self::resolution::{
pub use self::serialization::{Deserializer, DeserializerExt, Serializable, Serializer}; pub use self::serialization::{Deserializer, DeserializerExt, Serializable, Serializer};
pub use self::slice_deserializer::SliceDeserializer; pub use self::slice_deserializer::SliceDeserializer;
/// Helper alias for [`WeakFunctor::F`] of [`Context::T`]. /// Helper alias for [`WeakFunctor::F`] of [`FunctorContext::T`].
pub type Wrapped<'a, Ctx, A> = Wrap<'a, A, <Ctx as Context<'a>>::T>; pub type Wrapped<'a, Ctx, A> = WrapC<'a, A, Ctx>;
/// Fundamental trait for ADN objects. /// Fundamental trait for ADN objects.
pub trait Mentionable<'a, Ctx: Context<'a>>: 'a + Serializable { pub trait Mentionable<'a, Ctx: Context<'a>>: 'a + Serializable {

View File

@ -1,9 +1,9 @@
use super::*; use super::*;
/// Execution context. /// Execution context.
pub trait Context<'a>: 'a { pub trait Context<'a>: FunctorContext<'a, T = Self::_Tm> {
/// Type to provide for [Monad]ic representation of computation, mostly that of resolution ([`Resolution`]). /// Type to provide for [Monad]ic representation of computation, mostly that of resolution ([`Resolution`]).
type T: Monad<'a>; type _Tm: Monad<'a>;
/// Type to allow improved support for result evaluation. /// Type to allow improved support for result evaluation.
/// This is important for async applications stopping early. /// This is important for async applications stopping early.

View File

@ -1,5 +1,8 @@
//! Shorthands for using [`Context::Fallible`]. //! Shorthands for using [`Context::Fallible`].
#[cfg(doc)]
use crate::func::context::FunctorContext;
use super::*; use super::*;
/// Preferred monad for fallible uses. /// Preferred monad for fallible uses.
@ -8,7 +11,7 @@ pub type FallibleMonad<'a, Ctx, E> = <<Ctx as Context<'a>>::Fallible as MonadFai
/// Preferred [Wrapped] [Result]. /// Preferred [Wrapped] [Result].
pub type FallibleWrapped<'a, Ctx, A, E> = Wrap<'a, A, FallibleMonad<'a, Ctx, E>>; pub type FallibleWrapped<'a, Ctx, A, E> = Wrap<'a, A, FallibleMonad<'a, Ctx, E>>;
/// Extention trait for simpler conversion between [`Context::T`] and [`Context::Fallible`]. /// Extention trait for simpler conversion between [`FunctorContext::T`] and [`Context::Fallible`].
/// ///
/// Until either Rust type system or [`crate::func`] take serious changes, /// Until either Rust type system or [`crate::func`] take serious changes,
/// this is the preferred way to switch between [Wrapped] and [fallible]. /// this is the preferred way to switch between [Wrapped] and [fallible].

View File

@ -6,13 +6,13 @@ struct TracedResolver<'a, Ctx: Context<'a>> {
resolver: Rc<dyn Resolver<'a, Ctx>>, resolver: Rc<dyn Resolver<'a, Ctx>>,
} }
impl<'a, Ctx: Context<'a, T = TracedInstance>> TracedResolver<'a, Ctx> { impl<'a, Ctx: Context<'a, _Tm = TracedInstance>> TracedResolver<'a, Ctx> {
fn wrap(resolver: Rc<dyn Resolver<'a, Ctx>>) -> Rc<dyn Resolver<'a, Ctx>> { fn wrap(resolver: Rc<dyn Resolver<'a, Ctx>>) -> Rc<dyn Resolver<'a, Ctx>> {
Rc::new(Self { resolver }) Rc::new(Self { resolver })
} }
} }
impl<'a, Ctx: Context<'a, T = TracedInstance>> Resolver<'a, Ctx> for TracedResolver<'a, Ctx> { impl<'a, Ctx: Context<'a, _Tm = TracedInstance>> Resolver<'a, Ctx> for TracedResolver<'a, Ctx> {
fn resolve(self: Rc<Self>, address: Address) -> HashResolution<'a, Ctx> { fn resolve(self: Rc<Self>, address: Address) -> HashResolution<'a, Ctx> {
TracedInstance::fmap(self.resolver.clone().resolve(address), |resolved| { TracedInstance::fmap(self.resolver.clone().resolve(address), |resolved| {
let (src, resolver) = resolved?; let (src, resolver) = resolved?;
@ -24,7 +24,7 @@ impl<'a, Ctx: Context<'a, T = TracedInstance>> Resolver<'a, Ctx> for TracedResol
} }
/// Extension trait to trace the evaluation flow. /// Extension trait to trace the evaluation flow.
pub trait Traceable<'a, Ctx: Context<'a, T = TracedInstance>>: pub trait Traceable<'a, Ctx: Context<'a, _Tm = TracedInstance>>:
Mentionable<'a, Ctx> + Sized Mentionable<'a, Ctx> + Sized
{ {
/// Re-cast the value, adding an extra[^extra] /// Re-cast the value, adding an extra[^extra]
@ -35,7 +35,7 @@ pub trait Traceable<'a, Ctx: Context<'a, T = TracedInstance>>:
fn trace(self: Rc<Self>) -> CastResult<'a, Ctx, Self>; fn trace(self: Rc<Self>) -> CastResult<'a, Ctx, Self>;
} }
impl<'a, Ctx: Context<'a, T = TracedInstance>, A: Mentionable<'a, Ctx>> Traceable<'a, Ctx> for A impl<'a, Ctx: Context<'a, _Tm = TracedInstance>, A: Mentionable<'a, Ctx>> Traceable<'a, Ctx> for A
where where
Ctx::LookupError: From<CastError<'a>>, Ctx::LookupError: From<CastError<'a>>,
{ {

View File

@ -6,10 +6,9 @@ use std::{error::Error, fmt::Display, rc::Rc};
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use crate::func::*; use crate::func::{context::FunctorContext, *};
use crate::rcore::*; use crate::rcore::*;
use crate::rstd::cast::*; use crate::rstd::{cast::*, typeless::*};
use crate::rstd::typeless::*;
pub struct NoDiagnostic; pub struct NoDiagnostic;
@ -67,8 +66,12 @@ impl<'a> Display for TestLookupError<'a> {
impl<'a> Error for TestLookupError<'a> {} impl<'a> Error for TestLookupError<'a> {}
impl<'a> Context<'a> for TestContextPlain { impl<'a> FunctorContext<'a> for TestContextPlain {
type T = instances::solo::SoloInstance; type T = instances::solo::SoloInstance;
}
impl<'a> Context<'a> for TestContextPlain {
type _Tm = Self::T;
type Fallible = instances::result::ResultFailAny; type Fallible = instances::result::ResultFailAny;

View File

@ -1,6 +1,6 @@
use std::cmp::max; use std::cmp::max;
use crate::func::*; use crate::func::{context::*, *};
use crate::rcore::*; use crate::rcore::*;
use crate::rstd::typeless::*; use crate::rstd::typeless::*;
@ -8,8 +8,12 @@ use super::*;
pub struct TestContextCounted; pub struct TestContextCounted;
impl<'a> Context<'a> for TestContextCounted { impl<'a> FunctorContext<'a> for TestContextCounted {
type T = CountedInstance; type T = CountedInstance;
}
impl<'a> Context<'a> for TestContextCounted {
type _Tm = Self::T;
type Fallible = instances::result::ResultFailOver<Self::T>; type Fallible = instances::result::ResultFailOver<Self::T>;

View File

@ -1,3 +1,4 @@
use crate::func::context::*;
use crate::rcore::*; use crate::rcore::*;
use crate::rstd::tracing::*; use crate::rstd::tracing::*;
@ -5,8 +6,12 @@ use super::*;
pub struct TestContextTraced; pub struct TestContextTraced;
impl<'a> Context<'a> for TestContextTraced { impl<'a> FunctorContext<'a> for TestContextTraced {
type T = TracedInstance; type T = TracedInstance;
}
impl<'a> Context<'a> for TestContextTraced {
type _Tm = Self::T;
type Fallible = instances::result::ResultFailOver<Self::T>; type Fallible = instances::result::ResultFailOver<Self::T>;