Skip to content

Commit

Permalink
Align with _mut conventions
Browse files Browse the repository at this point in the history
As per [RFC
52](https://github.com/rust-lang/rfcs/blob/master/active/0052-ownership-variants.md),
use `_mut` suffixes to mark mutable variants, and `into_iter` for moving
iterators.

[breaking-change]
  • Loading branch information
aturon committed Sep 16, 2014
1 parent 828e075 commit d8dfe19
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 68 deletions.
15 changes: 13 additions & 2 deletions src/libcollections/dlist.rs
Expand Up @@ -475,9 +475,15 @@ impl<T> DList<T> {
Items{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
}

/// Deprecated: use `iter_mut`.
#[deprecated = "use iter_mut"]
pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
self.iter_mut()
}

/// Provides a forward iterator with mutable references.
#[inline]
pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
pub fn iter_mut<'a>(&'a mut self) -> MutItems<'a, T> {
let head_raw = match self.list_head {
Some(ref mut h) => Rawlink::some(&mut **h),
None => Rawlink::none(),
Expand All @@ -490,10 +496,15 @@ impl<T> DList<T> {
}
}

/// Deprecated: use `into_iter`.
#[deprecated = "use into_iter"]
pub fn move_iter(self) -> MoveItems<T> {
self.into_iter()
}

/// Consumes the list into an iterator yielding elements by value.
#[inline]
pub fn move_iter(self) -> MoveItems<T> {
pub fn into_iter(self) -> MoveItems<T> {
MoveItems{list: self}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/libcollections/ringbuf.rs
Expand Up @@ -250,6 +250,12 @@ impl<T> RingBuf<T> {
Items{index: 0, rindex: self.nelts, lo: self.lo, elts: self.elts.as_slice()}
}

/// Deprecated: use `iter_mut`
#[deprecated = "use iter_mut"]
pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
self.iter_mut()
}

/// Returns a front-to-back iterator which returns mutable references.
///
/// # Example
Expand All @@ -267,7 +273,7 @@ impl<T> RingBuf<T> {
/// let b: &[_] = &[&mut 3, &mut 1, &mut 2];
/// assert_eq!(buf.mut_iter().collect::<Vec<&mut int>>().as_slice(), b);
/// ```
pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
pub fn iter_mut<'a>(&'a mut self) -> MutItems<'a, T> {
let start_index = raw_index(self.lo, self.elts.len(), 0);
let end_index = raw_index(self.lo, self.elts.len(), self.nelts);

Expand Down
18 changes: 16 additions & 2 deletions src/libcollections/smallintmap.rs
Expand Up @@ -260,6 +260,12 @@ impl<V> SmallIntMap<V> {
}
}

/// Deprecated: use `iter_mut`
#[deprecated = "use iter_mut"]
pub fn mut_iter<'r>(&'r mut self) -> MutEntries<'r, V> {
self.iter_mut()
}

/// Returns an iterator visiting all key-value pairs in ascending order by the keys,
/// with mutable references to the values.
/// The iterator's element type is `(uint, &'r mut V)`.
Expand All @@ -282,14 +288,22 @@ impl<V> SmallIntMap<V> {
/// assert_eq!(value, &"x");
/// }
/// ```
pub fn mut_iter<'r>(&'r mut self) -> MutEntries<'r, V> {
pub fn iter_mut<'r>(&'r mut self) -> MutEntries<'r, V> {
MutEntries {
front: 0,
back: self.v.len(),
iter: self.v.mut_iter()
}
}

/// Deprecated: use `into_iter` instead.
#[deprecated = "use into_iter"]
pub fn move_iter(&mut self)
-> FilterMap<(uint, Option<V>), (uint, V),
Enumerate<vec::MoveItems<Option<V>>>> {
self.into_iter()
}

