rstd::tcast
All checks were successful
buildbot/cargo fmt (1.72) Build done.
buildbot/cargo doc (1.72) Build done.
buildbot/cargo clippy (1.72) Build done.
buildbot/cargo test (1.65) Build done.
buildbot/cargo clippy (1.65) Build done.

This commit is contained in:
AF 2023-09-03 19:53:29 +00:00
parent 34f4f36550
commit 36a12f3053
2 changed files with 79 additions and 0 deletions

View File

@ -12,6 +12,7 @@ mod local_origin;
pub mod nullable; pub mod nullable;
pub mod point; pub mod point;
pub mod singular; pub mod singular;
pub mod tcast;
pub mod tracing; pub mod tracing;
pub mod typeless; pub mod typeless;
mod wrapped_origin; mod wrapped_origin;

78
src/rstd/tcast.rs Normal file
View File

@ -0,0 +1,78 @@
use std::sync::Arc;
use crate::rcore::*;
use super::{singular::*, *};
pub trait CastMentionable<'a, Ctx: SingularCtx<'a>>: Mentionable<'a, Ctx> {
fn m_cast<A: Mentionable<'a, Ctx>>(&self, factory: &Fctr<'a, A>) -> ParseResultA<'a, A> {
factory.parse_slice(
&self.bytes(),
&SingularResolver::from_mentionable(self).robust(),
)
}
}
impl<'a, Ctx: SingularCtx<'a>, B: Mentionable<'a, Ctx>> CastMentionable<'a, Ctx> for B {}
struct CastedOrigin<O: ?Sized, F> {
origin: Arc<O>,
factory: F,
}
impl<'a, Ctx: SingularCtx<'a>, O: ?Sized + Origin<'a, Ctx>, F: Factory<'a, Ctx>> Origin<'a, Ctx>
for CastedOrigin<O, F>
{
type Mtbl = F::Mtbl;
fn factory(&self) -> OFctr<'a, Ctx, Self> {
self.factory.clone()
}
fn resolve(self: Arc<Self>) -> Resolution<'a, Ctx, Self::Mtbl>
where
OFctr<'a, Ctx, Self>: FactoryParse<'a, Ctx>,
{
self.clone().origin.resolve_bytes_map(move |resolved| {
resolved
.map_err(ResolutionError::Lookup)
.and_then(|(src, resolver)| {
self.factory
.parse_slice(&src, &resolver)
.map_err(ResolutionError::Parse)
})
.map(Arc::new)
})
}
fn resolve_bytes(self: Arc<Self>) -> HashResolution<'a, Ctx> {
self.origin.ref_resolve_bytes()
}
}
pub trait CastOrigin<'a, Ctx: SingularCtx<'a>>: Origin<'a, Ctx> {
fn o_cast<A: Mentionable<'a, Ctx>>(
self: &Arc<Self>,
factory: Fctr<'a, A>,
) -> Arc<dyn Origin<'a, Ctx, Mtbl = A>> {
Arc::new(CastedOrigin {
origin: self.clone(),
factory,
}) as _
}
}
impl<'a, Ctx: SingularCtx<'a>, O: ?Sized + Origin<'a, Ctx>> CastOrigin<'a, Ctx> for O {}
pub trait CastPoint<'a, Ctx: SingularCtx<'a>> {
fn p_cast<A: Mentionable<'a, Ctx>>(self, factory: Fctr<'a, A>) -> Point<'a, Ctx, A>;
}
impl<'a, Ctx: SingularCtx<'a>, B: Mentionable<'a, Ctx>> CastPoint<'a, Ctx> for Point<'a, Ctx, B> {
fn p_cast<A: Mentionable<'a, Ctx>>(self, factory: Fctr<'a, A>) -> Point<'a, Ctx, A> {
Point {
point: self.point,
origin: self.origin.o_cast(factory),
}
}
}