diff options
| author | bors <bors@rust-lang.org> | 2014-12-13 22:57:21 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-12-13 22:57:21 +0000 |
| commit | 444fa1b7cffcd99ca5b8abb51acf979f06a25899 (patch) | |
| tree | 51a8beea4fd983ce8cb46879a2fdbaf72916c13e /src/libcore/slice.rs | |
| parent | 567b90ff095076054c98fa2f08d6c552ae60968d (diff) | |
| parent | b8e0b81dd57621af28d7b2b5551a3217f7bd4cec (diff) | |
| download | rust-444fa1b7cffcd99ca5b8abb51acf979f06a25899.tar.gz rust-444fa1b7cffcd99ca5b8abb51acf979f06a25899.zip | |
auto merge of #19467 : japaric/rust/uc, r=alexcrichton
This PR moves almost all our current uses of closures, both in public API and internal uses, to the new "unboxed" closures system.
In most cases, downstream code that *only uses* closures will continue to work as it is. The reason is that the `|| {}` syntax can be inferred either as a boxed or an "unboxed" closure according to the context. For example the following code will continue to work:
``` rust
some_option.map(|x| x.transform_with(upvar))
```
And will get silently upgraded to an "unboxed" closure.
In some other cases, it may be necessary to "annotate" which `Fn*` trait the closure implements:
```
// Change this
|x| { /* body */}
// to either of these
|: x| { /* body */} // closure implements the FnOnce trait
|&mut : x| { /* body */} // FnMut
|&: x| { /* body */} // Fn
```
This mainly occurs when the closure is assigned to a variable first, and then passed to a function/method.
``` rust
let closure = |: x| x.transform_with(upvar);
some.option.map(closure)
```
(It's very likely that in the future, an improved inference engine will make this annotation unnecessary)
Other cases that require annotation are closures that implement some trait via a blanket `impl`, for example:
- `std::finally::Finally`
- `regex::Replacer`
- `std::str::CharEq`
``` rust
string.trim_left_chars(|c: char| c.is_whitespace())
//~^ ERROR: the trait `Fn<(char,), bool>` is not implemented for the type `|char| -> bool`
string.trim_left_chars(|&: c: char| c.is_whitespace()) // OK
```
Finally, all implementations of traits that contain boxed closures in the arguments of their methods are now broken. And will need to be updated to use unboxed closures. These are the main affected traits:
- `serialize::Decoder`
- `serialize::DecoderHelpers`
- `serialize::Encoder`
- `serialize::EncoderHelpers`
- `rustrt::ToCStr`
For example, change this:
``` rust
// libserialize/json.rs
impl<'a> Encoder<io::IoError> for Encoder<'a> {
fn emit_enum(&mut self,
_name: &str,
f: |&mut Encoder<'a>| -> EncodeResult) -> EncodeResult {
f(self)
}
}
```
to:
``` rust
// libserialize/json.rs
impl<'a> Encoder<io::IoError> for Encoder<'a> {
fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
f(self)
}
}
```
[breaking-change]
---
### How the `Fn*` bound has been selected
I've chosen the bounds to make the functions/structs as "generic as possible", i.e. to let them allow the maximum amount of input.
- An `F: FnOnce` bound accepts the three kinds of closures: `|:|`, `|&mut:|` and `|&:|`.
- An `F: FnMut` bound only accepts "non-consuming" closures: `|&mut:|` and `|&:|`.
- An `F: Fn` bound only accept the "immutable environment" closures: `|&:|`.
This means that whenever possible the `FnOnce` bound has been used, if the `FnOnce` bound couldn't be used, then the `FnMut` was used. The `Fn` bound was never used in the whole repository.
The `FnMut` bound was the most used, because it resembles the semantics of the current boxed closures: the closure can modify its environment, and the closure may be called several times.
The `FnOnce` bound allows new semantics: you can move out the upvars when the closure is called. This can be effectively paired with the `move || {}` syntax to transfer ownership from the environment to the closure caller.
In the case of trait methods, is hard to select the "right" bound since we can't control how the trait may be implemented by downstream users. In these cases, I have selected the bound based on how we use these traits in the repository. For this reason the selected bounds may not be ideal, and may require tweaking before stabilization.
r? @aturon
Diffstat (limited to 'src/libcore/slice.rs')
| -rw-r--r-- | src/libcore/slice.rs | 81 |
1 files changed, 48 insertions, 33 deletions
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 4e3007b55fe..27a4328ba80 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -43,7 +43,7 @@ use default::Default; use iter::*; use kinds::Copy; use num::Int; -use ops; +use ops::{FnMut, mod}; use option::Option; use option::Option::{None, Some}; use ptr; @@ -105,20 +105,23 @@ pub trait SlicePrelude<T> for Sized? { /// Returns an iterator over subslices separated by elements that match /// `pred`. The matched element is not contained in the subslices. #[unstable = "iterator type may change, waiting on unboxed closures"] - fn split<'a>(&'a self, pred: |&T|: 'a -> bool) -> Splits<'a, T>; + fn split<'a, P>(&'a self, pred: P) -> Splits<'a, T, P> where + P: FnMut(&T) -> bool; /// Returns an iterator over subslices separated by elements that match /// `pred`, limited to splitting at most `n` times. The matched element is /// not contained in the subslices. #[unstable = "iterator type may change"] - fn splitn<'a>(&'a self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<Splits<'a, T>>; + fn splitn<'a, P>(&'a self, n: uint, pred: P) -> SplitsN<Splits<'a, T, P>> where + P: FnMut(&T) -> bool; /// Returns an iterator over subslices separated by elements that match /// `pred` limited to splitting at most `n` times. This starts at the end of /// the slice and works backwards. The matched element is not contained in /// the subslices. #[unstable = "iterator type may change"] - fn rsplitn<'a>(&'a self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<Splits<'a, T>>; + fn rsplitn<'a, P>(&'a self, n: uint, pred: P) -> SplitsN<Splits<'a, T, P>> where + P: FnMut(&T) -> bool; /// Returns an iterator over all contiguous windows of length /// `size`. The windows overlap. If the slice is shorter than @@ -235,7 +238,7 @@ pub trait SlicePrelude<T> for Sized? { /// assert!(match r { Found(1...4) => true, _ => false, }); /// ``` #[unstable = "waiting on unboxed closures"] - fn binary_search(&self, f: |&T| -> Ordering) -> BinarySearchResult; + fn binary_search<F>(&self, f: F) -> BinarySearchResult where F: FnMut(&T) -> Ordering; /// Return the number of elements in the slice /// @@ -316,20 +319,23 @@ pub trait SlicePrelude<T> for Sized? { /// Returns an iterator over mutable subslices separated by elements that /// match `pred`. The matched element is not contained in the subslices. #[unstable = "waiting on unboxed closures, iterator type name conventions"] - fn split_mut<'a>(&'a mut self, pred: |&T|: 'a -> bool) -> MutSplits<'a, T>; + fn split_mut<'a, P>(&'a mut self, pred: P) -> MutSplits<'a, T, P> where + P: FnMut(&T) -> bool; /// Returns an iterator over subslices separated by elements that match /// `pred`, limited to splitting at most `n` times. The matched element is /// not contained in the subslices. #[unstable = "waiting on unboxed closures, iterator type name conventions"] - fn splitn_mut<'a>(&'a mut self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<MutSplits<'a, T>>; + fn splitn_mut<'a, P>(&'a mut self, n: uint, pred: P) -> SplitsN<MutSplits<'a, T, P>> where + P: FnMut(&T) -> bool; /// Returns an iterator over subslices separated by elements that match /// `pred` limited to splitting at most `n` times. This starts at the end of /// the slice and works backwards. The matched element is not contained in /// the subslices. #[unstable = "waiting on unboxed closures, iterator type name conventions"] - fn rsplitn_mut<'a>(&'a mut self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<MutSplits<'a, T>>; + fn rsplitn_mut<'a, P>(&'a mut self, n: uint, pred: P) -> SplitsN<MutSplits<'a, T, P>> where + P: FnMut(&T) -> bool; /// Returns an iterator over `chunk_size` elements of the slice at a time. /// The chunks are mutable and do not overlap. If `chunk_size` does @@ -470,7 +476,7 @@ impl<T> SlicePrelude<T> for [T] { } #[inline] - fn split<'a>(&'a self, pred: |&T|: 'a -> bool) -> Splits<'a, T> { + fn split<'a, P>(&'a self, pred: P) -> Splits<'a, T, P> where P: FnMut(&T) -> bool { Splits { v: self, pred: pred, @@ -479,7 +485,9 @@ impl<T> SlicePrelude<T> for [T] { } #[inline] - fn splitn<'a>(&'a self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<Splits<'a, T>> { + fn splitn<'a, P>(&'a self, n: uint, pred: P) -> SplitsN<Splits<'a, T, P>> where + P: FnMut(&T) -> bool, + { SplitsN { iter: self.split(pred), count: n, @@ -488,7 +496,9 @@ impl<T> SlicePrelude<T> for [T] { } #[inline] - fn rsplitn<'a>(&'a self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<Splits<'a, T>> { + fn rsplitn<'a, P>(&'a self, n: uint, pred: P) -> SplitsN<Splits<'a, T, P>> where + P: FnMut(&T) -> bool, + { SplitsN { iter: self.split(pred), count: n, @@ -542,7 +552,7 @@ impl<T> SlicePrelude<T> for [T] { } #[unstable] - fn binary_search(&self, f: |&T| -> Ordering) -> BinarySearchResult { + fn binary_search<F>(&self, mut f: F) -> BinarySearchResult where F: FnMut(&T) -> Ordering { let mut base : uint = 0; let mut lim : uint = self.len(); @@ -637,12 +647,14 @@ impl<T> SlicePrelude<T> for [T] { } #[inline] - fn split_mut<'a>(&'a mut self, pred: |&T|: 'a -> bool) -> MutSplits<'a, T> { + fn split_mut<'a, P>(&'a mut self, pred: P) -> MutSplits<'a, T, P> where P: FnMut(&T) -> bool { MutSplits { v: self, pred: pred, finished: false } } #[inline] - fn splitn_mut<'a>(&'a mut self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<MutSplits<'a, T>> { + fn splitn_mut<'a, P>(&'a mut self, n: uint, pred: P) -> SplitsN<MutSplits<'a, T, P>> where + P: FnMut(&T) -> bool + { SplitsN { iter: self.split_mut(pred), count: n, @@ -651,7 +663,9 @@ impl<T> SlicePrelude<T> for [T] { } #[inline] - fn rsplitn_mut<'a>(&'a mut self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN<MutSplits<'a, T>> { + fn rsplitn_mut<'a, P>(&'a mut self, n: uint, pred: P) -> SplitsN<MutSplits<'a, T, P>> where + P: FnMut(&T) -> bool, + { SplitsN { iter: self.split_mut(pred), count: n, @@ -1271,14 +1285,14 @@ trait SplitsIter<E>: DoubleEndedIterator<E> { /// An iterator over subslices separated by elements that match a predicate /// function. #[experimental = "needs review"] -pub struct Splits<'a, T:'a> { +pub struct Splits<'a, T:'a, P> where P: FnMut(&T) -> bool { v: &'a [T], - pred: |t: &T|: 'a -> bool, + pred: P, finished: bool } #[experimental = "needs review"] -impl<'a, T> Iterator<&'a [T]> for Splits<'a, T> { +impl<'a, T, P> Iterator<&'a [T]> for Splits<'a, T, P> where P: FnMut(&T) -> bool { #[inline] fn next(&mut self) -> Option<&'a [T]> { if self.finished { return None; } @@ -1304,7 +1318,7 @@ impl<'a, T> Iterator<&'a [T]> for Splits<'a, T> { } #[experimental = "needs review"] -impl<'a, T> DoubleEndedIterator<&'a [T]> for Splits<'a, T> { +impl<'a, T, P> DoubleEndedIterator<&'a [T]> for Splits<'a, T, P> where P: FnMut(&T) -> bool { #[inline] fn next_back(&mut self) -> Option<&'a [T]> { if self.finished { return None; } @@ -1320,7 +1334,7 @@ impl<'a, T> DoubleEndedIterator<&'a [T]> for Splits<'a, T> { } } -impl<'a, T> SplitsIter<&'a [T]> for Splits<'a, T> { +impl<'a, T, P> SplitsIter<&'a [T]> for Splits<'a, T, P> where P: FnMut(&T) -> bool { #[inline] fn finish(&mut self) -> Option<&'a [T]> { if self.finished { None } else { self.finished = true; Some(self.v) } @@ -1330,13 +1344,13 @@ impl<'a, T> SplitsIter<&'a [T]> for Splits<'a, T> { /// An iterator over the subslices of the vector which are separated /// by elements that match `pred`. #[experimental = "needs review"] -pub struct MutSplits<'a, T:'a> { +pub struct MutSplits<'a, T:'a, P> where P: FnMut(&T) -> bool { v: &'a mut [T], - pred: |t: &T|: 'a -> bool, + pred: P, finished: bool } -impl<'a, T> SplitsIter<&'a mut [T]> for MutSplits<'a, T> { +impl<'a, T, P> SplitsIter<&'a mut [T]> for MutSplits<'a, T, P> where P: FnMut(&T) -> bool { #[inline] fn finish(&mut self) -> Option<&'a mut [T]> { if self.finished { @@ -1349,7 +1363,7 @@ impl<'a, T> SplitsIter<&'a mut [T]> for MutSplits<'a, T> { } #[experimental = "needs review"] -impl<'a, T> Iterator<&'a mut [T]> for MutSplits<'a, T> { +impl<'a, T, P> Iterator<&'a mut [T]> for MutSplits<'a, T, P> where P: FnMut(&T) -> bool { #[inline] fn next(&mut self) -> Option<&'a mut [T]> { if self.finished { return None; } @@ -1382,7 +1396,9 @@ impl<'a, T> Iterator<&'a mut [T]> for MutSplits<'a, T> { } #[experimental = "needs review"] -impl<'a, T> DoubleEndedIterator<&'a mut [T]> for MutSplits<'a, T> { +impl<'a, T, P> DoubleEndedIterator<&'a mut [T]> for MutSplits<'a, T, P> where + P: FnMut(&T) -> bool, +{ #[inline] fn next_back(&mut self) -> Option<&'a mut [T]> { if self.finished { return None; } @@ -1709,6 +1725,7 @@ pub mod raw { use mem::transmute; use ptr::RawPtr; use raw::Slice; + use ops::FnOnce; use option::Option; use option::Option::{None, Some}; @@ -1716,8 +1733,9 @@ pub mod raw { /// not bytes). #[inline] #[deprecated = "renamed to slice::from_raw_buf"] - pub unsafe fn buf_as_slice<T,U>(p: *const T, len: uint, f: |v: &[T]| -> U) - -> U { + pub unsafe fn buf_as_slice<T, U, F>(p: *const T, len: uint, f: F) -> U where + F: FnOnce(&[T]) -> U, + { f(transmute(Slice { data: p, len: len @@ -1728,12 +1746,9 @@ pub mod raw { /// not bytes). #[inline] #[deprecated = "renamed to slice::from_raw_mut_buf"] - pub unsafe fn mut_buf_as_slice<T, - U>( - p: *mut T, - len: uint, - f: |v: &mut [T]| -> U) - -> U { + pub unsafe fn mut_buf_as_slice<T, U, F>(p: *mut T, len: uint, f: F) -> U where + F: FnOnce(&mut [T]) -> U, + { f(transmute(Slice { data: p as *const T, len: len |
