]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/extern-call-indirect.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / extern-call-indirect.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(libc)]
12
13 extern crate libc;
14
15 mod rustrt {
16     extern crate libc;
17
18     #[link(name = "rust_test_helpers")]
19     extern {
20         pub fn rust_dbg_call(cb: extern "C" fn(libc::uintptr_t) -> libc::uintptr_t,
21                              data: libc::uintptr_t)
22                              -> libc::uintptr_t;
23     }
24 }
25
26 extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
27     if data == 1 {
28         data
29     } else {
30         fact(data - 1) * data
31     }
32 }
33
34 fn fact(n: libc::uintptr_t) -> libc::uintptr_t {
35     unsafe {
36         println!("n = {}", n);
37         rustrt::rust_dbg_call(cb, n)
38     }
39 }
40
41 pub fn main() {
42     let result = fact(10);
43     println!("result = {}", result);
44     assert_eq!(result, 3628800);
45 }