Skip to content

Commit 0e784e1

Browse files
committed
auto merge of #17268 : aturon/rust/mut-conventions, r=alexcrichton
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]
2 parents ceb9bbf + fc525ee commit 0e784e1

File tree

212 files changed

+1085
-849
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

212 files changed

+1085
-849
lines changed

src/compiletest/compiletest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ pub fn run_tests(config: &Config) {
259259
// parallel (especially when we have lots and lots of child processes).
260260
// For context, see #8904
261261
io::test::raise_fd_limit();
262-
let res = test::run_tests_console(&opts, tests.move_iter().collect());
262+
let res = test::run_tests_console(&opts, tests.into_iter().collect());
263263
match res {
264264
Ok(true) => {}
265265
Ok(false) => fail!("Some tests failed"),
@@ -400,4 +400,4 @@ fn extract_gdb_version(full_version_line: Option<String>) -> Option<String> {
400400
},
401401
_ => None
402402
}
403-
}
403+
}

src/compiletest/procsrv.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn run(lib_path: &str,
4040
let mut cmd = Command::new(prog);
4141
cmd.args(args);
4242
add_target_env(&mut cmd, lib_path, aux_path);
43-
for (key, val) in env.move_iter() {
43+
for (key, val) in env.into_iter() {
4444
cmd.env(key, val);
4545
}
4646

@@ -72,7 +72,7 @@ pub fn run_background(lib_path: &str,
7272
let mut cmd = Command::new(prog);
7373
cmd.args(args);
7474
add_target_env(&mut cmd, lib_path, aux_path);
75-
for (key, val) in env.move_iter() {
75+
for (key, val) in env.into_iter() {
7676
cmd.env(key, val);
7777
}
7878

src/compiletest/runtest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ fn cleanup_debug_info_options(options: &Option<String>) -> Option<String> {
768768
"--debuginfo".to_string()
769769
];
770770
let new_options =
771-
split_maybe_args(options).move_iter()
771+
split_maybe_args(options).into_iter()
772772
.filter(|x| !options_to_remove.contains(x))
773773
.collect::<Vec<String>>()
774774
.connect(" ");
@@ -1461,7 +1461,7 @@ fn _arm_exec_compiled_test(config: &Config,
14611461

14621462
// run test via adb_run_wrapper
14631463
runargs.push("shell".to_string());
1464-
for (key, val) in env.move_iter() {
1464+
for (key, val) in env.into_iter() {
14651465
runargs.push(format!("{}={}", key, val));
14661466
}
14671467
runargs.push(format!("{}/adb_run_wrapper.sh", config.adb_test_dir));

src/doc/guide-tasks.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ fn main() {
238238
let mut futures = Vec::from_fn(1000, |ind| Future::spawn( proc() { partial_sum(ind) }));
239239
240240
let mut final_res = 0f64;
241-
for ft in futures.mut_iter() {
241+
for ft in futures.iter_mut() {
242242
final_res += ft.get();
243243
}
244244
println!("π^2/6 is not far from : {}", final_res);

src/doc/guide.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ upper bound is exclusive, though, so our loop will print `0` through `9`, not
13191319

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

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

src/liballoc/heap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static MIN_ALIGN: uint = 16;
140140
#[cfg(jemalloc)]
141141
mod imp {
142142
use core::option::{None, Option};
143-
use core::ptr::{RawPtr, mut_null, null};
143+
use core::ptr::{RawPtr, null_mut, null};
144144
use core::num::Int;
145145
use libc::{c_char, c_int, c_void, size_t};
146146
use super::MIN_ALIGN;
@@ -230,7 +230,7 @@ mod imp {
230230

231231
pub fn stats_print() {
232232
unsafe {
233-
je_malloc_stats_print(None, mut_null(), null())
233+
je_malloc_stats_print(None, null_mut(), null())
234234
}
235235
}
236236
}

src/liballoc/libc_heap.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
//! The global (exchange) heap.
1313
1414
use libc::{c_void, size_t, free, malloc, realloc};
15-
use core::ptr::{RawPtr, mut_null};
15+
use core::ptr::{RawPtr, null_mut};
1616

1717
/// A wrapper around libc::malloc, aborting on out-of-memory.
1818
#[inline]
1919
pub unsafe fn malloc_raw(size: uint) -> *mut u8 {
2020
// `malloc(0)` may allocate, but it may also return a null pointer
2121
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html
2222
if size == 0 {
23-
mut_null()
23+
null_mut()
2424
} else {
2525
let p = malloc(size as size_t);
2626
if p.is_null() {
@@ -37,7 +37,7 @@ pub unsafe fn realloc_raw(ptr: *mut u8, size: uint) -> *mut u8 {
3737
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html
3838
if size == 0 {
3939
free(ptr as *mut c_void);
40-
mut_null()
40+
null_mut()
4141
} else {
4242
let p = realloc(ptr as *mut c_void, size as size_t);
4343
if p.is_null() {

src/libarena/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ impl<T> TypedArena<T> {
448448
#[inline]
449449
pub fn with_capacity(capacity: uint) -> TypedArena<T> {
450450
unsafe {
451-
let chunk = TypedArenaChunk::<T>::new(ptr::mut_null(), capacity);
451+
let chunk = TypedArenaChunk::<T>::new(ptr::null_mut(), capacity);
452452
TypedArena {
453453
ptr: Cell::new((*chunk).start() as *const T),
454454
end: Cell::new((*chunk).end() as *const T),

src/libcollections/bitv.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl Bitv {
178178
// `op` is a bitwise operation, since any bits that should've
179179
// been masked were fine to change anyway. `b` is masked to
180180
// make sure its unmasked bits do not cause damage.
181-
for (a, (_, b)) in self.storage.mut_iter()
181+
for (a, (_, b)) in self.storage.iter_mut()
182182
.zip(other.mask_words(0)) {
183183
let w = op(*a, b);
184184
if *a != w {
@@ -310,7 +310,7 @@ impl Bitv {
310310
/// ```
311311
#[inline]
312312
pub fn set_all(&mut self) {
313-
for w in self.storage.mut_iter() { *w = !0u; }
313+
for w in self.storage.iter_mut() { *w = !0u; }
314314
}
315315

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

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

@@ -831,7 +831,7 @@ impl Clone for Bitv {
831831
fn clone_from(&mut self, source: &Bitv) {
832832
self.nbits = source.nbits;
833833
self.storage.reserve(source.storage.len());
834-
for (i, w) in self.storage.mut_iter().enumerate() { *w = source.storage[i]; }
834+
for (i, w) in self.storage.iter_mut().enumerate() { *w = source.storage[i]; }
835835
}
836836
}
837837

src/libcollections/dlist.rs

+34-23
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub struct MoveItems<T> {
7979
impl<T> Rawlink<T> {
8080
/// Like Option::None for Rawlink
8181
fn none() -> Rawlink<T> {
82-
Rawlink{p: ptr::mut_null()}
82+
Rawlink{p: ptr::null_mut()}
8383
}
8484

8585
/// Like Option::Some for Rawlink
@@ -431,7 +431,7 @@ impl<T> DList<T> {
431431
/// ```
432432
pub fn insert_when(&mut self, elt: T, f: |&T, &T| -> bool) {
433433
{
434-
let mut it = self.mut_iter();
434+
let mut it = self.iter_mut();
435435
loop {
436436
match it.peek_next() {
437437
None => break,
@@ -451,7 +451,7 @@ impl<T> DList<T> {
451451
/// This operation should compute in O(max(N, M)) time.
452452
pub fn merge(&mut self, mut other: DList<T>, f: |&T, &T| -> bool) {
453453
{
454-
let mut it = self.mut_iter();
454+
let mut it = self.iter_mut();
455455
loop {
456456
let take_a = match (it.peek_next(), other.front()) {
457457
(_ , None) => return,
@@ -475,9 +475,15 @@ impl<T> DList<T> {
475475
Items{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
476476
}
477477

478+
/// Deprecated: use `iter_mut`.
479+
#[deprecated = "use iter_mut"]
480+
pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
481+
self.iter_mut()
482+
}
483+
478484
/// Provides a forward iterator with mutable references.
479485
#[inline]
480-
pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
486+
pub fn iter_mut<'a>(&'a mut self) -> MutItems<'a, T> {
481487
let head_raw = match self.list_head {
482488
Some(ref mut h) => Rawlink::some(&mut **h),
483489
None => Rawlink::none(),
@@ -490,10 +496,15 @@ impl<T> DList<T> {
490496
}
491497
}
492498

499+
/// Deprecated: use `into_iter`.
500+
#[deprecated = "use into_iter"]
501+
pub fn move_iter(self) -> MoveItems<T> {
502+
self.into_iter()
503+
}
493504

494505
/// Consumes the list into an iterator yielding elements by value.
495506
#[inline]
496-
pub fn move_iter(self) -> MoveItems<T> {
507+
pub fn into_iter(self) -> MoveItems<T> {
497508
MoveItems{list: self}
498509
}
499510
}
@@ -860,7 +871,7 @@ mod tests {
860871
check_links(&m);
861872
let sum = v.append(u.as_slice());
862873
assert_eq!(sum.len(), m.len());
863-
for elt in sum.move_iter() {
874+
for elt in sum.into_iter() {
864875
assert_eq!(m.pop_front(), Some(elt))
865876
}
866877
}
@@ -884,7 +895,7 @@ mod tests {
884895
check_links(&m);
885896
let sum = u.append(v.as_slice());
886897
assert_eq!(sum.len(), m.len());
887-
for elt in sum.move_iter() {
898+
for elt in sum.into_iter() {
888899
assert_eq!(m.pop_front(), Some(elt))
889900
}
890901
}
@@ -909,7 +920,7 @@ mod tests {
909920
m.rotate_backward(); check_links(&m);
910921
m.push_front(9); check_links(&m);
911922
m.rotate_forward(); check_links(&m);
912-
assert_eq!(vec![3i,9,5,1,2], m.move_iter().collect());
923+
assert_eq!(vec![3i,9,5,1,2], m.into_iter().collect());
913924
}
914925

915926
#[test]
@@ -980,16 +991,16 @@ mod tests {
980991
fn test_mut_iter() {
981992
let mut m = generate_test();
982993
let mut len = m.len();
983-
for (i, elt) in m.mut_iter().enumerate() {
994+
for (i, elt) in m.iter_mut().enumerate() {
984995
assert_eq!(i as int, *elt);
985996
len -= 1;
986997
}
987998
assert_eq!(len, 0);
988999
let mut n = DList::new();
989-
assert!(n.mut_iter().next().is_none());
1000+
assert!(n.iter_mut().next().is_none());
9901001
n.push_front(4i);
9911002
n.push(5);
992-
let mut it = n.mut_iter();
1003+
let mut it = n.iter_mut();
9931004
assert_eq!(it.size_hint(), (2, Some(2)));
9941005
assert!(it.next().is_some());
9951006
assert!(it.next().is_some());
@@ -1000,11 +1011,11 @@ mod tests {
10001011
#[test]
10011012
fn test_iterator_mut_double_end() {
10021013
let mut n = DList::new();
1003-
assert!(n.mut_iter().next_back().is_none());
1014+
assert!(n.iter_mut().next_back().is_none());
10041015
n.push_front(4i);
10051016
n.push_front(5);
10061017
n.push_front(6);
1007-
let mut it = n.mut_iter();
1018+
let mut it = n.iter_mut();
10081019
assert_eq!(it.size_hint(), (3, Some(3)));
10091020
assert_eq!(*it.next().unwrap(), 6);
10101021
assert_eq!(it.size_hint(), (2, Some(2)));
@@ -1020,7 +1031,7 @@ mod tests {
10201031
let mut m = list_from(&[0i,2,4,6,8]);
10211032
let len = m.len();
10221033
{
1023-
let mut it = m.mut_iter();
1034+
let mut it = m.iter_mut();
10241035
it.insert_next(-2);
10251036
loop {
10261037
match it.next() {
@@ -1039,7 +1050,7 @@ mod tests {
10391050
}
10401051
check_links(&m);
10411052
assert_eq!(m.len(), 3 + len * 2);
1042-
assert_eq!(m.move_iter().collect::<Vec<int>>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]);
1053+
assert_eq!(m.into_iter().collect::<Vec<int>>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]);
10431054
}
10441055

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

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

10721083
#[test]
10731084
fn test_mut_rev_iter() {
10741085
let mut m = generate_test();
1075-
for (i, elt) in m.mut_iter().rev().enumerate() {
1086+
for (i, elt) in m.iter_mut().rev().enumerate() {
10761087
assert_eq!((6-i) as int, *elt);
10771088
}
10781089
let mut n = DList::new();
1079-
assert!(n.mut_iter().rev().next().is_none());
1090+
assert!(n.iter_mut().rev().next().is_none());
10801091
n.push_front(4i);
1081-
let mut it = n.mut_iter().rev();
1092+
let mut it = n.iter_mut().rev();
10821093
assert!(it.next().is_some());
10831094
assert!(it.next().is_none());
10841095
}
@@ -1218,7 +1229,7 @@ mod tests {
12181229
check_links(&m);
12191230

12201231
let mut i = 0u;
1221-
for (a, &b) in m.move_iter().zip(v.iter()) {
1232+
for (a, &b) in m.into_iter().zip(v.iter()) {
12221233
i += 1;
12231234
assert_eq!(a, b);
12241235
}
@@ -1300,7 +1311,7 @@ mod tests {
13001311
let v = &[0i, ..128];
13011312
let mut m: DList<int> = v.iter().map(|&x|x).collect();
13021313
b.iter(|| {
1303-
assert!(m.mut_iter().count() == 128);
1314+
assert!(m.iter_mut().count() == 128);
13041315
})
13051316
}
13061317
#[bench]
@@ -1316,7 +1327,7 @@ mod tests {
13161327
let v = &[0i, ..128];
13171328
let mut m: DList<int> = v.iter().map(|&x|x).collect();
13181329
b.iter(|| {
1319-
assert!(m.mut_iter().rev().count() == 128);
1330+
assert!(m.iter_mut().rev().count() == 128);
13201331
})
13211332
}
13221333
}

0 commit comments

Comments
 (0)