diff options
| author | bors <bors@rust-lang.org> | 2014-09-16 23:26:11 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-09-16 23:26:11 +0000 |
| commit | 0e784e16840e8a0c623cc6166de26da9334db3d6 (patch) | |
| tree | cb9ee37525225e3cbe4cda7d7954f2f72d24acb8 /src/libcore | |
| parent | ceb9bbfbf5933f9df238fecdd14e75304439c4f4 (diff) | |
| parent | fc525eeb4ec3443d29bce677f589b19f31c189bb (diff) | |
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]
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/fmt/float.rs | 4 | ||||
| -rw-r--r-- | src/libcore/fmt/mod.rs | 2 | ||||
| -rw-r--r-- | src/libcore/fmt/num.rs | 4 | ||||
| -rw-r--r-- | src/libcore/iter.rs | 6 | ||||
| -rw-r--r-- | src/libcore/option.rs | 16 | ||||
| -rw-r--r-- | src/libcore/ptr.rs | 10 | ||||
| -rw-r--r-- | src/libcore/result.rs | 16 | ||||
| -rw-r--r-- | src/libcore/slice.rs | 127 |
8 files changed, 133 insertions, 52 deletions
diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs index 3a07e43e509..fcc794fd0d1 100644 --- a/src/libcore/fmt/float.rs +++ b/src/libcore/fmt/float.rs @@ -203,7 +203,7 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>( _ => () } - buf.mut_slice_to(end).reverse(); + buf.slice_to_mut(end).reverse(); // Remember start of the fractional digits. // Points one beyond end of buf if none get generated, @@ -342,7 +342,7 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>( impl<'a> fmt::FormatWriter for Filler<'a> { fn write(&mut self, bytes: &[u8]) -> fmt::Result { - slice::bytes::copy_memory(self.buf.mut_slice_from(*self.end), + slice::bytes::copy_memory(self.buf.slice_from_mut(*self.end), bytes); *self.end += bytes.len(); Ok(()) diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index f41d02070c9..7bab59960b0 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -420,7 +420,7 @@ impl<'a> Formatter<'a> { // Writes the sign if it exists, and then the prefix if it was requested let write_prefix = |f: &mut Formatter| { - for c in sign.move_iter() { + for c in sign.into_iter() { let mut b = [0, ..4]; let n = c.encode_utf8(b).unwrap_or(0); try!(f.buf.write(b.slice_to(n))); diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs index 21cbafdc605..568745d70b3 100644 --- a/src/libcore/fmt/num.rs +++ b/src/libcore/fmt/num.rs @@ -43,7 +43,7 @@ trait GenericRadix { if is_positive { // Accumulate each digit of the number from the least significant // to the most significant figure. - for byte in buf.mut_iter().rev() { + for byte in buf.iter_mut().rev() { let n = x % base; // Get the current place value. x = x / base; // Deaccumulate the number. *byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer. @@ -52,7 +52,7 @@ trait GenericRadix { } } else { // Do the same as above, but accounting for two's complement. - for byte in buf.mut_iter().rev() { + for byte in buf.iter_mut().rev() { let n = -(x % base); // Get the current place value. x = x / base; // Deaccumulate the number. *byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer. diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index cf977a6ebe6..6bee0573fac 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -377,7 +377,7 @@ pub trait Iterator<A> { /// sum /// } /// let x = vec![1i,2,3,7,8,9]; - /// assert_eq!(process(x.move_iter()), 1006); + /// assert_eq!(process(x.into_iter()), 1006); /// ``` #[inline] fn fuse(self) -> Fuse<Self> { @@ -1682,7 +1682,7 @@ impl<'a, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for FlatMap<'a, A, T, #[inline] fn next(&mut self) -> Option<B> { loop { - for inner in self.frontiter.mut_iter() { + for inner in self.frontiter.iter_mut() { for x in *inner { return Some(x) } @@ -1713,7 +1713,7 @@ impl<'a, #[inline] fn next_back(&mut self) -> Option<B> { loop { - for inner in self.backiter.mut_iter() { + for inner in self.backiter.iter_mut() { match inner.next_back() { None => (), y => return y diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 537d78a67fe..cd6e8f3e666 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -372,17 +372,29 @@ impl<T> Option<T> { Item{opt: self.as_ref()} } + /// Deprecated: use `iter_mut` + #[deprecated = "use iter_mut"] + pub fn mut_iter<'r>(&'r mut self) -> Item<&'r mut T> { + self.iter_mut() + } + /// Returns a mutable iterator over the possibly contained value. #[inline] #[unstable = "waiting for iterator conventions"] - pub fn mut_iter<'r>(&'r mut self) -> Item<&'r mut T> { + pub fn iter_mut<'r>(&'r mut self) -> Item<&'r mut T> { Item{opt: self.as_mut()} } + /// Deprecated: use `into_iter`. + #[deprecated = "use into_iter"] + pub fn move_iter(self) -> Item<T> { + self.into_iter() + } + /// Returns a consuming iterator over the possibly contained value. #[inline] #[unstable = "waiting for iterator conventions"] - pub fn move_iter(self) -> Item<T> { + pub fn into_iter(self) -> Item<T> { Item{opt: self} } diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index d1bea25dded..b76c92140fd 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -113,6 +113,10 @@ pub use intrinsics::set_memory; #[unstable = "may need a different name after pending changes to pointer types"] pub fn null<T>() -> *const T { 0 as *const T } +/// Deprecated: use `null_mut`. +#[deprecated = "use null_mut"] +pub fn mut_null<T>() -> *mut T { null_mut() } + /// Create an unsafe mutable null pointer. /// /// # Example @@ -120,12 +124,12 @@ pub fn null<T>() -> *const T { 0 as *const T } /// ``` /// use std::ptr; /// -/// let p: *mut int = ptr::mut_null(); +/// let p: *mut int = ptr::null_mut(); /// assert!(p.is_null()); /// ``` #[inline] #[unstable = "may need a different name after pending changes to pointer types"] -pub fn mut_null<T>() -> *mut T { 0 as *mut T } +pub fn null_mut<T>() -> *mut T { 0 as *mut T } /// Zeroes out `count * size_of::<T>` bytes of memory at `dst` #[inline] @@ -323,7 +327,7 @@ impl<T> RawPtr<T> for *const T { impl<T> RawPtr<T> for *mut T { #[inline] - fn null() -> *mut T { mut_null() } + fn null() -> *mut T { null_mut() } #[inline] fn is_null(&self) -> bool { *self == RawPtr::null() } diff --git a/src/libcore/result.rs b/src/libcore/result.rs index bf351ecc89b..426ae8f0929 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -500,17 +500,29 @@ impl<T, E> Result<T, E> { Item{opt: self.as_ref().ok()} } + /// Deprecated: use `iter_mut`. + #[deprecated = "use iter_mut"] + pub fn mut_iter<'r>(&'r mut self) -> Item<&'r mut T> { + self.iter_mut() + } + /// Returns a mutable iterator over the possibly contained value. #[inline] #[unstable = "waiting for iterator conventions"] - pub fn mut_iter<'r>(&'r mut self) -> Item<&'r mut T> { + pub fn iter_mut<'r>(&'r mut self) -> Item<&'r mut T> { Item{opt: self.as_mut().ok()} } + /// Deprecated: `use into_iter`. + #[deprecated = "use into_iter"] + pub fn move_iter(self) -> Item<T> { + self.into_iter() + } + /// Returns a consuming iterator over the possibly contained value. #[inline] #[unstable = "waiting for iterator conventions"] - pub fn move_iter(self) -> Item<T> { + pub fn into_iter(self) -> Item<T> { Item{opt: self.ok()} } diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index cc2b01e3bb5..65ad7bb1753 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -486,38 +486,80 @@ pub trait MutableSlice<'a, T> { /// Primarily intended for getting a &mut [T] from a [T, ..N]. fn as_mut_slice(self) -> &'a mut [T]; + /// Deprecated: use `slice_mut`. + #[deprecated = "use slice_mut"] + fn mut_slice(self, start: uint, end: uint) -> &'a mut [T] { + self.slice_mut(start, end) + } + /// Returns a mutable subslice spanning the interval [`start`, `end`). /// /// Fails when the end of the new slice lies beyond the end of the /// original slice (i.e. when `end > self.len()`) or when `start > end`. /// /// Slicing with `start` equal to `end` yields an empty slice. - fn mut_slice(self, start: uint, end: uint) -> &'a mut [T]; + fn slice_mut(self, start: uint, end: uint) -> &'a mut [T]; + + /// Deprecated: use `slice_from_mut`. + #[deprecated = "use slice_from_mut"] + fn mut_slice_from(self, start: uint) -> &'a mut [T] { + self.slice_from_mut(start) + } /// Returns a mutable subslice from `start` to the end of the slice. /// /// Fails when `start` is strictly greater than the length of the original slice. /// /// Slicing from `self.len()` yields an empty slice. - fn mut_slice_from(self, start: uint) -> &'a mut [T]; + fn slice_from_mut(self, start: uint) -> &'a mut [T]; + + /// Deprecated: use `slice_to_mut`. + #[deprecated = "use slice_to_mut"] + fn mut_slice_to(self, end: uint) -> &'a mut [T] { + self.slice_to_mut(end) + } /// Returns a mutable subslice from the start of the slice to `end`. /// /// Fails when `end` is strictly greater than the length of the original slice. /// /// Slicing to `0` yields an empty slice. - fn mut_slice_to(self, end: uint) -> &'a mut [T]; + fn slice_to_mut(self, end: uint) -> &'a mut [T]; + + /// Deprecated: use `iter_mut`. + #[deprecated = "use iter_mut"] + fn mut_iter(self) -> MutItems<'a, T> { + self.iter_mut() + } /// Returns an iterator that allows modifying each value - fn mut_iter(self) -> MutItems<'a, T>; + fn iter_mut(self) -> MutItems<'a, T>; + + /// Deprecated: use `last_mut`. + #[deprecated = "use last_mut"] + fn mut_last(self) -> Option<&'a mut T> { + self.last_mut() + } /// Returns a mutable pointer to the last item in the vector. - fn mut_last(self) -> Option<&'a mut T>; + fn last_mut(self) -> Option<&'a mut T>; + + /// Deprecated: use `split_mut`. + #[deprecated = "use split_mut"] + fn mut_split(self, pred: |&T|: 'a -> bool) -> MutSplits<'a, T> { + self.split_mut(pred) + } /// Returns an iterator over the mutable subslices of the vector /// which are separated by elements that match `pred`. The /// matched element is not contained in the subslices. - fn mut_split(self, pred: |&T|: 'a -> bool) -> MutSplits<'a, T>; + fn split_mut(self, pred: |&T|: 'a -> bool) -> MutSplits<'a, T>; + + /// Deprecated: use `chunks_mut`. + #[deprecated = "use chunks_mut"] + fn mut_chunks(self, chunk_size: uint) -> MutChunks<'a, T> { + self.chunks_mut(chunk_size) + } /** * Returns an iterator over `chunk_size` elements of the vector at a time. @@ -529,7 +571,7 @@ pub trait MutableSlice<'a, T> { * * Fails if `chunk_size` is 0. */ - fn mut_chunks(self, chunk_size: uint) -> MutChunks<'a, T>; + fn chunks_mut(self, chunk_size: uint) -> MutChunks<'a, T>; /** * Returns a mutable reference to the first element in this slice @@ -541,7 +583,7 @@ pub trait MutableSlice<'a, T> { * ```ignore * if self.len() == 0 { return None; } * let head = &mut self[0]; - * *self = self.mut_slice_from(1); + * *self = self.slice_from_mut(1); * Some(head) * ``` * @@ -560,7 +602,7 @@ pub trait MutableSlice<'a, T> { * ```ignore * if self.len() == 0 { return None; } * let tail = &mut self[self.len() - 1]; - * *self = self.mut_slice_to(self.len() - 1); + * *self = self.slice_to_mut(self.len() - 1); * Some(tail) * ``` * @@ -587,6 +629,11 @@ pub trait MutableSlice<'a, T> { /// ``` fn swap(self, a: uint, b: uint); + /// Deprecated: use `split_at_mut`. + #[deprecated = "use split_at_mut"] + fn mut_split_at(self, mid: uint) -> (&'a mut [T], &'a mut [T]) { + self.split_at_mut(mid) + } /// Divides one `&mut` into two at an index. /// @@ -603,24 +650,24 @@ pub trait MutableSlice<'a, T> { /// /// // scoped to restrict the lifetime of the borrows /// { - /// let (left, right) = v.mut_split_at(0); + /// let (left, right) = v.split_at_mut(0); /// assert!(left == &mut []); /// assert!(right == &mut [1i, 2, 3, 4, 5, 6]); /// } /// /// { - /// let (left, right) = v.mut_split_at(2); + /// let (left, right) = v.split_at_mut(2); /// assert!(left == &mut [1i, 2]); /// assert!(right == &mut [3i, 4, 5, 6]); /// } /// /// { - /// let (left, right) = v.mut_split_at(6); + /// let (left, right) = v.split_at_mut(6); /// assert!(left == &mut [1i, 2, 3, 4, 5, 6]); /// assert!(right == &mut []); /// } /// ``` - fn mut_split_at(self, mid: uint) -> (&'a mut [T], &'a mut [T]); + fn split_at_mut(self, mid: uint) -> (&'a mut [T], &'a mut [T]); /// Reverse the order of elements in a vector, in place. /// @@ -633,8 +680,14 @@ pub trait MutableSlice<'a, T> { /// ``` fn reverse(self); + /// Deprecated: use `unsafe_mut`. + #[deprecated = "use unsafe_mut"] + unsafe fn unsafe_mut_ref(self, index: uint) -> &'a mut T { + self.unsafe_mut(index) + } + /// Returns an unsafe mutable pointer to the element in index - unsafe fn unsafe_mut_ref(self, index: uint) -> &'a mut T; + unsafe fn unsafe_mut(self, index: uint) -> &'a mut T; /// Return an unsafe mutable pointer to the vector's buffer. /// @@ -701,7 +754,7 @@ impl<'a,T> MutableSlice<'a, T> for &'a mut [T] { #[inline] fn as_mut_slice(self) -> &'a mut [T] { self } - fn mut_slice(self, start: uint, end: uint) -> &'a mut [T] { + fn slice_mut(self, start: uint, end: uint) -> &'a mut [T] { assert!(start <= end); assert!(end <= self.len()); unsafe { @@ -713,27 +766,27 @@ impl<'a,T> MutableSlice<'a, T> for &'a mut [T] { } #[inline] - fn mut_slice_from(self, start: uint) -> &'a mut [T] { + fn slice_from_mut(self, start: uint) -> &'a mut [T] { let len = self.len(); - self.mut_slice(start, len) + self.slice_mut(start, len) } #[inline] - fn mut_slice_to(self, end: uint) -> &'a mut [T] { - self.mut_slice(0, end) + fn slice_to_mut(self, end: uint) -> &'a mut [T] { + self.slice_mut(0, end) } #[inline] - fn mut_split_at(self, mid: uint) -> (&'a mut [T], &'a mut [T]) { + fn split_at_mut(self, mid: uint) -> (&'a mut [T], &'a mut [T]) { unsafe { let len = self.len(); let self2: &'a mut [T] = mem::transmute_copy(&self); - (self.mut_slice(0, mid), self2.mut_slice(mid, len)) + (self.slice_mut(0, mid), self2.slice_mut(mid, len)) } } #[inline] - fn mut_iter(self) -> MutItems<'a, T> { + fn iter_mut(self) -> MutItems<'a, T> { unsafe { let p = self.as_mut_ptr(); if mem::size_of::<T>() == 0 { @@ -751,19 +804,19 @@ impl<'a,T> MutableSlice<'a, T> for &'a mut [T] { } #[inline] - fn mut_last(self) -> Option<&'a mut T> { + fn last_mut(self) -> Option<&'a mut T> { let len = self.len(); if len == 0 { return None; } Some(&mut self[len - 1]) } #[inline] - fn mut_split(self, pred: |&T|: 'a -> bool) -> MutSplits<'a, T> { + fn split_mut(self, pred: |&T|: 'a -> bool) -> MutSplits<'a, T> { MutSplits { v: self, pred: pred, finished: false } } #[inline] - fn mut_chunks(self, chunk_size: uint) -> MutChunks<'a, T> { + fn chunks_mut(self, chunk_size: uint) -> MutChunks<'a, T> { assert!(chunk_size > 0); MutChunks { v: self, chunk_size: chunk_size } } @@ -808,8 +861,8 @@ impl<'a,T> MutableSlice<'a, T> for &'a mut [T] { while i < ln / 2 { // Unsafe swap to avoid the bounds check in safe swap. unsafe { - let pa: *mut T = self.unsafe_mut_ref(i); - let pb: *mut T = self.unsafe_mut_ref(ln - i - 1); + let pa: *mut T = self.unsafe_mut(i); + let pb: *mut T = self.unsafe_mut(ln - i - 1); ptr::swap(pa, pb); } i += 1; @@ -817,7 +870,7 @@ impl<'a,T> MutableSlice<'a, T> for &'a mut [T] { } #[inline] - unsafe fn unsafe_mut_ref(self, index: uint) -> &'a mut T { + unsafe fn unsafe_mut(self, index: uint) -> &'a mut T { transmute((self.repr().data as *mut T).offset(index as int)) } @@ -828,7 +881,7 @@ impl<'a,T> MutableSlice<'a, T> for &'a mut [T] { #[inline] unsafe fn unsafe_set(self, index: uint, val: T) { - *self.unsafe_mut_ref(index) = val; + *self.unsafe_mut(index) = val; } #[inline] @@ -965,7 +1018,7 @@ pub trait MutableCloneableSlice<T> { impl<'a, T:Clone> MutableCloneableSlice<T> for &'a mut [T] { #[inline] fn clone_from_slice(self, src: &[T]) -> uint { - for (a, b) in self.mut_iter().zip(src.iter()) { + for (a, b) in self.iter_mut().zip(src.iter()) { a.clone_from(b); } cmp::min(self.len(), src.len()) @@ -1221,14 +1274,14 @@ impl<'a, T> Iterator<&'a mut [T]> for MutSplits<'a, T> { self.finished = true; let tmp = mem::replace(&mut self.v, &mut []); let len = tmp.len(); - let (head, tail) = tmp.mut_split_at(len); + let (head, tail) = tmp.split_at_mut(len); self.v = tail; Some(head) } Some(idx) => { let tmp = mem::replace(&mut self.v, &mut []); - let (head, tail) = tmp.mut_split_at(idx); - self.v = tail.mut_slice_from(1); + let (head, tail) = tmp.split_at_mut(idx); + self.v = tail.slice_from_mut(1); Some(head) } } @@ -1261,9 +1314,9 @@ impl<'a, T> DoubleEndedIterator<&'a mut [T]> for MutSplits<'a, T> { } Some(idx) => { let tmp = mem::replace(&mut self.v, &mut []); - let (head, tail) = tmp.mut_split_at(idx); + let (head, tail) = tmp.split_at_mut(idx); self.v = head; - Some(tail.mut_slice_from(1)) + Some(tail.slice_from_mut(1)) } } } @@ -1430,7 +1483,7 @@ impl<'a, T> Iterator<&'a mut [T]> for MutChunks<'a, T> { } else { let sz = cmp::min(self.v.len(), self.chunk_size); let tmp = mem::replace(&mut self.v, &mut []); - let (head, tail) = tmp.mut_split_at(sz); + let (head, tail) = tmp.split_at_mut(sz); self.v = tail; Some(head) } @@ -1459,7 +1512,7 @@ impl<'a, T> DoubleEndedIterator<&'a mut [T]> for MutChunks<'a, T> { let sz = if remainder != 0 { remainder } else { self.chunk_size }; let tmp = mem::replace(&mut self.v, &mut []); let tmp_len = tmp.len(); - let (head, tail) = tmp.mut_split_at(tmp_len - sz); + let (head, tail) = tmp.split_at_mut(tmp_len - sz); self.v = head; Some(tail) } |
