From 5f0927c94aa837095247f0209424d5b356223ef6 Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 16 Jun 2023 06:32:50 +0000 Subject: [PATCH] reduce `Rc` usage in `stack` --- src/rstd/collections/stack.rs | 39 +++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/rstd/collections/stack.rs b/src/rstd/collections/stack.rs index e775606..9cd537f 100644 --- a/src/rstd/collections/stack.rs +++ b/src/rstd/collections/stack.rs @@ -9,7 +9,7 @@ pub struct StackNode<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> { /// Reference comes first due to being inlineable. pub rest: Stack<'a, Ctx, A>, /// Unlike the original implementation in Python, doesn't default to using Point. - pub element: Rc, + pub element: A, } /// Type representing a stack, an alias to a [Nullable] of a [StackNode]. @@ -90,17 +90,15 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for StackNodeFa return Err(StackParseError::Point(ppe)); } }; - let element = Rc::new( - match self - .element_factory - .deserialize(deserializer, resolver, addresses) - { - Ok(element) => element, - Err(epe) => { - return Err(StackParseError::Element(epe)); - } - }, - ); + let element = match self + .element_factory + .deserialize(deserializer, resolver, addresses) + { + Ok(element) => element, + Err(epe) => { + return Err(StackParseError::Element(epe)); + } + }; Ok(StackNode { rest, element }) } @@ -113,9 +111,9 @@ impl<'a, Ctx: Context<'a>, F: Factory<'a, Ctx>> Factory<'a, Ctx> for StackNodeFa pub type StackFaiure<'a, Ctx, A> = ResolutionFailure<'a, Ctx, StackNode<'a, Ctx, A>>; /// See [`StackVecWrapped`]. -pub type StackVecResult<'a, Ctx, A> = Result>, StackFaiure<'a, Ctx, A>>; +pub type StackVecResult<'a, Ctx, A> = Result, StackFaiure<'a, Ctx, A>>; -/// See [`ExtStack::vec`]. +/// See [`ExtStackClone::vec`]. pub type StackVecWrapped<'a, Ctx, A> = Wrapped<'a, Ctx, StackVecResult<'a, Ctx, A>>; /// Extention trait with helper methods for [Stack]s. @@ -128,6 +126,12 @@ pub trait ExtStack<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>>: Mentionable<' /// /// Note: consumes the stack. For non-consuming version do `.clone()` before adding. fn add(self, element: A) -> Self; +} + +/// Extention trait with helper methods for [Stack]s. +pub trait ExtStackClone<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone>: + Mentionable<'a, Ctx> +{ /// Collect all the elements into a [`Vec`]. fn vec(self) -> StackVecWrapped<'a, Ctx, A>; } @@ -143,11 +147,16 @@ impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> ExtStack<'a, Ctx, A> for Sta Nullable::NotNull( StackNode { rest: self, - element: Rc::new(element), + element, } .into(), ) } +} + +impl<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx> + Clone> ExtStackClone<'a, Ctx, A> + for Stack<'a, Ctx, A> +{ fn vec(self) -> StackVecWrapped<'a, Ctx, A> { Ctx::T::iterate_mut((vec![], self), |(mut vec, stack)| match stack { Nullable::Null(_) => Ctx::pure(ControlFlow::Break(Ok(vec))),