/// Returns an iterator visiting all key-value pairs in ascending order by
/// the keys, emptying (but not consuming) the original `SmallIntMap`.
/// The iterator's element type is `(uint, &'r V)`.
Expand All @@ -309,7 +323,7 @@ impl<V> SmallIntMap<V> {
///
/// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
/// ```
pub fn move_iter(&mut self)
pub fn into_iter(&mut self)
-> FilterMap<(uint, Option<V>), (uint, V),
Enumerate<vec::MoveItems<Option<V>>>>
{
Expand Down
79 changes: 60 additions & 19 deletions src/libcollections/treemap.rs
Expand Up @@ -226,10 +226,10 @@ impl<K: Ord, V> Map<K, V> for TreeMap<K, V> {
}

impl<K: Ord, V> MutableMap<K, V> for TreeMap<K, V> {
// See comments on def_tree_find_mut_with
// See comments on tree_find_with_mut
#[inline]
fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V> {
tree_find_mut_with(&mut self.root, |x| key.cmp(x))
tree_find_with_mut(&mut self.root, |x| key.cmp(x))
}

fn swap(&mut self, key: K, value: V) -> Option<V> {
Expand Down Expand Up @@ -361,6 +361,12 @@ impl<K: Ord, V> TreeMap<K, V> {
RevEntries{iter: self.iter()}
}

/// Deprecated: use `iter_mut`.
#[deprecated = "use iter_mut"]
pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, K, V> {
self.iter_mut()
}

/// Gets a lazy forward iterator over the key-value pairs in the
/// map, with the values being mutable.
///
Expand All @@ -383,15 +389,21 @@ impl<K: Ord, V> TreeMap<K, V> {
/// assert_eq!(map.find(&"b"), Some(&12));
/// assert_eq!(map.find(&"c"), Some(&3));
/// ```
pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, K, V> {
pub fn iter_mut<'a>(&'a mut self) -> MutEntries<'a, K, V> {
MutEntries {
stack: vec!(),
node: mut_deref(&mut self.root),
node: deref_mut(&mut self.root),
remaining_min: self.length,
remaining_max: self.length
}
}

/// Deprecated: use `rev_iter_mut`.
#[deprecated = "use rev_iter_mut"]
pub fn mut_rev_iter<'a>(&'a mut self) -> RevMutEntries<'a, K, V> {
self.rev_iter_mut()
}

/// Gets a lazy reverse iterator over the key-value pairs in the
/// map, with the values being mutable.
///
Expand All @@ -414,10 +426,15 @@ impl<K: Ord, V> TreeMap<K, V> {
/// assert_eq!(map.find(&"b"), Some(&12));
/// assert_eq!(map.find(&"c"), Some(&13));
/// ```
pub fn mut_rev_iter<'a>(&'a mut self) -> RevMutEntries<'a, K, V> {
pub fn rev_iter_mut<'a>(&'a mut self) -> RevMutEntries<'a, K, V> {
RevMutEntries{iter: self.mut_iter()}
}

/// Deprecated: use `into_iter`.
#[depreated = "use into_iter"]
pub fn move_iter(self) -> MoveEntries<K, V> {
self.into_iter()
}

/// Gets a lazy iterator that consumes the treemap.
///
Expand All @@ -434,7 +451,7 @@ impl<K: Ord, V> TreeMap<K, V> {
/// let vec: Vec<(&str, int)> = map.move_iter().collect();
/// assert_eq!(vec, vec![("a", 1), ("b", 2), ("c", 3)]);
/// ```
pub fn move_iter(self) -> MoveEntries<K, V> {
pub fn into_iter(self) -> MoveEntries<K, V> {
let TreeMap { root: root, length: length } = self;
let stk = match root {
None => vec!(),
Expand Down Expand Up @@ -477,6 +494,12 @@ impl<K, V> TreeMap<K, V> {
tree_find_with(&self.root, f)
}

/// Deprecated: use `find_with_mut`.
#[deprecated = "use find_with_mut"]
pub fn find_mut_with<'a>(&'a mut self, f:|&K| -> Ordering) -> Option<&'a mut V> {
self.find_with_mut(f)
}

/// Returns the value for which `f(key)` returns `Equal`. `f` is invoked
/// with current key and guides tree navigation. That means `f` should
/// be aware of natural ordering of the tree.
Expand All @@ -497,8 +520,8 @@ impl<K, V> TreeMap<K, V> {
/// assert_eq!(t.find(&"User-Agent"), Some(&new_ua));
/// ```
#[inline]
pub fn find_mut_with<'a>(&'a mut self, f:|&K| -> Ordering) -> Option<&'a mut V> {
tree_find_mut_with(&mut self.root, f)
pub fn find_with_mut<'a>(&'a mut self, f:|&K| -> Ordering) -> Option<&'a mut V> {
tree_find_with_mut(&mut self.root, f)
}
}

