merge ctypeless+typeless

This commit is contained in:
AF 2023-05-07 17:56:23 +00:00
parent 28065991f7
commit 5d1349cd80
8 changed files with 174 additions and 178 deletions

View File

@ -77,7 +77,7 @@ pub type ParseResult<'a, Ctx, F> =
Result<<F as Factory<'a, Ctx>>::Mtbl, <F as Factory<'a, Ctx>>::ParseError>; Result<<F as Factory<'a, Ctx>>::Mtbl, <F as Factory<'a, Ctx>>::ParseError>;
/// Trait representing deserialisation rules for [Mentionable]s. /// Trait representing deserialisation rules for [Mentionable]s.
/// Crucial for [`crate::std::ctypeless`]. /// Crucial for [`crate::std::typeless`].
pub trait Factory<'a, Ctx: 'a + Context>: 'a + Send + Sync + Clone { pub trait Factory<'a, Ctx: 'a + Context>: 'a + Send + Sync + Clone {
/// Type of the associated objects. /// Type of the associated objects.
type Mtbl: Mentionable<'a, Ctx, Fctr = Self>; type Mtbl: Mentionable<'a, Ctx, Fctr = Self>;

View File

@ -3,14 +3,13 @@
pub mod atomic; pub mod atomic;
pub mod cast; pub mod cast;
pub mod collections; pub mod collections;
pub mod ctypeless;
pub mod fallible; pub mod fallible;
pub mod inlining; pub mod inlining;
mod local_origin; mod local_origin;
pub mod nullable; pub mod nullable;
pub mod point; pub mod point;
pub mod tracing; pub mod tracing;
mod typeless; pub mod typeless;
mod wrapped_origin; mod wrapped_origin;
use std::{error::Error, fmt::Display, rc::Rc}; use std::{error::Error, fmt::Display, rc::Rc};

View File

@ -7,7 +7,7 @@ use std::convert::identity;
use crate::core::*; use crate::core::*;
use super::{ctypeless::*, typeless::*, wrapped_origin::*, *}; use super::{typeless::*, wrapped_origin::*, *};
struct CastResolver<'a, Ctx: 'a + Context> { struct CastResolver<'a, Ctx: 'a + Context> {
points: Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>, points: Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>,

View File

@ -1,169 +0,0 @@
//! Previously part of [`crate::core`].
use super::{typeless::*, *};
type TypelessSerialize<'a> = dyn 'a + Fn(&mut dyn Serializer);
/// See [`Point::typeless`].
pub struct TypelessMentionable<'a, Ctx: 'a + Context> {
t_serialize: Box<TypelessSerialize<'a>>,
t_factory: TypelessFactory<'a, Ctx>,
t_topology: Hash,
t_points: Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>,
}
type TypelessParsed<'a, Ctx> = Result<TypelessMentionable<'a, Ctx>, Box<dyn 'a + Error>>;
trait Tde<'a, Ctx: 'a + Context>: 'a + Send + Sync {
fn clone_box(&self) -> TdeBox<'a, Ctx>;
fn de(
&self,
deserializer: &mut dyn Deserializer,
resolver: Rc<dyn Resolver<'a, Ctx>>,
addresses: &mut Addresses,
) -> TypelessParsed<'a, Ctx>;
}
type TdeBox<'a, Ctx> = Box<dyn Tde<'a, Ctx>>;
trait Tut<'a, Ctx: 'a + Context>: 'a + Send + Sync {
fn clone_box(&self) -> TutBox<'a, Ctx>;
fn ut(&self, tail: &[u8]) -> TypelessError<'a>;
}
type TutBox<'a, Ctx> = Box<dyn Tut<'a, Ctx>>;
/// See [`Point::typeless`]/[`TypelessMentionable`].
pub struct TypelessFactory<'a, Ctx: 'a + Context> {
t_deserialize: TdeBox<'a, Ctx>,
t_unexpected_tail: TutBox<'a, Ctx>,
}
impl<'a, Ctx: 'a + Context> Serializable for TypelessMentionable<'a, Ctx> {
fn serialize(&self, serializer: &mut dyn Serializer) {
(self.t_serialize)(serializer);
}
}
impl<'a, Ctx: 'a + Context> Mentionable<'a, Ctx> for TypelessMentionable<'a, Ctx> {
type Fctr = TypelessFactory<'a, Ctx>;
fn factory(&self) -> Self::Fctr {
self.t_factory.clone()
}
fn topology(&self) -> Hash {
self.t_topology
}
fn points_typed(&self, points: &mut impl TakesPoints<'a, Ctx>) {
for point in self.t_points.iter() {
points.take(point)
}
}
}
impl<'a, Ctx: 'a + Context> Clone for TypelessFactory<'a, Ctx> {
fn clone(&self) -> Self {
Self {
t_deserialize: self.t_deserialize.clone_box(),
t_unexpected_tail: self.t_unexpected_tail.clone_box(),
}
}
}
/// See [`Point::typeless`]/[`TypelessFactory`].
#[derive(Debug)]
pub struct TypelessError<'a>(Box<dyn 'a + Error>);
impl<'a> Display for TypelessError<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("typeless error: {}", self.0))
}
}
impl<'a> Error for TypelessError<'a> {}
impl<'a, Ctx: 'a + Context> Factory<'a, Ctx> for TypelessFactory<'a, Ctx> {
type Mtbl = TypelessMentionable<'a, Ctx>;
type ParseError = TypelessError<'a>;
fn deserialize(
&self,
deserializer: &mut dyn Deserializer,
resolver: Rc<dyn Resolver<'a, Ctx>>,
addresses: &mut Addresses,
) -> ParseResult<'a, Ctx, Self> {
match self.t_deserialize.de(deserializer, resolver, addresses) {
Ok(mentionable) => Ok(mentionable),
Err(error) => Err(TypelessError(error)),
}
}
fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError {
self.t_unexpected_tail.ut(tail)
}
}
impl<'a, Ctx: 'a + Context> 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();
let points = mentionable.points_vec();
TypelessMentionable {
t_serialize: Box::new(move |serializer| mentionable.serialize(serializer)),
t_factory: factory,
t_topology: topology,
t_points: points,
}
}
}
impl<'a, Ctx: 'a + Context, F: Factory<'a, Ctx>> Tde<'a, Ctx> for F {
fn clone_box(&self) -> TdeBox<'a, Ctx> {
Box::new(self.clone())
}
fn de(
&self,
deserializer: &mut dyn Deserializer,
resolver: Rc<dyn Resolver<'a, Ctx>>,
addresses: &mut Addresses,
) -> TypelessParsed<'a, Ctx> {
match self.deserialize(deserializer, resolver, addresses) {
Ok(mentionable) => Ok(TypelessMentionable::from_typed(Rc::new(mentionable))),
Err(error) => {
let boxed: Box<dyn 'a + Error> = Box::new(error);
Err(boxed)
}
}
}
}
impl<'a, Ctx: 'a + Context, F: Factory<'a, Ctx>> Tut<'a, Ctx> for F {
fn clone_box(&self) -> TutBox<'a, Ctx> {
Box::new(self.clone())
}
fn ut(&self, tail: &[u8]) -> TypelessError<'a> {
TypelessError::from_typed(self.unexpected_tail(tail))
}
}
impl<'a, Ctx: 'a + Context> TypelessFactory<'a, Ctx> {
pub fn from_typed<F: Factory<'a, Ctx>>(factory: F) -> Self {
TypelessFactory {
t_deserialize: Box::new(factory.clone()),
t_unexpected_tail: Box::new(factory),
}
}
}
impl<'a> TypelessError<'a> {
pub fn from_typed<E: 'a + Error>(error: E) -> Self {
TypelessError(Box::new(error))
}
}

