This commit is contained in:
AF 2023-08-01 20:33:51 +00:00
parent e6ff986e42
commit 1acce76b0b
2 changed files with 38 additions and 67 deletions

View File

@ -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<CastError<'a>>,
{
fn from_cast(cast_error: CastError<'a>) -> Self::LookupError {
cast_error.into()
}
}
impl<'a> Display for CastError<'a> { impl<'a> Display for CastError<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
@ -64,19 +77,13 @@ impl<'a> Display for CastError<'a> {
} }
impl<'a> CastError<'a> { impl<'a> CastError<'a> {
fn pure<Ctx: Context<'a>>(self) -> HashResolution<'a, Ctx> fn pure<Ctx: CastCtx<'a>>(self) -> HashResolution<'a, Ctx> {
where Ctx::pure(Err(Ctx::from_cast(self)))
Ctx::LookupError: From<CastError<'a>>,
{
Ctx::pure(Err(self.into()))
} }
} }
impl<'a, Ctx: Context<'a>> CastResolver<'a, Ctx> { impl<'a, Ctx: CastCtx<'a>> CastResolver<'a, Ctx> {
fn rc(points: Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>) -> Rc<dyn Resolver<'a, Ctx>> fn rc(points: Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>) -> Rc<dyn Resolver<'a, Ctx>> {
where
Ctx::LookupError: From<CastError<'a>>,
{
Rc::new(Self { points }) 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>>, resolved: ResolutionResult<'a, Ctx, TypelessMentionable<'a, Ctx>>,
) -> HashResolutionResult<'a, Ctx> ) -> HashResolutionResult<'a, Ctx> {
where
Ctx::LookupError: From<CastError<'a>>,
{
match resolved { match resolved {
Ok(mentionable) => Ok(( Ok(mentionable) => Ok((
mentionable.bytes(), mentionable.bytes(),
@ -132,15 +136,12 @@ where
)), )),
Err(error) => Err(match error { Err(error) => Err(match error {
ResolutionError::Lookup(lookup_error) => lookup_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> impl<'a, Ctx: CastCtx<'a>> Resolver<'a, Ctx> for CastResolver<'a, Ctx> {
where
Ctx::LookupError: From<CastError<'a>>,
{
fn resolve(self: Rc<Self>, address: Address) -> HashResolution<'a, Ctx> { fn resolve(self: Rc<Self>, address: Address) -> HashResolution<'a, Ctx> {
let point = match self.get_point(address) { let point = match self.get_point(address) {
Ok(point) => point, Ok(point) => point,
@ -150,10 +151,7 @@ where
} }
} }
impl<'a, Ctx: Context<'a>> TypelessMentionable<'a, Ctx> impl<'a, Ctx: CastCtx<'a>> TypelessMentionable<'a, Ctx> {
where
Ctx::LookupError: From<CastError<'a>>,
{
pub fn cast_full<A: Mentionable<'a, Ctx>>( pub fn cast_full<A: Mentionable<'a, Ctx>>(
&self, &self,
factory: A::Fctr, 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<dyn Origin<'a, Ctx, Mtbl = TypelessMentionable<'a, Ctx>>>, typeless_origin: Rc<dyn Origin<'a, Ctx, Mtbl = TypelessMentionable<'a, Ctx>>>,
factory: A::Fctr, factory: A::Fctr,
) -> Resolution<'a, Ctx, A> ) -> Resolution<'a, Ctx, A> {
where
Ctx::LookupError: From<CastError<'a>>,
{
Ctx::fmap( Ctx::fmap(
typeless_origin.clone().resolve(), typeless_origin.clone().resolve(),
move |resolved| match resolved { move |resolved| match resolved {
@ -187,19 +182,18 @@ where
}, },
Err(error) => Err(ResolutionError::Lookup(match error { Err(error) => Err(ResolutionError::Lookup(match error {
ResolutionError::Lookup(lookup_error) => lookup_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<dyn Origin<'a, Ctx, Mtbl = TypelessMentionable<'a, Ctx>>>, typeless_origin: Rc<dyn Origin<'a, Ctx, Mtbl = TypelessMentionable<'a, Ctx>>>,
factory: A::Fctr, factory: A::Fctr,
) -> Rc<dyn Origin<'a, Ctx, Mtbl = A>> ) -> Rc<dyn Origin<'a, Ctx, Mtbl = A>> {
where
Ctx::LookupError: From<CastError<'a>>,
{
let origin_rb = typeless_origin.clone(); let origin_rb = typeless_origin.clone();
wrapped_origin( wrapped_origin(
factory.clone(), factory.clone(),
@ -208,10 +202,7 @@ where
) )
} }
impl<'a, Ctx: Context<'a>> Point<'a, Ctx, TypelessMentionable<'a, Ctx>> impl<'a, Ctx: CastCtx<'a>> Point<'a, Ctx, TypelessMentionable<'a, Ctx>> {
where
Ctx::LookupError: From<CastError<'a>>,
{
fn cast_origin<A: Mentionable<'a, Ctx>>( fn cast_origin<A: Mentionable<'a, Ctx>>(
&self, &self,
factory: A::Fctr, factory: A::Fctr,

View File

@ -2,7 +2,7 @@
//! //!
//! [`rcore`]: crate::rcore //! [`rcore`]: crate::rcore
use super::{cast::CastError, wrapped_origin::*, *}; use super::{cast::*, wrapped_origin::*, *};
use crate::mode::*; use crate::mode::*;
type TypelessSerialize<'a> = dyn 'a + Fn(&mut dyn Serializer); 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> impl<'a, Ctx: CastCtx<'a>> TypelessMentionable<'a, Ctx> {
where
Ctx::LookupError: From<CastError<'a>>,
{
pub fn from_typed<A: Mentionable<'a, Ctx>>(mentionable: Rc<A>) -> Self { pub fn from_typed<A: Mentionable<'a, Ctx>>(mentionable: Rc<A>) -> Self {
let factory = TypelessFactory::from_typed(mentionable.factory()); let factory = TypelessFactory::from_typed(mentionable.factory());
let topology = mentionable.topology(); let topology = mentionable.topology();
@ -129,10 +126,7 @@ where
} }
} }
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tde<'a, Ctx> for F impl<'a, Ctx: CastCtx<'a>, F: Factory<'a, Ctx>> Tde<'a, Ctx> for F {
where
Ctx::LookupError: From<CastError<'a>>,
{
fn clone_box(&self) -> TdeBox<'a, Ctx> { fn clone_box(&self) -> TdeBox<'a, Ctx> {
Box::new(self.clone()) Box::new(self.clone())
} }
@ -148,10 +142,7 @@ where
} }
} }
impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Tut<'a, Ctx> for F impl<'a, Ctx: CastCtx<'a>, F: Factory<'a, Ctx>> Tut<'a, Ctx> for F {
where
Ctx::LookupError: From<CastError<'a>>,
{
fn clone_box(&self) -> TutBox<'a, Ctx> { fn clone_box(&self) -> TutBox<'a, Ctx> {
Box::new(self.clone()) Box::new(self.clone())
} }
@ -174,10 +165,7 @@ where
} }
} }
impl<'a, Ctx: Context<'a>> TypelessFactory<'a, Ctx> impl<'a, Ctx: CastCtx<'a>> TypelessFactory<'a, Ctx> {
where
Ctx::LookupError: From<CastError<'a>>,
{
pub fn from_typed<F: Factory<'a, Ctx>>(factory: F) -> Self { pub fn from_typed<F: Factory<'a, Ctx>>(factory: F) -> Self {
TypelessFactory { TypelessFactory {
t_deserialize: Box::new(factory.clone()), 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> impl<'a, Ctx: CastCtx<'a>, A: Mentionable<'a, Ctx>> Point<'a, Ctx, A> {
where
Ctx::LookupError: From<CastError<'a>>,
{
/// Typeless version of the point. /// Typeless version of the point.
pub fn typeless(&self) -> Point<'a, Ctx, TypelessMentionable<'a, Ctx>> { pub fn typeless(&self) -> Point<'a, Ctx, TypelessMentionable<'a, Ctx>> {
Point { Point {
@ -216,10 +201,7 @@ pub trait MentionableExt<'a, Ctx: Context<'a>>: Mentionable<'a, Ctx> {
fn points_vec(&self) -> Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>; fn points_vec(&self) -> Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>;
} }
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableExt<'a, Ctx> for A impl<'a, Ctx: CastCtx<'a>, A: Mentionable<'a, Ctx>> MentionableExt<'a, Ctx> for A {
where
Ctx::LookupError: From<CastError<'a>>,
{
fn points_typeless(&self, points: &mut Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>) { fn points_typeless(&self, points: &mut Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>) {
self.points_typed(points) 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<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>> for Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>
where
Ctx::LookupError: From<CastError<'a>>,
{ {
fn visit<A: Mentionable<'a, Ctx>>(&mut self, point: &Point<'a, Ctx, A>) { fn visit<A: Mentionable<'a, Ctx>>(&mut self, point: &Point<'a, Ctx, A>) {
self.push(point.typeless()); self.push(point.typeless());