Expand Down Expand Up @@ -594,15 +617,21 @@ impl<K: Ord, V> TreeMap<K, V> {

/// Gets a lazy iterator that should be initialized using
/// `traverse_left`/`traverse_right`/`traverse_complete`.
fn mut_iter_for_traversal<'a>(&'a mut self) -> MutEntries<'a, K, V> {
fn iter_mut_for_traversal<'a>(&'a mut self) -> MutEntries<'a, K, V> {
MutEntries {
stack: vec!(),
node: mut_deref(&mut self.root),
node: deref_mut(&mut self.root),
remaining_min: 0,
remaining_max: self.length
}
}

/// Deprecated: use `lower_bound_mut`.
#[deprecated = "use lower_bound_mut"]
pub fn mut_lower_bound<'a>(&'a mut self, k: &K) -> MutEntries<'a, K, V> {
self.lower_bound_mut(k)
}

/// Returns a lazy value iterator to the first key-value pair (with
/// the value being mutable) whose key is not less than `k`.
///
Expand Down Expand Up @@ -633,8 +662,14 @@ impl<K: Ord, V> TreeMap<K, V> {
/// assert_eq!(map.find(&6), Some(&"changed"));
/// assert_eq!(map.find(&8), Some(&"changed"));
/// ```
pub fn mut_lower_bound<'a>(&'a mut self, k: &K) -> MutEntries<'a, K, V> {
bound_setup!(self.mut_iter_for_traversal(), k, true)
pub fn lower_bound_mut<'a>(&'a mut self, k: &K) -> MutEntries<'a, K, V> {
bound_setup!(self.iter_mut_for_traversal(), k, true)
}

/// Deprecated: use `upper_bound_mut`.
#[deprecated = "use upper_bound_mut"]
pub fn mut_upper_bound<'a>(&'a mut self, k: &K) -> MutEntries<'a, K, V> {
self.upper_bound_mut(k)
}

/// Returns a lazy iterator to the first key-value pair (with the
Expand Down Expand Up @@ -667,8 +702,8 @@ impl<K: Ord, V> TreeMap<K, V> {
/// assert_eq!(map.find(&6), Some(&"changed"));
/// assert_eq!(map.find(&8), Some(&"changed"));
/// ```
pub fn mut_upper_bound<'a>(&'a mut self, k: &K) -> MutEntries<'a, K, V> {
bound_setup!(self.mut_iter_for_traversal(), k, false)
pub fn upper_bound_mut<'a>(&'a mut self, k: &K) -> MutEntries<'a, K, V> {
bound_setup!(self.iter_mut_for_traversal(), k, false)
}
}

Expand Down Expand Up @@ -862,7 +897,7 @@ define_iterator! {
define_iterator! {
MutEntries,
RevMutEntries,
deref = mut_deref,
deref = deref_mut,

addr_mut = mut
}
Expand All @@ -877,7 +912,7 @@ fn deref<'a, K, V>(node: &'a Option<Box<TreeNode<K, V>>>) -> *const TreeNode<K,
}
}

