From 1acce76b0bee58fbc9568bac573c0e5c6c3fbb4e Mon Sep 17 00:00:00 2001 From: timofey Date: Tue, 1 Aug 2023 20:33:51 +0000 Subject: [PATCH] `CastCtx` --- src/rstd/cast.rs | 69 +++++++++++++++++++------------------------- src/rstd/typeless.rs | 36 +++++------------------ 2 files changed, 38 insertions(+), 67 deletions(-) diff --git a/src/rstd/cast.rs b/src/rstd/cast.rs index 3c08f50..ac2abf2 100644 --- a/src/rstd/cast.rs +++ b/src/rstd/cast.rs @@ -39,6 +39,19 @@ pub enum CastError<'a> { }, } +pub trait CastCtx<'a>: Context<'a> { + fn from_cast(cast_error: CastError<'a>) -> Self::LookupError; +} + +impl<'a, Ctx: Context<'a>> CastCtx<'a> for Ctx +where + Self::LookupError: From>, +{ + fn from_cast(cast_error: CastError<'a>) -> Self::LookupError { + cast_error.into() + } +} + impl<'a> Display for CastError<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -64,19 +77,13 @@ impl<'a> Display for CastError<'a> { } impl<'a> CastError<'a> { - fn pure>(self) -> HashResolution<'a, Ctx> - where - Ctx::LookupError: From>, - { - Ctx::pure(Err(self.into())) + fn pure>(self) -> HashResolution<'a, Ctx> { + Ctx::pure(Err(Ctx::from_cast(self))) } } -impl<'a, Ctx: Context<'a>> CastResolver<'a, Ctx> { - fn rc(points: Vec>>) -> Rc> - where - Ctx::LookupError: From>, - { +impl<'a, Ctx: CastCtx<'a>> CastResolver<'a, Ctx> { + fn rc(points: Vec>>) -> Rc> { Rc::new(Self { points }) } @@ -119,12 +126,9 @@ impl<'a, Ctx: Context<'a>> CastResolver<'a, Ctx> { } } -fn cast_resolved<'a, Ctx: Context<'a>>( +fn cast_resolved<'a, Ctx: CastCtx<'a>>( resolved: ResolutionResult<'a, Ctx, TypelessMentionable<'a, Ctx>>, -) -> HashResolutionResult<'a, Ctx> -where - Ctx::LookupError: From>, -{ +) -> HashResolutionResult<'a, Ctx> { match resolved { Ok(mentionable) => Ok(( mentionable.bytes(), @@ -132,15 +136,12 @@ where )), Err(error) => Err(match error { ResolutionError::Lookup(lookup_error) => lookup_error, - ResolutionError::Parse(parse_error) => CastError::Typeless(parse_error).into(), + ResolutionError::Parse(parse_error) => Ctx::from_cast(CastError::Typeless(parse_error)), }), } } -impl<'a, Ctx: Context<'a>> Resolver<'a, Ctx> for CastResolver<'a, Ctx> -where - Ctx::LookupError: From>, -{ +impl<'a, Ctx: CastCtx<'a>> Resolver<'a, Ctx> for CastResolver<'a, Ctx> { fn resolve(self: Rc, address: Address) -> HashResolution<'a, Ctx> { let point = match self.get_point(address) { Ok(point) => point, @@ -150,10 +151,7 @@ where } } -impl<'a, Ctx: Context<'a>> TypelessMentionable<'a, Ctx> -where - Ctx::LookupError: From>, -{ +impl<'a, Ctx: CastCtx<'a>> TypelessMentionable<'a, Ctx> { pub fn cast_full>( &self, factory: A::Fctr, @@ -171,13 +169,10 @@ where } } -fn cast_resolve<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>>( +fn cast_resolve<'a, Ctx: CastCtx<'a>, A: Mentionable<'a, Ctx>>( typeless_origin: Rc>>, factory: A::Fctr, -) -> Resolution<'a, Ctx, A> -where - Ctx::LookupError: From>, -{ +) -> Resolution<'a, Ctx, A> { Ctx::fmap( typeless_origin.clone().resolve(), move |resolved| match resolved { @@ -187,19 +182,18 @@ where }, Err(error) => Err(ResolutionError::Lookup(match error { ResolutionError::Lookup(lookup_error) => lookup_error, - ResolutionError::Parse(parse_error) => CastError::Typeless(parse_error).into(), + ResolutionError::Parse(parse_error) => { + Ctx::from_cast(CastError::Typeless(parse_error)) + } })), }, ) } -fn cast_origin<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>>( +fn cast_origin<'a, Ctx: CastCtx<'a>, A: Mentionable<'a, Ctx>>( typeless_origin: Rc>>, factory: A::Fctr, -) -> Rc> -where - Ctx::LookupError: From>, -{ +) -> Rc> { let origin_rb = typeless_origin.clone(); wrapped_origin( factory.clone(), @@ -208,10 +202,7 @@ where ) } -impl<'a, Ctx: Context<'a>> Point<'a, Ctx, TypelessMentionable<'a, Ctx>> -where - Ctx::LookupError: From>, -{ +impl<'a, Ctx: CastCtx<'a>> Point<'a, Ctx, TypelessMentionable<'a, Ctx>> { fn cast_origin>( &self, factory: A::Fctr, diff --git a/src/rstd/typeless.rs b/src/rstd/typeless.rs index e8a18ad..4a2f48f 100644 --- a/src/rstd/typeless.rs +++ b/src/rstd/typeless.rs @@ -2,7 +2,7 @@ //! //! [`rcore`]: crate::rcore -use super::{cast::CastError, wrapped_origin::*, *}; +use super::{cast::*, wrapped_origin::*, *}; use crate::mode::*; type TypelessSerialize<'a> = dyn 'a + Fn(&mut dyn Serializer); @@ -112,10 +112,7 @@ impl<'a, Ctx: Context<'a>> CRegularFactory<'a, Ctx> for TypelessFactory<'a, Ctx> } } -impl<'a, Ctx: Context<'a>> TypelessMentionable<'a, Ctx> -where - Ctx::LookupError: From>, -{ +impl<'a, Ctx: CastCtx<'a>> TypelessMentionable<'a, Ctx> { pub fn from_typed>(mentionable: Rc) -> Self { let factory = TypelessFactory::from_typed(mentionable.factory()); let topology = mentionable.topology(); @@ -129,10 +126,7 @@ where } } -impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tde<'a, Ctx> for F -where - Ctx::LookupError: From>, -{ +impl<'a, Ctx: CastCtx<'a>, F: Factory<'a, Ctx>> Tde<'a, Ctx> for F { fn clone_box(&self) -> TdeBox<'a, Ctx> { Box::new(self.clone()) } @@ -148,10 +142,7 @@ where } } -impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tut<'a, Ctx> for F -where - Ctx::LookupError: From>, -{ +impl<'a, Ctx: CastCtx<'a>, F: Factory<'a, Ctx>> Tut<'a, Ctx> for F { fn clone_box(&self) -> TutBox<'a, Ctx> { Box::new(self.clone()) } @@ -174,10 +165,7 @@ where } } -impl<'a, Ctx: Context<'a>> TypelessFactory<'a, Ctx> -where - Ctx::LookupError: From>, -{ +impl<'a, Ctx: CastCtx<'a>> TypelessFactory<'a, Ctx> { pub fn from_typed>(factory: F) -> Self { TypelessFactory { t_deserialize: Box::new(factory.clone()), @@ -192,10 +180,7 @@ impl<'a> TypelessError<'a> { } } -impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> Point<'a, Ctx, A> -where - Ctx::LookupError: From>, -{ +impl<'a, Ctx: CastCtx<'a>, A: Mentionable<'a, Ctx>> Point<'a, Ctx, A> { /// Typeless version of the point. pub fn typeless(&self) -> Point<'a, Ctx, TypelessMentionable<'a, Ctx>> { Point { @@ -216,10 +201,7 @@ pub trait MentionableExt<'a, Ctx: Context<'a>>: Mentionable<'a, Ctx> { fn points_vec(&self) -> Vec>>; } -impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableExt<'a, Ctx> for A -where - Ctx::LookupError: From>, -{ +impl<'a, Ctx: CastCtx<'a>, A: Mentionable<'a, Ctx>> MentionableExt<'a, Ctx> for A { fn points_typeless(&self, points: &mut Vec>>) { self.points_typed(points) } @@ -231,10 +213,8 @@ where } } -impl<'a, Ctx: Context<'a>> PointsVisitor<'a, Ctx> +impl<'a, Ctx: CastCtx<'a>> PointsVisitor<'a, Ctx> for Vec>> -where - Ctx::LookupError: From>, { fn visit>(&mut self, point: &Point<'a, Ctx, A>) { self.push(point.typeless());