From c6b3029798b8bfc9763091d214729808f120703a Mon Sep 17 00:00:00 2001 From: timofey Date: Sat, 29 Jul 2023 08:46:35 +0000 Subject: [PATCH] `MentionableTop` --- src/rcore.rs | 12 +++++++++--- src/rcore/origin.rs | 2 +- src/rcore/point.rs | 2 +- src/rcore/points.rs | 4 ++-- src/rcore/resolution.rs | 4 ++-- src/rstd/atomic.rs | 2 +- src/rstd/atomic/atomic_object.rs | 4 +++- src/rstd/cast.rs | 2 +- src/rstd/collections/stack.rs | 4 ++++ src/rstd/collections/tree.rs | 4 ++++ src/rstd/inlining/static_pair.rs | 8 ++++++-- src/rstd/nullable.rs | 4 ++++ src/rstd/point.rs | 2 ++ src/rstd/typeless.rs | 4 +++- 14 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/rcore.rs b/src/rcore.rs index 0ce8b6b..4bcbe3e 100644 --- a/src/rcore.rs +++ b/src/rcore.rs @@ -55,6 +55,9 @@ pub trait MentionableBase<'a, Ctx: Context<'a>>: 'a + Serializable + Sized { /// Value of the associated factory. fn factory(&self) -> Self::Fctr; +} + +pub trait MentionableTop<'a, Ctx: Context<'a>>: 'a { /// See implementation for the definition. /// Hash of all the references' points concatenated, ordered, non-unique. /// Used for walking over object trees to ensure two objects with different references don't collide. @@ -67,11 +70,14 @@ pub trait MentionableBase<'a, Ctx: Context<'a>>: 'a + Serializable + Sized { fn points_typed(&self, points: &mut impl PointsVisitor<'a, Ctx>); } -pub trait Mentionable<'a, Ctx: Context<'a>>: MentionableBase<'a, Ctx, Fctr = Self::_Fctr> { +pub trait Mentionable<'a, Ctx: Context<'a>>: + MentionableBase<'a, Ctx, Fctr = Self::_Fctr> + MentionableTop<'a, Ctx> +{ type _Fctr: Factory<'a, Ctx, Mtbl = Self>; } -impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> Mentionable<'a, Ctx> for A +impl<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx> + MentionableTop<'a, Ctx>> + Mentionable<'a, Ctx> for A where Self::Fctr: Factory<'a, Ctx>, { @@ -90,7 +96,7 @@ pub type ParseResultA<'a, Ctx, A> = Result>; /// [Factory] base. pub trait FactoryBase<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone { /// Type of the associated objects. - type Mtbl: MentionableBase<'a, Ctx, Fctr = Self>; + type Mtbl: MentionableBase<'a, Ctx, Fctr = Self> + MentionableTop<'a, Ctx>; /// Type of an error that [`Factory::deserialize`] can fail with. type ParseError: 'a + Error; } diff --git a/src/rcore/origin.rs b/src/rcore/origin.rs index 7d343cc..f389902 100644 --- a/src/rcore/origin.rs +++ b/src/rcore/origin.rs @@ -3,7 +3,7 @@ use super::*; /// Represents a potentially resolvable [`Mentionable`]. pub trait Origin<'a, Ctx: Context<'a>>: 'a { /// Type of the associated object. - type Mtbl: MentionableBase<'a, Ctx>; + type Mtbl: MentionableBase<'a, Ctx> + MentionableTop<'a, Ctx>; /// Clone the associated factory. fn factory(&self) -> OFctr<'a, Ctx, Self>; /// Try resolving the value. diff --git a/src/rcore/point.rs b/src/rcore/point.rs index 648e94f..f6d37c2 100644 --- a/src/rcore/point.rs +++ b/src/rcore/point.rs @@ -4,7 +4,7 @@ use super::*; pub struct Point<'a, Ctx: Context<'a>, A: MentionableBase<'a, Ctx>> { /// Hash of the referred content. /// Derived both from the serialised value ([`Serializable::serialize`]) - /// and its topology ([`MentionableBase::topology`]). + /// and its topology ([`MentionableTop::topology`]). pub point: Hash, /// [Origin] used in [`Point::resolve`]. pub origin: Rc>, diff --git a/src/rcore/points.rs b/src/rcore/points.rs index 30b596d..8d8c962 100644 --- a/src/rcore/points.rs +++ b/src/rcore/points.rs @@ -1,13 +1,13 @@ use super::*; -/// Visitor used in [`MentionableBase::points_typed`]. +/// Visitor used in [`MentionableTop::points_typed`]. pub trait PointsVisitor<'a, Ctx: Context<'a>> { /// Visit a [Point]. fn visit>(&mut self, point: &Point<'a, Ctx, A>); } impl<'a, Ctx: Context<'a>> PointsVisitor<'a, Ctx> for Vec { - /// The only natural implementation, as used in [`MentionableBase::topology`]. + /// The only natural implementation, as used in [`MentionableTop::topology`]. fn visit>(&mut self, point: &Point<'a, Ctx, A>) { self.extend(point.point) } diff --git a/src/rcore/resolution.rs b/src/rcore/resolution.rs index 0fd0a15..cf520ae 100644 --- a/src/rcore/resolution.rs +++ b/src/rcore/resolution.rs @@ -44,14 +44,14 @@ pub type HashResolution<'a, Ctx> = Wrapped<'a, Ctx, HashResolutionResult<'a, Ctx #[derive(Clone, Copy, Debug)] pub struct Address { pub point: Hash, - /// Index of the point in the [`MentionableBase::points_typed()`]. + /// Index of the point in the [`MentionableTop::points_typed()`]. pub index: usize, } /// Trait representing the "rainbow table" behaviour. pub trait Resolver<'a, Ctx: Context<'a>>: 'a { /// Successfully returned value should be the inverse of the point passed - /// with topology header ([`MentionableBase::topology()`]) omitted. + /// with topology header ([`MentionableTop::topology()`]) omitted. fn resolve(self: Rc, address: Address) -> HashResolution<'a, Ctx>; } diff --git a/src/rstd/atomic.rs b/src/rstd/atomic.rs index c6ad432..e2bd80a 100644 --- a/src/rstd/atomic.rs +++ b/src/rstd/atomic.rs @@ -21,7 +21,7 @@ pub type AParseError = ::AParseError; pub type AParseResult = Result>; /// This trait combines functionality of [`Mentionable`] and [`Factory`], -/// while limiting [`MentionableBase::points_typed`] (and corresponding [`MentionableBase::topology`]) +/// while limiting [`MentionableTop::points_typed`] (and corresponding [`MentionableTop::topology`]) /// to an empty sequence. pub trait AtomicBase: 'static + Send + Sync + Send + Clone + Serializable { /// Equivalent of [`FactoryBase::ParseError`]. diff --git a/src/rstd/atomic/atomic_object.rs b/src/rstd/atomic/atomic_object.rs index 12c6b06..cec52d0 100644 --- a/src/rstd/atomic/atomic_object.rs +++ b/src/rstd/atomic/atomic_object.rs @@ -37,7 +37,7 @@ impl Serializable for AtomicObject { } pub trait AoProxy<'a, Ctx: Context<'a>>: AtomicProxy { - type Mtbl: MentionableBase<'a, Ctx, Fctr = Self::Fctr>; + type Mtbl: MentionableBase<'a, Ctx, Fctr = Self::Fctr> + MentionableTop<'a, Ctx>; type Fctr: Factory<'a, Ctx, Mtbl = Self::Mtbl>; fn factory() -> Self::Fctr; @@ -54,7 +54,9 @@ where fn factory(&self) -> Self::Fctr { >::factory() } +} +impl<'a, Ctx: Context<'a>, A: AtomicBase> MentionableTop<'a, Ctx> for AtomicObject { fn topology(&self) -> Hash { Ctx::hash(b"") } diff --git a/src/rstd/cast.rs b/src/rstd/cast.rs index 26ac748..42fd8fc 100644 --- a/src/rstd/cast.rs +++ b/src/rstd/cast.rs @@ -24,7 +24,7 @@ pub enum CastError<'a> { /// If you don't know what that means, it's a good idea to [`panic!`]. /// Happens due to internal resolver using indices rather than `point`s. /// This error usually indicates inconsistent behaviour - /// of [`MentionableBase::points_typed`] and/or [`MentionableBase::topology`]. + /// of [`MentionableTop::points_typed`] and/or [`MentionableTop::topology`]. AddressIndexOutOfBounds { index: usize, length: usize, diff --git a/src/rstd/collections/stack.rs b/src/rstd/collections/stack.rs index 0284e37..36391f0 100644 --- a/src/rstd/collections/stack.rs +++ b/src/rstd/collections/stack.rs @@ -86,7 +86,11 @@ impl<'a, Ctx: Context<'a>, A: StackCompatible<'a, Ctx>> MentionableBase<'a, Ctx> fn factory(&self) -> Self::Fctr { StackNodeFactory::new(self.element.factory()) } +} +impl<'a, Ctx: Context<'a>, A: StackCompatible<'a, Ctx>> MentionableTop<'a, Ctx> + for StackNode<'a, Ctx, A> +{ fn points_typed(&self, points: &mut impl PointsVisitor<'a, Ctx>) { >::points_typed_rest(&self.rest, points); self.element.points_typed(points); diff --git a/src/rstd/collections/tree.rs b/src/rstd/collections/tree.rs index 338372b..f747fea 100644 --- a/src/rstd/collections/tree.rs +++ b/src/rstd/collections/tree.rs @@ -106,7 +106,9 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableBase<'a, Ctx> for fn factory(&self) -> Self::Fctr { NodeFactory(self.key.factory()) } +} +impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableTop<'a, Ctx> for Node<'a, Ctx, A> { fn points_typed(&self, points: &mut impl PointsVisitor<'a, Ctx>) { self.l.points_typed(points); self.r.points_typed(points); @@ -120,7 +122,9 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableBase<'a, Ctx> for fn factory(&self) -> Self::Fctr { TreeFactory(self.node.factory()) } +} +impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableTop<'a, Ctx> for Tree<'a, Ctx, A> { fn points_typed(&self, points: &mut impl PointsVisitor<'a, Ctx>) { self.node.points_typed(points); } diff --git a/src/rstd/inlining/static_pair.rs b/src/rstd/inlining/static_pair.rs index ded1bf3..fe075f4 100644 --- a/src/rstd/inlining/static_pair.rs +++ b/src/rstd/inlining/static_pair.rs @@ -34,9 +34,9 @@ pub trait StaticPair<'a, Ctx: Context<'a>>: /// [Factory] data. May, depending on the usecase, include factory (factories) on the element(s). type FactoryData: 'a + Send + Sync + Clone; /// First element's type. Must equal [`StaticPairSerializable::SA`]. - type A: MentionableBase<'a, Ctx, Fctr = Self::FA>; + type A: MentionableBase<'a, Ctx, Fctr = Self::FA> + MentionableTop<'a, Ctx>; /// Second element's type. Must equal [`StaticPairSerializable::SB`]. - type B: MentionableBase<'a, Ctx, Fctr = Self::FB>; + type B: MentionableBase<'a, Ctx, Fctr = Self::FB> + MentionableTop<'a, Ctx>; /// First element's factory. type FA: FactoryBase<'a, Ctx, Mtbl = Self::A> + InlineableFactory<'a, Ctx>; /// Second element's factory. @@ -110,7 +110,11 @@ impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> MentionableBase<'a, Ctx> factory_data: self.pair.factory_data(), } } +} +impl<'a, Ctx: Context<'a>, SP: StaticPair<'a, Ctx>> MentionableTop<'a, Ctx> + for StaticPairObject +{ fn points_typed(&self, points: &mut impl PointsVisitor<'a, Ctx>) { let (a, b) = self.pair.elements(); a.points_typed(points); diff --git a/src/rstd/nullable.rs b/src/rstd/nullable.rs index 7e08dca..411361c 100644 --- a/src/rstd/nullable.rs +++ b/src/rstd/nullable.rs @@ -43,7 +43,11 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableBase<'a, Ctx> }, } } +} +impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableTop<'a, Ctx> + for Nullable<'a, Ctx, A> +{ fn points_typed(&self, points: &mut impl PointsVisitor<'a, Ctx>) { match self { Self::Null(_) => {} diff --git a/src/rstd/point.rs b/src/rstd/point.rs index 2f3f7ee..8ecc120 100644 --- a/src/rstd/point.rs +++ b/src/rstd/point.rs @@ -19,7 +19,9 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableBase<'a, Ctx> for factory: self.origin.factory(), } } +} +impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> MentionableTop<'a, Ctx> for Point<'a, Ctx, A> { fn topology(&self) -> Hash { Ctx::hash(&self.point) } diff --git a/src/rstd/typeless.rs b/src/rstd/typeless.rs index aadd518..111b023 100644 --- a/src/rstd/typeless.rs +++ b/src/rstd/typeless.rs @@ -54,7 +54,9 @@ impl<'a, Ctx: Context<'a>> MentionableBase<'a, Ctx> for TypelessMentionable<'a, fn factory(&self) -> Self::Fctr { self.t_factory.clone() } +} +impl<'a, Ctx: Context<'a>> MentionableTop<'a, Ctx> for TypelessMentionable<'a, Ctx> { fn topology(&self) -> Hash { self.t_topology } @@ -207,7 +209,7 @@ where pub trait MentionableExt<'a, Ctx: Context<'a>>: Mentionable<'a, Ctx> { /// References ([Point]s) to other objects. Typeless. fn points_typeless(&self, points: &mut Vec>>); - /// [Vec] of [Point]s as used by [`MentionableBase::topology`]. + /// [Vec] of [Point]s as used by [`MentionableTop::topology`]. fn points_vec(&self) -> Vec>>; }