2014-12-01 08:23:40 -06:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Checks that higher-ranked extern fn pointers implement the full range of Fn traits.
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn square(x: &isize) -> isize { (*x) * (*x) }
|
2014-12-01 08:23:40 -06:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn call_it<F:Fn(&isize)->isize>(f: &F, x: isize) -> isize {
|
2014-12-01 08:23:40 -06:00
|
|
|
(*f)(&x)
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn call_it_boxed(f: &Fn(&isize) -> isize, x: isize) -> isize {
|
2015-12-02 19:31:49 -06:00
|
|
|
f(&x)
|
2014-12-01 08:23:40 -06:00
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn call_it_mut<F:FnMut(&isize)->isize>(f: &mut F, x: isize) -> isize {
|
2014-12-01 08:23:40 -06:00
|
|
|
(*f)(&x)
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn call_it_once<F:FnOnce(&isize)->isize>(f: F, x: isize) -> isize {
|
2014-12-01 08:23:40 -06:00
|
|
|
f(&x)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = call_it(&square, 22);
|
|
|
|
let x1 = call_it_boxed(&square, 22);
|
|
|
|
let y = call_it_mut(&mut square, 22);
|
|
|
|
let z = call_it_once(square, 22);
|
|
|
|
assert_eq!(x, square(&22));
|
|
|
|
assert_eq!(x1, square(&22));
|
|
|
|
assert_eq!(y, square(&22));
|
|
|
|
assert_eq!(z, square(&22));
|
|
|
|
}
|