View File

@ -1,4 +1,4 @@
use crate::std::{cast::*, ctypeless::*}; use crate::std::{cast::*, typeless::*};
use super::*; use super::*;

View File

@ -1,6 +1,172 @@
use crate::core::*; //! Previously part of [`crate::core`].
use super::{ctypeless::*, wrapped_origin::*}; use super::{wrapped_origin::*, *};
type TypelessSerialize<'a> = dyn 'a + Fn(&mut dyn Serializer);
/// See [`Point::typeless`].
pub struct TypelessMentionable<'a, Ctx: 'a + Context> {
t_serialize: Box<TypelessSerialize<'a>>,
t_factory: TypelessFactory<'a, Ctx>,
t_topology: Hash,
t_points: Vec<Point<'a, Ctx, TypelessMentionable<'a, Ctx>>>,
}
type TypelessParsed<'a, Ctx> = Result<TypelessMentionable<'a, Ctx>, Box<dyn 'a + Error>>;
trait Tde<'a, Ctx: 'a + Context>: 'a + Send + Sync {
fn clone_box(&self) -> TdeBox<'a, Ctx>;
fn de(
&self,
deserializer: &mut dyn Deserializer,
resolver: Rc<dyn Resolver<'a, Ctx>>,
addresses: &mut Addresses,
) -> TypelessParsed<'a, Ctx>;
}
type TdeBox<'a, Ctx> = Box<dyn Tde<'a, Ctx>>;
trait Tut<'a, Ctx: 'a + Context>: 'a + Send + Sync {
fn clone_box(&self) -> TutBox<'a, Ctx>;
fn ut(&self, tail: &[u8]) -> TypelessError<'a>;
}
type TutBox<'a, Ctx> = Box<dyn Tut<'a, Ctx>>;
/// See [`Point::typeless`]/[`TypelessMentionable`].
pub struct TypelessFactory<'a, Ctx: 'a + Context> {
t_deserialize: TdeBox<'a, Ctx>,
t_unexpected_tail: TutBox<'a, Ctx>,
}
impl<'a, Ctx: 'a + Context> Serializable for TypelessMentionable<'a, Ctx> {
fn serialize(&self, serializer: &mut dyn Serializer) {
(self.t_serialize)(serializer);
}
}
impl<'a, Ctx: 'a + Context> Mentionable<'a, Ctx> for TypelessMentionable<'a, Ctx> {
type Fctr = TypelessFactory<'a, Ctx>;
fn factory(&self) -> Self::Fctr {
self.t_factory.clone()
}
fn topology(&self) -> Hash {
self.t_topology
}
fn points_typed(&self, points: &mut impl TakesPoints<'a, Ctx>) {
for point in self.t_points.iter() {
points.take(point)
}
}
}
impl<'a, Ctx: 'a + Context> Clone for TypelessFactory<'a, Ctx> {
fn clone(&self) -> Self {
Self {
t_deserialize: self.t_deserialize.clone_box(),
t_unexpected_tail: self.t_unexpected_tail.clone_box(),
}
}
}
/// See [`Point::typeless`]/[`TypelessFactory`].
#[derive(Debug)]
pub struct TypelessError<'a>(Box<dyn 'a + Error>);
impl<'a> Display for TypelessError<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("typeless error: {}", self.0))
}
}
impl<'a> Error for TypelessError<'a> {}
impl<'a, Ctx: 'a + Context> Factory<'a, Ctx> for TypelessFactory<'a, Ctx> {
type Mtbl = TypelessMentionable<'a, Ctx>;
type ParseError = TypelessError<'a>;
fn deserialize(
&self,
deserializer: &mut dyn Deserializer,
resolver: Rc<dyn Resolver<'a, Ctx>>,
addresses: &mut Addresses,
) -> ParseResult<'a, Ctx, Self> {
match self.t_deserialize.de(deserializer, resolver, addresses) {
Ok(mentionable) => Ok(mentionable),
Err(error) => Err(TypelessError(error)),
}
}
fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError {
self.t_unexpected_tail.ut(tail)
}
}
impl<'a, Ctx: 'a + Context> 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();
let points = mentionable.points_vec();
TypelessMentionable {
t_serialize: Box::new(move |serializer| mentionable.serialize(serializer)),
t_factory: factory,
t_topology: topology,
t_points: points,
}
}
}
impl<'a, Ctx: 'a + Context, F: Factory<'a, Ctx>> Tde<'a, Ctx> for F {
fn clone_box(&self) -> TdeBox<'a, Ctx> {
Box::new(self.clone())
}
fn de(
&self,
deserializer: &mut dyn Deserializer,
resolver: Rc<dyn Resolver<'a, Ctx>>,
addresses: &mut Addresses,
) -> TypelessParsed<'a, Ctx> {
match self.deserialize(deserializer, resolver, addresses) {
Ok(mentionable) => Ok(TypelessMentionable::from_typed(Rc::new(mentionable))),
Err(error) => {
let boxed: Box<dyn 'a + Error> = Box::new(error);
Err(boxed)
}
}
}
}
impl<'a, Ctx: 'a + Context, F: Factory<'a, Ctx>> Tut<'a, Ctx> for F {
fn clone_box(&self) -> TutBox<'a, Ctx> {
Box::new(self.clone())
}
fn ut(&self, tail: &[u8]) -> TypelessError<'a> {
TypelessError::from_typed(self.unexpected_tail(tail))
}
}
impl<'a, Ctx: 'a + Context> TypelessFactory<'a, Ctx> {
pub fn from_typed<F: Factory<'a, Ctx>>(factory: F) -> Self {
TypelessFactory {
t_deserialize: Box::new(factory.clone()),
t_unexpected_tail: Box::new(factory),
}
}
}
impl<'a> TypelessError<'a> {
pub fn from_typed<E: 'a + Error>(error: E) -> Self {
TypelessError(Box::new(error))
}
}
impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>> Point<'a, Ctx, A> { impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>> Point<'a, Ctx, A> {
/// Typeless version of the point. /// Typeless version of the point.

View File

@ -8,7 +8,7 @@ use sha2::{Digest, Sha256};
use crate::core::*; use crate::core::*;
use crate::func::*; use crate::func::*;
use crate::std::cast::*; use crate::std::cast::*;
use crate::std::ctypeless::*; use crate::std::typeless::*;
pub struct NoDiagnostic; pub struct NoDiagnostic;

View File

@ -2,7 +2,7 @@ use std::cmp::max;
use crate::core::*; use crate::core::*;
use crate::func::*; use crate::func::*;
use crate::std::ctypeless::*; use crate::std::typeless::*;
use super::*; use super::*;