2019-02-13 03:07:31 -06:00
|
|
|
trait Answer {
|
|
|
|
fn answer() -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Answer for i32 {
|
|
|
|
fn answer() -> i32 {
|
|
|
|
42
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A generic function, to make its address unstable
|
|
|
|
fn f<T: Answer>() -> T {
|
|
|
|
Answer::answer()
|
2016-06-08 06:43:34 -05:00
|
|
|
}
|
|
|
|
|
2016-09-15 09:32:30 -05:00
|
|
|
fn g(i: i32) -> i32 {
|
|
|
|
i*42
|
|
|
|
}
|
|
|
|
|
2016-09-21 08:57:13 -05:00
|
|
|
fn h(i: i32, j: i32) -> i32 {
|
|
|
|
j * i * 7
|
|
|
|
}
|
|
|
|
|
2018-12-17 03:12:04 -06:00
|
|
|
fn return_fn_ptr(f: fn() -> i32) -> fn() -> i32 {
|
2016-06-08 06:43:34 -05:00
|
|
|
f
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_fn_ptr() -> i32 {
|
2018-12-17 03:12:04 -06:00
|
|
|
return_fn_ptr(f)()
|
2016-06-08 06:43:34 -05:00
|
|
|
}
|
|
|
|
|
2016-09-15 09:32:30 -05:00
|
|
|
fn indirect<F: Fn() -> i32>(f: F) -> i32 { f() }
|
|
|
|
fn indirect_mut<F: FnMut() -> i32>(mut f: F) -> i32 { f() }
|
|
|
|
fn indirect_once<F: FnOnce() -> i32>(f: F) -> i32 { f() }
|
|
|
|
|
|
|
|
fn indirect2<F: Fn(i32) -> i32>(f: F) -> i32 { f(10) }
|
|
|
|
fn indirect_mut2<F: FnMut(i32) -> i32>(mut f: F) -> i32 { f(10) }
|
|
|
|
fn indirect_once2<F: FnOnce(i32) -> i32>(f: F) -> i32 { f(10) }
|
|
|
|
|
2016-09-21 08:57:13 -05:00
|
|
|
fn indirect3<F: Fn(i32, i32) -> i32>(f: F) -> i32 { f(10, 3) }
|
|
|
|
fn indirect_mut3<F: FnMut(i32, i32) -> i32>(mut f: F) -> i32 { f(10, 3) }
|
|
|
|
fn indirect_once3<F: FnOnce(i32, i32) -> i32>(f: F) -> i32 { f(10, 3) }
|
|
|
|
|
2016-06-13 07:27:05 -05:00
|
|
|
fn main() {
|
|
|
|
assert_eq!(call_fn_ptr(), 42);
|
2016-09-15 09:32:30 -05:00
|
|
|
assert_eq!(indirect(f), 42);
|
|
|
|
assert_eq!(indirect_mut(f), 42);
|
|
|
|
assert_eq!(indirect_once(f), 42);
|
|
|
|
assert_eq!(indirect2(g), 420);
|
|
|
|
assert_eq!(indirect_mut2(g), 420);
|
|
|
|
assert_eq!(indirect_once2(g), 420);
|
2016-09-21 08:57:13 -05:00
|
|
|
assert_eq!(indirect3(h), 210);
|
|
|
|
assert_eq!(indirect_mut3(h), 210);
|
|
|
|
assert_eq!(indirect_once3(h), 210);
|
2018-12-17 03:12:04 -06:00
|
|
|
let g = f as fn() -> i32;
|
|
|
|
assert!(return_fn_ptr(g) == g);
|
|
|
|
assert!(return_fn_ptr(g) as unsafe fn() -> i32 == g as fn() -> i32 as unsafe fn() -> i32);
|
2018-12-25 06:29:38 -06:00
|
|
|
assert!(return_fn_ptr(f) != f);
|
2016-06-13 07:27:05 -05:00
|
|
|
}
|