Skip to content

Commit

Permalink
auto merge of #17268 : aturon/rust/mut-conventions, r=alexcrichton
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. Additional details and motivation in the RFC.

Note that the iterator *type* names are not changed by this RFC; those are awaiting a separate RFC for standardization.

Closes #13660
Closes #16810

[breaking-change]
  • Loading branch information
bors committed Sep 16, 2014
2 parents ceb9bbf + fc525ee commit 0e784e1
Show file tree
Hide file tree
Showing 212 changed files with 1,085 additions and 849 deletions.
4 changes: 2 additions & 2 deletions src/compiletest/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ pub fn run_tests(config: &Config) {
// parallel (especially when we have lots and lots of child processes).
// For context, see #8904
io::test::raise_fd_limit();
let res = test::run_tests_console(&opts, tests.move_iter().collect());
let res = test::run_tests_console(&opts, tests.into_iter().collect());
match res {
Ok(true) => {}
Ok(false) => fail!("Some tests failed"),
Expand Down Expand Up @@ -400,4 +400,4 @@ fn extract_gdb_version(full_version_line: Option<String>) -> Option<String> {
},
_ => None
}
}
}
4 changes: 2 additions & 2 deletions src/compiletest/procsrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn run(lib_path: &str,
let mut cmd = Command::new(prog);
cmd.args(args);
add_target_env(&mut cmd, lib_path, aux_path);
for (key, val) in env.move_iter() {
for (key, val) in env.into_iter() {
cmd.env(key, val);
}

Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn run_background(lib_path: &str,
let mut cmd = Command::new(prog);
cmd.args(args);
add_target_env(&mut cmd, lib_path, aux_path);
for (key, val) in env.move_iter() {
for (key, val) in env.into_iter() {
cmd.env(key, val);
}

Expand Down
4 changes: 2 additions & 2 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ fn cleanup_debug_info_options(options: &Option<String>) -> Option<String> {
"--debuginfo".to_string()
];
let new_options =
split_maybe_args(options).move_iter()
split_maybe_args(options).into_iter()
.filter(|x| !options_to_remove.contains(x))
.collect::<Vec<String>>()
.connect(" ");
Expand Down Expand Up @@ -1461,7 +1461,7 @@ fn _arm_exec_compiled_test(config: &Config,

// run test via adb_run_wrapper
runargs.push("shell".to_string());
for (key, val) in env.move_iter() {
for (key, val) in env.into_iter() {
runargs.push(format!("{}={}", key, val));
}
runargs.push(format!("{}/adb_run_wrapper.sh", config.adb_test_dir));
Expand Down
2 changes: 1 addition & 1 deletion src/doc/guide-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ fn main() {
let mut futures = Vec::from_fn(1000, |ind| Future::spawn( proc() { partial_sum(ind) }));
let mut final_res = 0f64;
for ft in futures.mut_iter() {
for ft in futures.iter_mut() {
final_res += ft.get();
}
println!("π^2/6 is not far from : {}", final_res);
Expand Down
2 changes: 1 addition & 1 deletion src/doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ upper bound is exclusive, though, so our loop will print `0` through `9`, not

Rust does not have the "C style" `for` loop on purpose. Manually controlling
each element of the loop is complicated and error prone, even for experienced C
developers.
developers.

We'll talk more about `for` when we cover **iterator**s, later in the Guide.

Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ static MIN_ALIGN: uint = 16;
#[cfg(jemalloc)]
mod imp {
use core::option::{None, Option};
use core::ptr::{RawPtr, mut_null, null};
use core::ptr::{RawPtr, null_mut, null};
use core::num::Int;
use libc::{c_char, c_int, c_void, size_t};
use super::MIN_ALIGN;
Expand Down Expand Up @@ -230,7 +230,7 @@ mod imp {

pub fn stats_print() {
unsafe {
je_malloc_stats_print(None, mut_null(), null())
je_malloc_stats_print(None, null_mut(), null())
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/liballoc/libc_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
//! The global (exchange) heap.
use libc::{c_void, size_t, free, malloc, realloc};
use core::ptr::{RawPtr, mut_null};
use core::ptr::{RawPtr, null_mut};

/// A wrapper around libc::malloc, aborting on out-of-memory.
#[inline]
pub unsafe fn malloc_raw(size: uint) -> *mut u8 {
// `malloc(0)` may allocate, but it may also return a null pointer
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html
if size == 0 {
mut_null()
null_mut()
} else {
let p = malloc(size as size_t);
if p.is_null() {
Expand All @@ -37,7 +37,7 @@ pub unsafe fn realloc_raw(ptr: *mut u8, size: uint) -> *mut u8 {
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html
if size == 0 {
free(ptr as *mut c_void);
mut_null()
null_mut()
} else {
let p = realloc(ptr as *mut c_void, size as size_t);
if p.is_null() {
Expand Down
2 changes: 1 addition & 1 deletion src/libarena/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ impl<T> TypedArena<T> {
#[inline]
pub fn with_capacity(capacity: uint) -> TypedArena<T> {
unsafe {
let chunk = TypedArenaChunk::<T>::new(ptr::mut_null(), capacity);
let chunk = TypedArenaChunk::<T>::new(ptr::null_mut(), capacity);
TypedArena {
ptr: Cell::new((*chunk).start() as *const T),
end: Cell::new((*chunk).end() as *const T),
Expand Down
10 changes: 5 additions & 5 deletions src/libcollections/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl Bitv {
// `op` is a bitwise operation, since any bits that should've
// been masked were fine to change anyway. `b` is masked to
// make sure its unmasked bits do not cause damage.
for (a, (_, b)) in self.storage.mut_iter()
for (a, (_, b)) in self.storage.iter_mut()
.zip(other.mask_words(0)) {
let w = op(*a, b);
if *a != w {
Expand Down Expand Up @@ -310,7 +310,7 @@ impl Bitv {
/// ```
#[inline]
pub fn set_all(&mut self) {
for w in self.storage.mut_iter() { *w = !0u; }
for w in self.storage.iter_mut() { *w = !0u; }
}

/// Flips all bits.
Expand All @@ -329,7 +329,7 @@ impl Bitv {
/// ```
#[inline]
pub fn negate(&mut self) {
for w in self.storage.mut_iter() { *w = !*w; }
for w in self.storage.iter_mut() { *w = !*w; }
}

/// Calculates the union of two bitvectors. This acts like the bitwise `or`
Expand Down Expand Up @@ -797,7 +797,7 @@ impl Collection for Bitv {
impl Mutable for Bitv {
#[inline]
fn clear(&mut self) {
for w in self.storage.mut_iter() { *w = 0u; }
for w in self.storage.iter_mut() { *w = 0u; }
}
}

Expand Down Expand Up @@ -831,7 +831,7 @@ impl Clone for Bitv {
fn clone_from(&mut self, source: &Bitv) {
self.nbits = source.nbits;
self.storage.reserve(source.storage.len());
for (i, w) in self.storage.mut_iter().enumerate() { *w = source.storage[i]; }
for (i, w) in self.storage.iter_mut().enumerate() { *w = source.storage[i]; }
}
}

Expand Down
57 changes: 34 additions & 23 deletions src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub struct MoveItems<T> {
impl<T> Rawlink<T> {
/// Like Option::None for Rawlink
fn none() -> Rawlink<T> {
Rawlink{p: ptr::mut_null()}
Rawlink{p: ptr::null_mut()}
}

/// Like Option::Some for Rawlink
Expand Down Expand Up @@ -431,7 +431,7 @@ impl<T> DList<T> {
/// ```
pub fn insert_when(&mut self, elt: T, f: |&T, &T| -> bool) {
{
let mut it = self.mut_iter();
let mut it = self.iter_mut();
loop {
match it.peek_next() {
None => break,
Expand All @@ -451,7 +451,7 @@ impl<T> DList<T> {
/// This operation should compute in O(max(N, M)) time.
pub fn merge(&mut self, mut other: DList<T>, f: |&T, &T| -> bool) {
{
let mut it = self.mut_iter();
let mut it = self.iter_mut();
loop {
let take_a = match (it.peek_next(), other.front()) {
(_ , None) => return,
Expand All @@ -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 Expand Up @@ -860,7 +871,7 @@ mod tests {
check_links(&m);
let sum = v.append(u.as_slice());
assert_eq!(sum.len(), m.len());
for elt in sum.move_iter() {
for elt in sum.into_iter() {
assert_eq!(m.pop_front(), Some(elt))
}
}
Expand All @@ -884,7 +895,7 @@ mod tests {
check_links(&m);
let sum = u.append(v.as_slice());
assert_eq!(sum.len(), m.len());
for elt in sum.move_iter() {
for elt in sum.into_iter() {
assert_eq!(m.pop_front(), Some(elt))
}
}
Expand All @@ -909,7 +920,7 @@ mod tests {
m.rotate_backward(); check_links(&m);
m.push_front(9); check_links(&m);
m.rotate_forward(); check_links(&m);
assert_eq!(vec![3i,9,5,1,2], m.move_iter().collect());
assert_eq!(vec![3i,9,5,1,2], m.into_iter().collect());
}

#[test]
Expand Down Expand Up @@ -980,16 +991,16 @@ mod tests {
fn test_mut_iter() {
let mut m = generate_test();
let mut len = m.len();
for (i, elt) in m.mut_iter().enumerate() {
for (i, elt) in m.iter_mut().enumerate() {
assert_eq!(i as int, *elt);
len -= 1;
}
assert_eq!(len, 0);
let mut n = DList::new();
assert!(n.mut_iter().next().is_none());
assert!(n.iter_mut().next().is_none());
n.push_front(4i);
n.push(5);
let mut it = n.mut_iter();
let mut it = n.iter_mut();
assert_eq!(it.size_hint(), (2, Some(2)));
assert!(it.next().is_some());
assert!(it.next().is_some());
Expand All @@ -1000,11 +1011,11 @@ mod tests {
#[test]
fn test_iterator_mut_double_end() {
let mut n = DList::new();
assert!(n.mut_iter().next_back().is_none());
assert!(n.iter_mut().next_back().is_none());
n.push_front(4i);
n.push_front(5);
n.push_front(6);
let mut it = n.mut_iter();
let mut it = n.iter_mut();
assert_eq!(it.size_hint(), (3, Some(3)));
assert_eq!(*it.next().unwrap(), 6);
assert_eq!(it.size_hint(), (2, Some(2)));
Expand All @@ -1020,7 +1031,7 @@ mod tests {
let mut m = list_from(&[0i,2,4,6,8]);
let len = m.len();
{
let mut it = m.mut_iter();
let mut it = m.iter_mut();
it.insert_next(-2);
loop {
match it.next() {
Expand All @@ -1039,7 +1050,7 @@ mod tests {
}
check_links(&m);
assert_eq!(m.len(), 3 + len * 2);
assert_eq!(m.move_iter().collect::<Vec<int>>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]);
assert_eq!(m.into_iter().collect::<Vec<int>>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]);
}

#[test]
Expand All @@ -1050,7 +1061,7 @@ mod tests {
m.merge(n, |a, b| a <= b);
assert_eq!(m.len(), len);
check_links(&m);
let res = m.move_iter().collect::<Vec<int>>();
let res = m.into_iter().collect::<Vec<int>>();
assert_eq!(res, vec![-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]);
}

Expand All @@ -1066,19 +1077,19 @@ mod tests {
m.push(4);
m.insert_ordered(3);
check_links(&m);
assert_eq!(vec![2,3,4], m.move_iter().collect::<Vec<int>>());
assert_eq!(vec![2,3,4], m.into_iter().collect::<Vec<int>>());
}

#[test]
fn test_mut_rev_iter() {
let mut m = generate_test();
for (i, elt) in m.mut_iter().rev().enumerate() {
for (i, elt) in m.iter_mut().rev().enumerate() {
assert_eq!((6-i) as int, *elt);
}
let mut n = DList::new();
assert!(n.mut_iter().rev().next().is_none());
assert!(n.iter_mut().rev().next().is_none());
n.push_front(4i);
let mut it = n.mut_iter().rev();
let mut it = n.iter_mut().rev();
assert!(it.next().is_some());
assert!(it.next().is_none());
}
Expand Down Expand Up @@ -1218,7 +1229,7 @@ mod tests {
check_links(&m);

let mut i = 0u;
for (a, &b) in m.move_iter().zip(v.iter()) {
for (a, &b) in m.into_iter().zip(v.iter()) {
i += 1;
assert_eq!(a, b);
}
Expand Down Expand Up @@ -1300,7 +1311,7 @@ mod tests {
let v = &[0i, ..128];
let mut m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
assert!(m.mut_iter().count() == 128);
assert!(m.iter_mut().count() == 128);
})
}
#[bench]
Expand All @@ -1316,7 +1327,7 @@ mod tests {
let v = &[0i, ..128];
let mut m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
assert!(m.mut_iter().rev().count() == 128);
assert!(m.iter_mut().rev().count() == 128);
})
}
}
Loading

0 comments on commit 0e784e1

Please sign in to comment.