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> {
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<Ctx: Context<'a>>(self) -> HashResolution<'a, Ctx>
where
Ctx::LookupError: From<CastError<'a>>,
{
Ctx::pure(Err(self.into()))
fn pure<Ctx: CastCtx<'a>>(self) -> HashResolution<'a, Ctx> {
Ctx::pure(Err(Ctx::from_cast(self)))
}
}
impl<'a, Ctx: Context<'a>> CastResolver<'a, Ctx> {
fn rc(points: Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>) -> Rc<dyn Resolver<'a, Ctx>>
where
Ctx::LookupError: From<CastError<'a>>,
{
impl<'a, Ctx: CastCtx<'a>> CastResolver<'a, Ctx> {
fn rc(points: Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>) -> Rc<dyn Resolver<'a, Ctx>> {
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<CastError<'a>>,
{
) -> 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<CastError<'a>>,
{
impl<'a, Ctx: CastCtx<'a>> Resolver<'a, Ctx> for CastResolver<'a, Ctx> {
fn resolve(self: Rc<Self>, 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<CastError<'a>>,
{
impl<'a, Ctx: CastCtx<'a>> TypelessMentionable<'a, Ctx> {
pub fn cast_full<A: Mentionable<'a, Ctx>>(
&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<dyn Origin<'a, Ctx, Mtbl = TypelessMentionable<'a, Ctx>>>,
factory: A::Fctr,
) -> Resolution<'a, Ctx, A>
where
Ctx::LookupError: From<CastError<'a>>,
{
) -> 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<dyn Origin<'a, Ctx, Mtbl = TypelessMentionable<'a, Ctx>>>,
factory: A::Fctr,
) -> Rc<dyn Origin<'a, Ctx, Mtbl = A>>
where
Ctx::LookupError: From<CastError<'a>>,
{
) -> Rc<dyn Origin<'a, Ctx, Mtbl = A>> {
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<CastError<'a>>,
{
impl<'a, Ctx: CastCtx<'a>> Point<'a, Ctx, TypelessMentionable<'a, Ctx>> {
fn cast_origin<A: Mentionable<'a, Ctx>>(
&self,
factory: A::Fctr,

View File

@ -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<CastError<'a>>,
{
impl<'a, Ctx: CastCtx<'a>> TypelessMentionable<'a, Ctx> {
pub fn from_typed<A: Mentionable<'a, Ctx>>(mentionable: Rc<A>) -> 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<CastError<'a>>,
{
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<CastError<'a>>,
{
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<CastError<'a>>,
{
impl<'a, Ctx: CastCtx<'a>> TypelessFactory<'a, Ctx> {
pub fn from_typed<F: Factory<'a, Ctx>>(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<CastError<'a>>,
{
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<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>;
}
impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableExt<'a, Ctx> for A
where
Ctx::LookupError: From<CastError<'a>>,
{
impl<'a, Ctx: CastCtx<'a>, A: Mentionable<'a, Ctx>> MentionableExt<'a, Ctx> for A {
fn points_typeless(&self, points: &mut Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>) {
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>>>
where
Ctx::LookupError: From<CastError<'a>>,
{
fn visit<A: Mentionable<'a, Ctx>>(&mut self, point: &Point<'a, Ctx, A>) {
self.push(point.typeless());