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
|
|
|
|
// any Fn trait to be used where Fn is implemented.
|
|
|
|
|
2015-12-03 01:31:49 +00:00
|
|
|
#![feature(unboxed_closures, fn_traits)]
|
2014-10-28 11:24:25 +00:00
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
2015-01-12 15:27:25 +00:00
|
|
|
impl Fn<(i32,)> for S {
|
|
|
|
extern "rust-call" fn call(&self, (x,): (i32,)) -> i32 {
|
2014-10-28 11:24:25 +00:00
|
|
|
x * x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 14:08:33 +00:00
|
|
|
impl FnMut<(i32,)> for S {
|
|
|
|
extern "rust-call" fn call_mut(&mut self, args: (i32,)) -> i32 { self.call(args) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FnOnce<(i32,)> for S {
|
|
|
|
type Output = i32;
|
|
|
|
extern "rust-call" fn call_once(self, args: (i32,)) -> i32 { self.call(args) }
|
|
|
|
}
|
|
|
|
|
2015-01-12 15:27:25 +00:00
|
|
|
fn call_it<F:Fn(i32)->i32>(f: &F, x: i32) -> i32 {
|
2015-01-05 19:07:10 +00:00
|
|
|
f(x)
|
2014-10-28 11:24:25 +00:00
|
|
|
}
|
|
|
|
|
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-10-28 11:24:25 +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-10-28 11:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = call_it(&S, 22);
|
|
|
|
let y = call_it_mut(&mut S, 22);
|
|
|
|
let z = call_it_once(S, 22);
|
|
|
|
assert_eq!(x, y);
|
|
|
|
assert_eq!(y, z);
|
|
|
|
}
|