From 94e42c2d896cd26c1a48fa4a1748f2c62089fc2a Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 11 Jul 2014 18:08:46 -0700 Subject: [PATCH] collections: Make push_back/pop_back default methods --- src/libcollections/dlist.rs | 24 ++++++------------------ src/libcollections/lib.rs | 4 ++-- src/libcollections/ringbuf.rs | 32 ++++++++++++-------------------- 3 files changed, 20 insertions(+), 40 deletions(-) diff --git a/src/libcollections/dlist.rs b/src/libcollections/dlist.rs index f4d0763421c7..19488ee9f854 100644 --- a/src/libcollections/dlist.rs +++ b/src/libcollections/dlist.rs @@ -249,27 +249,15 @@ impl Deque for DList { fn pop_front(&mut self) -> Option { self.pop_front_node().map(|box Node{value, ..}| value) } - - /// Add an element last in the list - /// - /// O(1) - #[deprecated = "use the `push` method"] - fn push_back(&mut self, elt: T) { - self.push_back_node(box Node::new(elt)) - } - - /// Remove the last element and return it, or None if the list is empty - /// - /// O(1) - #[deprecated = "use the `pop` method"] - fn pop_back(&mut self) -> Option { - self.pop_back_node().map(|box Node{value, ..}| value) - } } impl MutableSeq for DList { - fn push(&mut self, elt: T) { self.push_back(elt) } - fn pop(&mut self) -> Option { self.pop_back() } + fn push(&mut self, elt: T) { + self.push_back_node(box Node::new(elt)) + } + fn pop(&mut self) -> Option { + self.pop_back_node().map(|box Node{value, ..}| value) + } } impl Default for DList { diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index 1d1907f45419..8d008f921da1 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -516,7 +516,7 @@ pub trait Deque : MutableSeq { /// assert_eq!(d.front(), Some(&1i)); /// ``` #[deprecated = "use the `push` method"] - fn push_back(&mut self, elt: T); + fn push_back(&mut self, elt: T) { self.push(elt) } /// Remove the last element and return it, or `None` if the sequence is empty. /// @@ -534,7 +534,7 @@ pub trait Deque : MutableSeq { /// assert_eq!(d.pop_back(), None); /// ``` #[deprecated = "use the `pop` method"] - fn pop_back(&mut self) -> Option; + fn pop_back(&mut self) -> Option { self.pop() } /// Remove the first element and return it, or `None` if the sequence is empty. /// diff --git a/src/libcollections/ringbuf.rs b/src/libcollections/ringbuf.rs index 9d24b349c975..c56781ec30b7 100644 --- a/src/libcollections/ringbuf.rs +++ b/src/libcollections/ringbuf.rs @@ -80,18 +80,6 @@ impl Deque for RingBuf { result } - /// Remove and return the last element in the RingBuf, or None if it is empty - #[deprecated = "use the `pop` method"] - fn pop_back(&mut self) -> Option { - if self.nelts > 0 { - self.nelts -= 1; - let hi = self.raw_index(self.nelts); - self.elts.get_mut(hi).take() - } else { - None - } - } - /// Prepend an element to the RingBuf fn push_front(&mut self, t: T) { if self.nelts == self.elts.len() { @@ -103,10 +91,10 @@ impl Deque for RingBuf { *self.elts.get_mut(self.lo) = Some(t); self.nelts += 1u; } +} - /// Append an element to the RingBuf - #[deprecated = "use the `push` method"] - fn push_back(&mut self, t: T) { +impl MutableSeq for RingBuf { + fn push(&mut self, t: T) { if self.nelts == self.elts.len() { grow(self.nelts, &mut self.lo, &mut self.elts); } @@ -114,11 +102,15 @@ impl Deque for RingBuf { *self.elts.get_mut(hi) = Some(t); self.nelts += 1u; } -} - -impl MutableSeq for RingBuf { - fn push(&mut self, t: T) { self.push_back(t) } - fn pop(&mut self) -> Option { self.pop_back() } + fn pop(&mut self) -> Option { + if self.nelts > 0 { + self.nelts -= 1; + let hi = self.raw_index(self.nelts); + self.elts.get_mut(hi).take() + } else { + None + } + } } impl Default for RingBuf {