reduce Rc usage in stack

This commit is contained in:
AF 2023-06-16 06:32:50 +00:00
parent cbd80a9307
commit 5f0927c94a

View File

@ -9,7 +9,7 @@ pub struct StackNode<'a, Ctx: Context<'a>, A: Mentionable<'a, Ctx>> {
/// Reference comes first due to being inlineable. /// Reference comes first due to being inlineable.
pub rest: Stack<'a, Ctx, A>, pub rest: Stack<'a, Ctx, A>,
/// Unlike the original implementation in Python, doesn't default to using Point. /// Unlike the original implementation in Python, doesn't default to using Point.
pub element: Rc<A>, pub element: A,
} }
/// Type representing a stack, an alias to a [Nullable] of a [StackNode]. /// 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)); return Err(StackParseError::Point(ppe));
} }
}; };
let element = Rc::new( let element = match self
match self .element_factory
.element_factory .deserialize(deserializer, resolver, addresses)
.deserialize(deserializer, resolver, addresses) {
{ Ok(element) => element,
Ok(element) => element, Err(epe) => {
Err(epe) => { return Err(StackParseError::Element(epe));
return Err(StackParseError::Element(epe)); }
} };
},
);
Ok(StackNode { rest, element }) 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>>; pub type StackFaiure<'a, Ctx, A> = ResolutionFailure<'a, Ctx, StackNode<'a, Ctx, A>>;
/// See [`StackVecWrapped`]. /// See [`StackVecWrapped`].
pub type StackVecResult<'a, Ctx, A> = Result<Vec<Rc<A>>, StackFaiure<'a, Ctx, A>>; pub type StackVecResult<'a, Ctx, A> = Result<Vec<A>, StackFaiure<'a, Ctx, A>>;
/// See [`ExtStack::vec`]. /// See [`ExtStackClone::vec`].
pub type StackVecWrapped<'a, Ctx, A> = Wrapped<'a, Ctx, StackVecResult<'a, Ctx, A>>; pub type StackVecWrapped<'a, Ctx, A> = Wrapped<'a, Ctx, StackVecResult<'a, Ctx, A>>;
/// Extention trait with helper methods for [Stack]s. /// 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. /// Note: consumes the stack. For non-consuming version do `.clone()` before adding.
fn add(self, element: A) -> Self; 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`]. /// Collect all the elements into a [`Vec`].
fn vec(self) -> StackVecWrapped<'a, Ctx, A>; 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( Nullable::NotNull(
StackNode { StackNode {
rest: self, rest: self,
element: Rc::new(element), element,
} }
.into(), .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> { fn vec(self) -> StackVecWrapped<'a, Ctx, A> {
Ctx::T::iterate_mut((vec![], self), |(mut vec, stack)| match stack { Ctx::T::iterate_mut((vec![], self), |(mut vec, stack)| match stack {
Nullable::Null(_) => Ctx::pure(ControlFlow::Break(Ok(vec))), Nullable::Null(_) => Ctx::pure(ControlFlow::Break(Ok(vec))),