2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
2015-12-02 19:31:49 -06:00
|
|
|
#![feature(unboxed_closures, fn_traits)]
|
2014-06-01 20:41:46 -05:00
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
2015-01-12 09:27:25 -06:00
|
|
|
impl FnMut<(i32,)> for S {
|
|
|
|
extern "rust-call" fn call_mut(&mut self, (x,): (i32,)) -> i32 {
|
2014-06-01 20:41:46 -05:00
|
|
|
x * x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 09:08:33 -05: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 09:27:25 -06:00
|
|
|
fn call_it<F:FnMut(i32)->i32>(mut f: F, x: i32) -> i32 {
|
2015-01-05 13:07:10 -06:00
|
|
|
f(x) + 3
|
2014-06-01 20:41:46 -05:00
|
|
|
}
|
|
|
|
|
2019-05-28 13:47:21 -05:00
|
|
|
fn call_box(f: &mut dyn FnMut(i32) -> i32, x: i32) -> i32 {
|
2015-01-05 13:07:10 -06:00
|
|
|
f(x) + 3
|
2014-06-01 20:41:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = call_it(S, 1);
|
|
|
|
let y = call_box(&mut S, 1);
|
2015-06-07 13:00:38 -05:00
|
|
|
assert_eq!(x, 4);
|
|
|
|
assert_eq!(y, 4);
|
2014-06-01 20:41:46 -05:00
|
|
|
}
|