From 7b8221b4c33bb0c743de339cea32bea9da883a8d Mon Sep 17 00:00:00 2001 From: timofey Date: Tue, 30 May 2023 01:27:57 +0000 Subject: [PATCH] restructuring --- src/flow/binary.rs | 3 +- src/func.rs | 71 ++-------------------------- src/func/extensions.rs | 34 +++++++++++++ src/func/shared.rs | 35 ++++++++++++++ src/rcore.rs | 17 ++++--- src/rcore/serialization.rs | 4 +- src/rstd/tracing.rs | 8 ++-- src/rstd/tracing/rendered_display.rs | 24 +++++----- 8 files changed, 103 insertions(+), 93 deletions(-) create mode 100644 src/func/extensions.rs create mode 100644 src/func/shared.rs diff --git a/src/flow/binary.rs b/src/flow/binary.rs index 2c7378c..71469f2 100644 --- a/src/flow/binary.rs +++ b/src/flow/binary.rs @@ -2,7 +2,8 @@ mod avl; use std::rc::Rc; -use crate::{flow::comparator::*, func::*}; +use crate::flow::comparator::*; +use crate::func::*; pub type KeyRc<'a, BT> = Rc<>::Key>; diff --git a/src/func.rs b/src/func.rs index 95d2d96..b1062c2 100644 --- a/src/func.rs +++ b/src/func.rs @@ -12,7 +12,9 @@ pub mod clone_func; mod controlflow; pub mod copy_func; pub mod derivations; +mod extensions; pub mod instances; +mod shared; #[cfg(test)] pub mod test_suite; #[cfg(test)] @@ -27,8 +29,10 @@ pub use self::applicative_select::{ }; use self::controlflow::{BindableMut, ControlFlowInstance}; pub use self::controlflow::{Iterative, IterativeWrapped}; +pub use self::extensions::{MonadExt, MonadFailAnyExt}; #[cfg(doc)] use self::instances::stackless::StacklessInstance; +pub use self::shared::{SharedFunctor, SharedFunctorAny}; pub trait WeakFunctorAny { /// Type of the wrapped value. @@ -137,27 +141,6 @@ pub trait Monad<'a>: Applicative<'a> { } } -pub trait MonadExt<'a>: Monad<'a> { - /// [`FnMut`] version of [`Monad::iterate`]. - /// Reasoning for this method existing at all is that - /// most usecases are better modelled with [`FnMut`] - /// rather than some dedicated state type. - fn iterate_mut( - a: A, - f: impl 'a + FnMut(A) -> Self::F>, - ) -> Self::F { - Self::iterate(BindableMut::new(a, f)) - } - - fn bind2( - fa: Self::F, - fb: Self::F, - f: impl 'a + FnOnce(A, B) -> Self::F, - ) -> Self::F { - Self::join(Self::la2(fa, fb, f)) - } -} - impl<'a, T: Monad<'a>> MonadExt<'a> for T {} /// Part of [`MonadFail`] responsible for Haskell's `fail`. @@ -255,49 +238,3 @@ pub trait MonadFailAny<'a>: 'a { } pub type WrapE<'a, A, E, Fallible> = Wrap<'a, A, >::W>; - -pub trait MonadFailAnyExt<'a>: MonadFailAny<'a> { - fn pure(a: A) -> WrapE<'a, A, E, Self> { - as Pure>::pure(a) - } - - fn fail(e: E) -> WrapE<'a, A, E, Self> { - as Fail>::fail(e) - } -} - -impl<'a, Fallible: ?Sized + MonadFailAny<'a>> MonadFailAnyExt<'a> for Fallible {} - -pub trait SharedFunctorAny: WeakFunctorAny { - type SharedAny<'a, A: 'a + Clone>: 'a + Clone - where - Self: 'a; - - fn share<'a, A: 'a + Clone>(fa: Self::FAny<'a, A>) -> Self::SharedAny<'a, A> - where - Self: 'a; - - fn unshare<'a, A: 'a + Clone>(sa: Self::SharedAny<'a, A>) -> Self::FAny<'a, A> - where - Self: 'a; -} - -pub trait SharedFunctor<'a>: WeakFunctor<'a> { - type Shared: 'a + Clone; - - fn share(fa: Self::F) -> Self::Shared; - - fn unshare(sa: Self::Shared) -> Self::F; -} - -impl<'a, T: 'a + SharedFunctorAny> SharedFunctor<'a> for T { - type Shared = T::SharedAny<'a, A>; - - fn share(fa: Self::F) -> Self::Shared { - T::share(fa) - } - - fn unshare(sa: Self::Shared) -> Self::F { - T::unshare(sa) - } -} diff --git a/src/func/extensions.rs b/src/func/extensions.rs new file mode 100644 index 0000000..0e2e7e8 --- /dev/null +++ b/src/func/extensions.rs @@ -0,0 +1,34 @@ +use super::*; + +pub trait MonadExt<'a>: Monad<'a> { + /// [`FnMut`] version of [`Monad::iterate`]. + /// Reasoning for this method existing at all is that + /// most usecases are better modelled with [`FnMut`] + /// rather than some dedicated state type. + fn iterate_mut( + a: A, + f: impl 'a + FnMut(A) -> Self::F>, + ) -> Self::F { + Self::iterate(BindableMut::new(a, f)) + } + + fn bind2( + fa: Self::F, + fb: Self::F, + f: impl 'a + FnOnce(A, B) -> Self::F, + ) -> Self::F { + Self::join(Self::la2(fa, fb, f)) + } +} + +pub trait MonadFailAnyExt<'a>: MonadFailAny<'a> { + fn pure(a: A) -> WrapE<'a, A, E, Self> { + as Pure>::pure(a) + } + + fn fail(e: E) -> WrapE<'a, A, E, Self> { + as Fail>::fail(e) + } +} + +impl<'a, Fallible: ?Sized + MonadFailAny<'a>> MonadFailAnyExt<'a> for Fallible {} diff --git a/src/func/shared.rs b/src/func/shared.rs new file mode 100644 index 0000000..7a8400e --- /dev/null +++ b/src/func/shared.rs @@ -0,0 +1,35 @@ +use super::*; + +pub trait SharedFunctorAny: WeakFunctorAny { + type SharedAny<'a, A: 'a + Clone>: 'a + Clone + where + Self: 'a; + + fn share<'a, A: 'a + Clone>(fa: Self::FAny<'a, A>) -> Self::SharedAny<'a, A> + where + Self: 'a; + + fn unshare<'a, A: 'a + Clone>(sa: Self::SharedAny<'a, A>) -> Self::FAny<'a, A> + where + Self: 'a; +} + +pub trait SharedFunctor<'a>: WeakFunctor<'a> { + type Shared: 'a + Clone; + + fn share(fa: Self::F) -> Self::Shared; + + fn unshare(sa: Self::Shared) -> Self::F; +} + +impl<'a, T: 'a + SharedFunctorAny> SharedFunctor<'a> for T { + type Shared = T::SharedAny<'a, A>; + + fn share(fa: Self::F) -> Self::Shared { + T::share(fa) + } + + fn unshare(sa: Self::Shared) -> Self::F { + T::unshare(sa) + } +} diff --git a/src/rcore.rs b/src/rcore.rs index e2b22cf..eb30212 100644 --- a/src/rcore.rs +++ b/src/rcore.rs @@ -16,14 +16,17 @@ use std::{error::Error, rc::Rc}; use crate::func::*; -pub use self::addresses::*; -pub use self::hashing::*; -pub use self::origin::*; -pub use self::point::*; +pub use self::addresses::Addresses; +pub use self::hashing::{Hash, HASH_SIZE, HASH_ZEROS}; +pub use self::origin::{OFctr, Origin}; +pub use self::point::Point; pub use self::points::TakesPoints; -pub use self::resolution::*; -pub use self::serialization::*; -pub use self::slice_deserializer::*; +pub use self::resolution::{ + Address, HashResolution, HashResolutionResult, Resolution, ResolutionError, ResolutionFailure, + ResolutionResult, Resolver, +}; +pub use self::serialization::{Deserializer, DeserializerExt, Serializable, Serializer}; +pub use self::slice_deserializer::SliceDeserializer; /// Basic support for tracing events across the execution. pub trait Diagnostic<'a, T: Monad<'a>> { diff --git a/src/rcore/serialization.rs b/src/rcore/serialization.rs index 09508d9..92431e4 100644 --- a/src/rcore/serialization.rs +++ b/src/rcore/serialization.rs @@ -41,12 +41,12 @@ pub trait Deserializer { } /// Extension trait for [Deserializer]s. -pub trait ExtDeserializer { +pub trait DeserializerExt { /// Try to read exactly `N` bytes. fn read_n_const(&mut self) -> Result<[u8; N], &[u8]>; } -impl ExtDeserializer for D { +impl DeserializerExt for D { fn read_n_const(&mut self) -> Result<[u8; N], &[u8]> { let slice = self.read_n(N); match slice.try_into() { diff --git a/src/rstd/tracing.rs b/src/rstd/tracing.rs index 224077f..e0792c5 100644 --- a/src/rstd/tracing.rs +++ b/src/rstd/tracing.rs @@ -10,10 +10,12 @@ use crate::rcore::*; use super::*; -pub use self::rendered::*; +pub use self::rendered::{ + RenderedAny, RenderedCommon, RenderedLong, RenderedWide, WithLengthAndWidth, +}; use self::trace::*; -pub use self::traceable::*; -pub use self::traced::*; +pub use self::traceable::Traceable; +pub use self::traced::Traced; /// [`Diagnostic`] for [Traced] objects. /// diff --git a/src/rstd/tracing/rendered_display.rs b/src/rstd/tracing/rendered_display.rs index c667338..3e2a476 100644 --- a/src/rstd/tracing/rendered_display.rs +++ b/src/rstd/tracing/rendered_display.rs @@ -11,9 +11,9 @@ impl Display for WithLengthAndWidth { impl Display for RenderedAny { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - RenderedAny::Common(common) => f.write_fmt(format_args!("{}", common)), - RenderedAny::Wide(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))), - RenderedAny::Long(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))), + Self::Common(common) => f.write_fmt(format_args!("{}", common)), + Self::Wide(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))), + Self::Long(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))), } } } @@ -21,12 +21,10 @@ impl Display for RenderedAny { impl Display for RenderedCommon { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - RenderedCommon::Empty => f.write_fmt(format_args!(".")), - RenderedCommon::Resolution => f.write_fmt(format_args!("?")), - RenderedCommon::Event(event) => f.write_fmt(format_args!("{}", event)), - RenderedCommon::Action { name, rendered } => { - f.write_fmt(format_args!("{} @ {}", name, rendered)) - } + Self::Empty => f.write_fmt(format_args!(".")), + Self::Resolution => f.write_fmt(format_args!("?")), + Self::Event(event) => f.write_fmt(format_args!("{}", event)), + Self::Action { name, rendered } => f.write_fmt(format_args!("{} @ {}", name, rendered)), } } } @@ -62,8 +60,8 @@ impl Display for RenderVec<&Vec>> { impl Display for RenderedLong { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - RenderedLong::Common(common) => f.write_fmt(format_args!("{}", common)), - RenderedLong::Long(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))), + Self::Common(common) => f.write_fmt(format_args!("{}", common)), + Self::Long(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))), } } } @@ -71,8 +69,8 @@ impl Display for RenderedLong { impl Display for RenderedWide { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - RenderedWide::Common(common) => f.write_fmt(format_args!("{}", common)), - RenderedWide::Wide(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))), + Self::Common(common) => f.write_fmt(format_args!("{}", common)), + Self::Wide(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))), } } }