2018-08-30 12:18:55 +00:00
|
|
|
//@ run-pass
|
2014-10-28 11:24:25 +00:00
|
|
|
// Checks that the Fn trait hierarchy rules permit
|
|
|
|
// FnMut or FnOnce to be used where FnMut is implemented.
|
|
|
|
|
2015-12-03 01:31:49 +00:00
|
|
|
#![feature(unboxed_closures, fn_traits)]
|
2014-06-02 01:41:46 +00:00
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
2015-01-12 15:27:25 +00:00
|
|
|
impl FnMut<(i32,)> for S {
|
|
|
|
extern "rust-call" fn call_mut(&mut self, (x,): (i32,)) -> i32 {
|
2014-06-02 01:41:46 +00:00
|
|
|
x * x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 14:08:33 +00:00
|
|
|
impl FnOnce<(i32,)> for S {
|
|
|
|
type Output = i32;
|
|
|
|
|
|
|
|
extern "rust-call" fn call_once(mut self, args: (i32,)) -> i32 { self.call_mut(args) }
|
|
|
|
}
|
|
|
|
|
2015-01-12 15:27:25 +00:00
|
|
|
fn call_it_mut<F:FnMut(i32)->i32>(f: &mut F, x: i32) -> i32 {
|
2015-01-05 19:07:10 +00:00
|
|
|
f(x)
|
2014-06-02 01:41:46 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 15:27:25 +00:00
|
|
|
fn call_it_once<F:FnOnce(i32)->i32>(f: F, x: i32) -> i32 {
|
2015-01-05 19:07:10 +00:00
|
|
|
f(x)
|
2014-06-02 01:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2014-10-28 11:24:25 +00:00
|
|
|
let y = call_it_mut(&mut S, 22);
|
|
|
|
let z = call_it_once(S, 22);
|
|
|
|
assert_eq!(y, z);
|
2014-06-02 01:41:46 +00:00
|
|
|
}
|