8f6c879d2a
The primary motivation here is to sidestep #19032 -- for a time, I thought that we should improve coherence or otherwise extend the language, but I now think that any such changes will require more time to bake. In the meantime, inheritance amongst the fn traits is both logically correct *and* a simple solution to that obstacle. This change introduces inheritance and modifies the compiler so that it can properly generate impls for closures and fns. Things enabled by this PR (but not included in this PR): 1. An impl of `FnMut` for `&mut F` where `F : FnMut` (https://github.com/rust-lang/rust/issues/23015). 2. A better version of `Thunk` I've been calling `FnBox`. I did not include either of these in the PR because: 1. Adding the impls in 1 currently induces a coherence conflict with the pattern trait. This is interesting and merits some discussion. 2. `FnBox` deserves to be a PR of its own. The main downside to this design is (a) the need to write impls by hand; (b) the possibility of implementing `FnMut` with different semantics from `Fn`, etc. Point (a) is minor -- in particular, it does not affect normal closure usage -- and could be addressed in the future in many ways (better defaults; convenient macros; specialization; etc). Point (b) is unfortunate but "just a bug" from my POV, and certainly not unique to these traits (c.f. Copy/Clone, PartialEq/Eq, etc). (Until we lift the feature-gate on implementing the Fn traits, in any case, there is room to correct both of these if we find a nice way.) Note that I believe this change is reversible in the future if we decide on another course of action, due to the feature gate on implementing the `Fn` traits, though I do not (currently) think we should reverse it. Fixes #18835. r? @nrc
45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
// file at the top-level directory of this distribution and at
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
// option. This file may not be copied, modified, or distributed
|
|
// except according to those terms.
|
|
|
|
// pretty-expanded FIXME #23616
|
|
|
|
#![feature(unboxed_closures, core)]
|
|
|
|
use std::ops::FnMut;
|
|
|
|
struct S;
|
|
|
|
impl FnMut<(i32,)> for S {
|
|
extern "rust-call" fn call_mut(&mut self, (x,): (i32,)) -> i32 {
|
|
x * x
|
|
}
|
|
}
|
|
|
|
impl FnOnce<(i32,)> for S {
|
|
type Output = i32;
|
|
|
|
extern "rust-call" fn call_once(mut self, args: (i32,)) -> i32 { self.call_mut(args) }
|
|
}
|
|
|
|
fn call_it<F:FnMut(i32)->i32>(mut f: F, x: i32) -> i32 {
|
|
f(x) + 3
|
|
}
|
|
|
|
fn call_box(f: &mut FnMut(i32) -> i32, x: i32) -> i32 {
|
|
f(x) + 3
|
|
}
|
|
|
|
fn main() {
|
|
let x = call_it(S, 1);
|
|
let y = call_box(&mut S, 1);
|
|
assert!(x == 4);
|
|
assert!(y == 4);
|
|
}
|