From 9259b022f8ec05072993fd5e3e9f2eba3afc0c95 Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Sun, 7 Sep 2014 20:00:54 +0300 Subject: syntax: fold: use move semantics for efficient folding. --- src/libsyntax/util/small_vector.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/libsyntax/util/small_vector.rs') diff --git a/src/libsyntax/util/small_vector.rs b/src/libsyntax/util/small_vector.rs index 517c5e5bf47..47aef987a63 100644 --- a/src/libsyntax/util/small_vector.rs +++ b/src/libsyntax/util/small_vector.rs @@ -12,6 +12,8 @@ use std::mem; use std::slice; use std::vec; +use fold::MoveMap; + /// A vector type optimized for cases where the size is almost always 0 or 1 pub struct SmallVector { repr: SmallVectorRepr, @@ -20,7 +22,7 @@ pub struct SmallVector { enum SmallVectorRepr { Zero, One(T), - Many(Vec ), + Many(Vec), } impl Collection for SmallVector { @@ -160,6 +162,17 @@ impl Iterator for MoveItems { } } +impl MoveMap for SmallVector { + fn move_map(self, f: |T| -> T) -> SmallVector { + let repr = match self.repr { + Zero => Zero, + One(v) => One(f(v)), + Many(vs) => Many(vs.move_map(f)) + }; + SmallVector { repr: repr } + } +} + #[cfg(test)] mod test { use super::*; -- cgit 1.4.1-3-g733a5