about summary refs log tree commit diff
path: root/src/libsyntax/util/small_vector.rs
diff options
context:
space:
mode:
authorSteven Fackler <sfackler@gmail.com>2013-11-24 23:08:53 -0800
committerSteven Fackler <sfackler@palantir.com>2013-11-26 13:56:02 -0800
commitc144752a2de4ffe3a2a22da9a8309ca2ecd85c58 (patch)
tree66ab1d0cce647d2f0863a94bdb127a511ad8fe62 /src/libsyntax/util/small_vector.rs
parent09f84aa8f4298489828720c048ec7f769338c0e2 (diff)
downloadrust-c144752a2de4ffe3a2a22da9a8309ca2ecd85c58.tar.gz
rust-c144752a2de4ffe3a2a22da9a8309ca2ecd85c58.zip
Support multiple item macros
Closes #4375
Diffstat (limited to 'src/libsyntax/util/small_vector.rs')
-rw-r--r--src/libsyntax/util/small_vector.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/libsyntax/util/small_vector.rs b/src/libsyntax/util/small_vector.rs
index c6b223f7c15..0196a0212e2 100644
--- a/src/libsyntax/util/small_vector.rs
+++ b/src/libsyntax/util/small_vector.rs
@@ -73,6 +73,22 @@ impl<T> SmallVector<T> {
         }
     }
 
+    pub fn pop(&mut self) -> T {
+        match *self {
+            Zero => fail!("attempted to pop from an empty SmallVector"),
+            One(*) => {
+                let mut tmp = Zero;
+                util::swap(self, &mut tmp);
+                match tmp {
+                    One(v) => v,
+                    _ => unreachable!()
+                }
+            }
+            // Should this reduce to a One if possible?
+            Many(ref mut vs) => vs.pop()
+        }
+    }
+
     pub fn get<'a>(&'a self, idx: uint) -> &'a T {
         match *self {
             One(ref v) if idx == 0 => v,
@@ -177,6 +193,18 @@ mod test {
     }
 
     #[test]
+    fn test_pop() {
+        let mut v = SmallVector::one(1);
+        assert_eq!(1, v.pop());
+        assert_eq!(0, v.len());
+
+        let mut v= SmallVector::many(~[1, 2]);
+        assert_eq!(2, v.pop());
+        assert_eq!(1, v.pop());
+        assert_eq!(0, v.len());
+    }
+
+    #[test]
     fn test_from_iterator() {
         let v: SmallVector<int> = (~[1, 2, 3]).move_iter().collect();
         assert_eq!(3, v.len());