Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
collections: Make push_back/pop_back default methods
  • Loading branch information
brson committed Jul 23, 2014
1 parent 2d79bfa commit 94e42c2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 38 deletions.
20 changes: 4 additions & 16 deletions src/libcollections/dlist.rs
Expand Up @@ -249,29 +249,17 @@ impl<T> Deque<T> for DList<T> {
fn pop_front(&mut self) -> Option<T> {
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) {
impl<T> MutableSeq<T> for DList<T> {
fn push(&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<T> {
fn pop(&mut self) -> Option<T> {
self.pop_back_node().map(|box Node{value, ..}| value)
}
}

impl<T> MutableSeq<T> for DList<T> {
fn push(&mut self, elt: T) { self.push_back(elt) }
fn pop(&mut self) -> Option<T> { self.pop_back() }
}

impl<T> Default for DList<T> {
#[inline]
fn default() -> DList<T> { DList::new() }
Expand Down
4 changes: 2 additions & 2 deletions src/libcollections/lib.rs
Expand Up @@ -516,7 +516,7 @@ pub trait Deque<T> : MutableSeq<T> {
/// 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.
///
Expand All @@ -534,7 +534,7 @@ pub trait Deque<T> : MutableSeq<T> {
/// assert_eq!(d.pop_back(), None);
/// ```
#[deprecated = "use the `pop` method"]
fn pop_back(&mut self) -> Option<T>;
fn pop_back(&mut self) -> Option<T> { self.pop() }

/// Remove the first element and return it, or `None` if the sequence is empty.
///
Expand Down
32 changes: 12 additions & 20 deletions src/libcollections/ringbuf.rs
Expand Up @@ -80,18 +80,6 @@ impl<T> Deque<T> for RingBuf<T> {
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<T> {
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() {
Expand All @@ -103,22 +91,26 @@ impl<T> Deque<T> for RingBuf<T> {
*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<T> MutableSeq<T> for RingBuf<T> {
fn push(&mut self, t: T) {
if self.nelts == self.elts.len() {
grow(self.nelts, &mut self.lo, &mut self.elts);
}
let hi = self.raw_index(self.nelts);
*self.elts.get_mut(hi) = Some(t);
self.nelts += 1u;
}
}

impl<T> MutableSeq<T> for RingBuf<T> {
fn push(&mut self, t: T) { self.push_back(t) }
fn pop(&mut self) -> Option<T> { self.pop_back() }
fn pop(&mut self) -> Option<T> {
if self.nelts > 0 {
self.nelts -= 1;
let hi = self.raw_index(self.nelts);
self.elts.get_mut(hi).take()
} else {
None
}
}
}

impl<T> Default for RingBuf<T> {
Expand Down

0 comments on commit 94e42c2

Please sign in to comment.