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/librustrt | |
| 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/librustrt')
| -rw-r--r-- | src/librustrt/args.rs | 4 | ||||
| -rw-r--r-- | src/librustrt/c_str.rs | 57 | ||||
| -rw-r--r-- | src/librustrt/lib.rs | 1 | ||||
| -rw-r--r-- | src/librustrt/task.rs | 7 |
4 files changed, 48 insertions, 21 deletions
diff --git a/src/librustrt/args.rs b/src/librustrt/args.rs index d94f731e75c..c1b48e989a1 100644 --- a/src/librustrt/args.rs +++ b/src/librustrt/args.rs @@ -89,7 +89,7 @@ mod imp { }) } - fn with_lock<T>(f: || -> T) -> T { + fn with_lock<T, F>(f: F) -> T where F: FnOnce() -> T { unsafe { let _guard = LOCK.lock(); f() @@ -128,7 +128,7 @@ mod imp { assert!(take() == Some(expected.clone())); assert!(take() == None); - (|| { + (|&mut:| { }).finally(|| { // Restore the actual global state. match saved_value { diff --git a/src/librustrt/c_str.rs b/src/librustrt/c_str.rs index 07094f08c5d..865c1af1d14 100644 --- a/src/librustrt/c_str.rs +++ b/src/librustrt/c_str.rs @@ -72,6 +72,7 @@ use collections::hash; use core::fmt; use core::kinds::{Sized, marker}; use core::mem; +use core::ops::{FnMut, FnOnce}; use core::prelude::{Clone, Drop, Eq, Iterator}; use core::prelude::{SlicePrelude, None, Option, Ordering, PartialEq}; use core::prelude::{PartialOrd, RawPtr, Some, StrPrelude, range}; @@ -319,14 +320,18 @@ pub trait ToCStr for Sized? { /// /// Panics the task if the receiver has an interior null. #[inline] - fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T { + fn with_c_str<T, F>(&self, f: F) -> T where + F: FnOnce(*const libc::c_char) -> T, + { let c_str = self.to_c_str(); f(c_str.as_ptr()) } /// Unsafe variant of `with_c_str()` that doesn't check for nulls. #[inline] - unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T { + unsafe fn with_c_str_unchecked<T, F>(&self, f: F) -> T where + F: FnOnce(*const libc::c_char) -> T, + { let c_str = self.to_c_str_unchecked(); f(c_str.as_ptr()) } @@ -344,12 +349,16 @@ impl ToCStr for str { } #[inline] - fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T { + fn with_c_str<T, F>(&self, f: F) -> T where + F: FnOnce(*const libc::c_char) -> T, + { self.as_bytes().with_c_str(f) } #[inline] - unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T { + unsafe fn with_c_str_unchecked<T, F>(&self, f: F) -> T where + F: FnOnce(*const libc::c_char) -> T, + { self.as_bytes().with_c_str_unchecked(f) } } @@ -366,12 +375,16 @@ impl ToCStr for String { } #[inline] - fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T { + fn with_c_str<T, F>(&self, f: F) -> T where + F: FnOnce(*const libc::c_char) -> T, + { self.as_bytes().with_c_str(f) } #[inline] - unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T { + unsafe fn with_c_str_unchecked<T, F>(&self, f: F) -> T where + F: FnOnce(*const libc::c_char) -> T, + { self.as_bytes().with_c_str_unchecked(f) } } @@ -397,11 +410,15 @@ impl ToCStr for [u8] { CString::new(buf as *const libc::c_char, true) } - fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T { + fn with_c_str<T, F>(&self, f: F) -> T where + F: FnOnce(*const libc::c_char) -> T, + { unsafe { with_c_str(self, true, f) } } - unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T { + unsafe fn with_c_str_unchecked<T, F>(&self, f: F) -> T where + F: FnOnce(*const libc::c_char) -> T, + { with_c_str(self, false, f) } } @@ -418,19 +435,24 @@ impl<'a, Sized? T: ToCStr> ToCStr for &'a T { } #[inline] - fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T { + fn with_c_str<T, F>(&self, f: F) -> T where + F: FnOnce(*const libc::c_char) -> T, + { (**self).with_c_str(f) } #[inline] - unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T { + unsafe fn with_c_str_unchecked<T, F>(&self, f: F) -> T where + F: FnOnce(*const libc::c_char) -> T, + { (**self).with_c_str_unchecked(f) } } // Unsafe function that handles possibly copying the &[u8] into a stack array. -unsafe fn with_c_str<T>(v: &[u8], checked: bool, - f: |*const libc::c_char| -> T) -> T { +unsafe fn with_c_str<T, F>(v: &[u8], checked: bool, f: F) -> T where + F: FnOnce(*const libc::c_char) -> T, +{ let c_str = if v.len() < BUF_LEN { let mut buf: [u8, .. BUF_LEN] = mem::uninitialized(); slice::bytes::copy_memory(&mut buf, v); @@ -489,9 +511,12 @@ impl<'a> Iterator<libc::c_char> for CChars<'a> { /// /// The specified closure is invoked with each string that /// is found, and the number of strings found is returned. -pub unsafe fn from_c_multistring(buf: *const libc::c_char, - count: Option<uint>, - f: |&CString|) -> uint { +pub unsafe fn from_c_multistring<F>(buf: *const libc::c_char, + count: Option<uint>, + mut f: F) + -> uint where + F: FnMut(&CString), +{ let mut curr_ptr: uint = buf as uint; let mut ctr = 0; @@ -678,7 +703,7 @@ mod tests { #[test] fn test_clone_noleak() { - fn foo(f: |c: &CString|) { + fn foo<F>(f: F) where F: FnOnce(&CString) { let s = "test".to_string(); let c = s.to_c_str(); // give the closure a non-owned CString diff --git a/src/librustrt/lib.rs b/src/librustrt/lib.rs index 066e8c51aef..c2ee91d6acc 100644 --- a/src/librustrt/lib.rs +++ b/src/librustrt/lib.rs @@ -19,6 +19,7 @@ #![feature(macro_rules, phase, globs, thread_local, asm)] #![feature(linkage, lang_items, unsafe_destructor, default_type_params)] #![feature(import_shadowing, slicing_syntax)] +#![feature(unboxed_closures)] #![no_std] #![experimental] diff --git a/src/librustrt/task.rs b/src/librustrt/task.rs index 325bdc284ac..7e657d3aef3 100644 --- a/src/librustrt/task.rs +++ b/src/librustrt/task.rs @@ -22,6 +22,7 @@ use core::atomic::{AtomicUint, SeqCst}; use core::iter::{IteratorExt, Take}; use core::kinds::marker; use core::mem; +use core::ops::FnMut; use core::prelude::{Clone, Drop, Err, Iterator, None, Ok, Option, Send, Some}; use core::prelude::{drop}; @@ -297,9 +298,9 @@ impl Task { // `awoken` field which indicates whether we were actually woken up via some // invocation of `reawaken`. This flag is only ever accessed inside the // lock, so there's no need to make it atomic. - pub fn deschedule(mut self: Box<Task>, - times: uint, - f: |BlockedTask| -> ::core::result::Result<(), BlockedTask>) { + pub fn deschedule<F>(mut self: Box<Task>, times: uint, mut f: F) where + F: FnMut(BlockedTask) -> ::core::result::Result<(), BlockedTask>, + { unsafe { let me = &mut *self as *mut Task; let task = BlockedTask::block(self); |
