- 14 12月, 2014 40 次提交
-
-
由 Niko Matsakis 提交于
-
由 Niko Matsakis 提交于
-
由 Niko Matsakis 提交于
-
由 Niko Matsakis 提交于
-
由 Niko Matsakis 提交于
cc @steveklabnick
-
由 Niko Matsakis 提交于
duplicate tests around object types.
-
由 Niko Matsakis 提交于
-
由 Niko Matsakis 提交于
-
由 Niko Matsakis 提交于
to prevent such oversights in the future.
-
由 Niko Matsakis 提交于
language. Recommend `move||` instead.
-
由 Niko Matsakis 提交于
with the fn traits
-
由 Niko Matsakis 提交于
Purge the hack that allows `FnOnce` to be used with a by-value self method. Besides being yucky, it will cause problems if we try to make all traits implement themselves, which would make a lot of things in life easier. Also, it was inextricably linked to `Box`, which was not the intention. We can work around its absence, so better to reimplement it later in a more thorough fashion.
-
由 Niko Matsakis 提交于
boxed `FnOnce` closures.
-
由 bors 提交于
r? @alexcrichton or anyone else closes #18006
-
由 bors 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 bors 提交于
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
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-
由 Jorge Aparicio 提交于
-