diff --git a/src/core.rs b/src/core.rs index d5d215b..10dd9ac 100644 --- a/src/core.rs +++ b/src/core.rs @@ -89,13 +89,21 @@ pub trait Mentionable<'a, Ctx: 'a + Context>: 'a + Serializable { /// Used for walking over object trees to ensure two objects with different references don't collide. fn topology(&self) -> Hash { let mut vec = Vec::new(); - for point in self.points() { + let mut points = Vec::new(); + self.points(&mut points); + for point in points { vec.extend(point.point); } Ctx::hash(&vec) } /// References ([Point]s) to other objects. - fn points(&self) -> Vec>>; + fn points(&self, points: &mut Vec>>); + + fn points_vec(&self) -> Vec>> { + let mut points = Vec::new(); + self.points(&mut points); + points + } } /// Short-hand for the type of values returned by [`Resolver::resolve`]. @@ -273,8 +281,8 @@ impl<'a, Ctx: 'a + Context> Mentionable<'a, Ctx> for TypelessMentionable<'a, Ctx self.t_topology } - fn points(&self) -> Vec>> { - self.t_points.clone() + fn points(&self, points: &mut Vec>>) { + points.extend(self.t_points.iter().map(Clone::clone)); } } @@ -324,7 +332,7 @@ impl<'a, Ctx: 'a + Context> TypelessMentionable<'a, Ctx> { pub fn from_typed>(mentionable: Rc) -> Self { let factory = TypelessFactory::from_typed(mentionable.factory()); let topology = mentionable.topology(); - let points = mentionable.points(); + let points = mentionable.points_vec(); TypelessMentionable { t_serialize: Box::new(move |serializer| mentionable.serialize(serializer)), t_factory: factory, diff --git a/src/std.rs b/src/std.rs index 811190a..34a9186 100644 --- a/src/std.rs +++ b/src/std.rs @@ -127,8 +127,8 @@ impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for Po Ctx::hash(&self.point) } - fn points(&self) -> Vec>> { - vec![self.typeless()] + fn points(&self, points: &mut Vec>>) { + points.push(self.typeless()); } } diff --git a/src/std/atomic/atomic_object.rs b/src/std/atomic/atomic_object.rs index 8d564a6..a19ac5b 100644 --- a/src/std/atomic/atomic_object.rs +++ b/src/std/atomic/atomic_object.rs @@ -43,9 +43,7 @@ impl<'a, Ctx: 'a + Context, A: Atomic> Mentionable<'a, Ctx> for AtomicObject Ctx::hash(b"") } - fn points(&self) -> Vec>> { - vec![] - } + fn points(&self, _points: &mut Vec>>) {} } /// Generic implementation of a factory for [Atomic]s. @@ -97,4 +95,3 @@ impl ExtAtomicObject for A { self.into() } } - diff --git a/src/std/cast.rs b/src/std/cast.rs index a255cde..a9045c0 100644 --- a/src/std/cast.rs +++ b/src/std/cast.rs @@ -84,7 +84,7 @@ where |resolved| match resolved { Ok(mentionable) => { let resolver: Rc> = Rc::new(CastResolver { - points: mentionable.points(), + points: mentionable.points_vec(), }); Ok((mentionable.bytes(), resolver)) } @@ -111,7 +111,7 @@ where factory.parse_slice( &self.bytes(), Rc::new(CastResolver { - points: self.points(), + points: self.points_vec(), }), ) } diff --git a/src/std/collections/stack.rs b/src/std/collections/stack.rs index e3bf910..5708189 100644 --- a/src/std/collections/stack.rs +++ b/src/std/collections/stack.rs @@ -44,10 +44,9 @@ impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> StackNodeFactory::new(self.element.factory()) } - fn points(&self) -> Vec>> { - let mut points = self.rest.points(); - points.extend(self.element.points()); - points + fn points(&self, points: &mut Vec>>) { + self.rest.points(points); + self.element.points(points); } } diff --git a/src/std/inlining.rs b/src/std/inlining.rs index 7d4e0a3..548f9d5 100644 --- a/src/std/inlining.rs +++ b/src/std/inlining.rs @@ -1,7 +1,7 @@ pub mod static_pair; -use super::atomic::*; use super::atomic::atomic_object::*; +use super::atomic::*; use crate::core::*; use crate::std::*; diff --git a/src/std/inlining/static_pair.rs b/src/std/inlining/static_pair.rs index cc93cc5..49479a0 100644 --- a/src/std/inlining/static_pair.rs +++ b/src/std/inlining/static_pair.rs @@ -79,11 +79,10 @@ impl<'a, Ctx: 'a + Context, SP: StaticPair<'a, Ctx>> Mentionable<'a, Ctx> for St } } - fn points(&self) -> Vec>> { + fn points(&self, points: &mut Vec>>) { let (a, b) = self.pair.elements(); - let mut vec = a.points(); - vec.extend(b.points()); - vec + a.points(points); + b.points(points); } } diff --git a/src/std/nullable.rs b/src/std/nullable.rs index 0f578f0..c8fe0cc 100644 --- a/src/std/nullable.rs +++ b/src/std/nullable.rs @@ -40,11 +40,11 @@ impl<'a, Ctx: 'a + Context, A: Mentionable<'a, Ctx>> Mentionable<'a, Ctx> for Nu } } - fn points(&self) -> Vec>> { + fn points(&self, points: &mut Vec>>) { match self { - Nullable::Null(_) => vec![], - Nullable::NotNull(point) => vec![point.typeless()], - } + Nullable::Null(_) => {} + Nullable::NotNull(point) => points.push(point.typeless()), + }; } }