fn mut_deref<K, V>(x: &mut Option<Box<TreeNode<K, V>>>)
fn deref_mut<K, V>(x: &mut Option<Box<TreeNode<K, V>>>)
-> *mut TreeNode<K, V> {
match *x {
Some(ref mut n) => {
Expand Down Expand Up @@ -1169,6 +1204,12 @@ impl<T: Ord> TreeSet<T> {
RevSetItems{iter: self.map.rev_iter()}
}

/// Deprecated: use `into_iter`.
#[deprecated = "use into_iter"]
pub fn move_iter(self) -> MoveSetItems<T> {
self.into_iter()
}

/// Creates a consuming iterator, that is, one that moves each value out of the
/// set in ascending order. The set cannot be used after calling this.
///
Expand All @@ -1183,8 +1224,8 @@ impl<T: Ord> TreeSet<T> {
/// assert_eq!(v, vec![1, 2, 3, 4, 5]);
/// ```
#[inline]
pub fn move_iter(self) -> MoveSetItems<T> {
self.map.move_iter().map(|(value, _)| value)
pub fn into_iter(self) -> MoveSetItems<T> {
self.map.into_iter().map(|(value, _)| value)
}

/// Gets a lazy iterator pointing to the first value not less than `v` (greater or equal).
Expand Down Expand Up @@ -1488,7 +1529,7 @@ fn tree_find_with<'r, K, V>(node: &'r Option<Box<TreeNode<K, V>>>,
}

// See comments above tree_find_with
fn tree_find_mut_with<'r, K, V>(node: &'r mut Option<Box<TreeNode<K, V>>>,
fn tree_find_with_mut<'r, K, V>(node: &'r mut Option<Box<TreeNode<K, V>>>,
f: |&K| -> Ordering) -> Option<&'r mut V> {

let mut current = node;
Expand Down
30 changes: 24 additions & 6 deletions src/libcollections/trie.rs
Expand Up @@ -267,6 +267,12 @@ impl<T> TrieMap<T> {
iter
}

/// Deprecated: use `iter_mut`.
#[deprecated = "use iter_mut"]
pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, T> {
self.iter_mut()
}

/// Gets an iterator over the key-value pairs in the map, with the
/// ability to mutate the values.
///
Expand All @@ -284,7 +290,7 @@ impl<T> TrieMap<T> {
/// assert_eq!(map.find(&2), Some(&-2));
/// assert_eq!(map.find(&3), Some(&-3));
/// ```
pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, T> {
pub fn iter_mut<'a>(&'a mut self) -> MutEntries<'a, T> {
let mut iter = unsafe {MutEntries::new()};
iter.stack[0] = self.root.children.mut_iter();
iter.length = 1;
Expand Down Expand Up @@ -425,13 +431,19 @@ impl<T> TrieMap<T> {
}
// If `upper` is true then returns upper_bound else returns lower_bound.
#[inline]
fn mut_bound<'a>(&'a mut self, key: uint, upper: bool) -> MutEntries<'a, T> {
fn bound_mut<'a>(&'a mut self, key: uint, upper: bool) -> MutEntries<'a, T> {
bound!(MutEntries, self = self,
key = key, is_upper = upper,
slice_from = mut_slice_from, iter = mut_iter,
mutability = mut)
}

/// Deprecated: use `lower_bound_mut`.
#[deprecated = "use lower_bound_mut"]
pub fn mut_lower_bound<'a>(&'a mut self, key: uint) -> MutEntries<'a, T> {
self.lower_bound_mut(key)
}

/// Gets an iterator pointing to the first key-value pair whose key is not less than `key`.
/// If all keys in the map are less than `key` an empty iterator is returned.
///
Expand All @@ -453,8 +465,14 @@ impl<T> TrieMap<T> {
/// assert_eq!(map.find(&4), Some(&"changed"));
/// assert_eq!(map.find(&6), Some(&"changed"));
/// ```
pub fn mut_lower_bound<'a>(&'a mut self, key: uint) -> MutEntries<'a, T> {
self.mut_bound(key, false)
pub fn lower_bound_mut<'a>(&'a mut self, key: uint) -> MutEntries<'a, T> {
self.bound_mut(key, false)
}

/// Deprecated: use `upper_bound_mut`.
#[deprecated = "use upper_bound_mut"]
pub fn mut_upper_bound<'a>(&'a mut self, key: uint) -> MutEntries<'a, T> {
self.upper_bound_mut(key)
}

/// Gets an iterator pointing to the first key-value pair whose key is greater than `key`.
Expand All @@ -478,8 +496,8 @@ impl<T> TrieMap<T> {
/// assert_eq!(map.find(&4), Some(&"b"));
/// assert_eq!(map.find(&6), Some(&"changed"));
/// ```
pub fn mut_upper_bound<'a>(&'a mut self, key: uint) -> MutEntries<'a, T> {
self.mut_bound(key, true)
pub fn upper_bound_mut<'a>(&'a mut self, key: uint) -> MutEntries<'a, T> {
self.bound_mut(key, true)
}
}

Expand Down

0 comments on commit d8dfe19

Please sign in to comment.