|
| 1 | +extern crate libcc2rs; |
| 2 | +use libcc2rs::*; |
| 3 | +use std::cell::RefCell; |
| 4 | +use std::collections::BTreeMap; |
| 5 | +use std::io::prelude::*; |
| 6 | +use std::io::{Read, Seek, Write}; |
| 7 | +use std::os::fd::AsFd; |
| 8 | +use std::rc::{Rc, Weak}; |
| 9 | +pub fn square_0(mut x: i32) -> i32 { |
| 10 | + return (x * x); |
| 11 | +} |
| 12 | +pub fn twice_1(mut x: i32) -> i32 { |
| 13 | + return (2 * x); |
| 14 | +} |
| 15 | +pub fn call_ref_2(f: FnPtr<fn(i32) -> i32>, mut x: i32) -> i32 { |
| 16 | + return ({ f.call(x) }); |
| 17 | +} |
| 18 | +pub fn call_deduced_3(f: FnPtr<fn(i32) -> i32>, mut x: i32) -> i32 { |
| 19 | + return ({ f.call(x) }); |
| 20 | +} |
| 21 | +pub fn call_forwarded_4(f: FnPtr<fn(i32) -> i32>, mut x: i32) -> i32 { |
| 22 | + return ({ f.call(x) }); |
| 23 | +} |
| 24 | +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] |
| 25 | +#[byte_size(8)] |
| 26 | +pub struct Holder { |
| 27 | + #[offset(0)] |
| 28 | + #[byte_size(8)] |
| 29 | + pub f: FnPtr<fn(i32) -> i32>, |
| 30 | +} |
| 31 | +pub fn main() { |
| 32 | + __cpp2rust_init_globals(); |
| 33 | + std::process::exit(main_0()); |
| 34 | +} |
| 35 | +fn main_0() -> i32 { |
| 36 | + assert!((({ call_ref_2(FnPtr::<fn(i32) -> i32>::new(square_0), 3,) }) == 9)); |
| 37 | + assert!((({ call_ref_2(FnPtr::<fn(i32) -> i32>::new(twice_1), 3,) }) == 6)); |
| 38 | + let r: FnPtr<fn(i32) -> i32> = FnPtr::<fn(i32) -> i32>::new(square_0); |
| 39 | + assert!((({ r.call(4,) }) == 16)); |
| 40 | + assert!((({ call_ref_2((r).clone(), 5,) }) == 25)); |
| 41 | + assert!((({ call_deduced_3(FnPtr::<fn(i32) -> i32>::new(twice_1), 7,) }) == 14)); |
| 42 | + assert!((({ call_forwarded_4(FnPtr::<fn(i32) -> i32>::new(square_0), 6,) }) == 36)); |
| 43 | + let h: Value<Holder> = Rc::new(RefCell::new(Holder { |
| 44 | + f: FnPtr::<fn(i32) -> i32>::new(twice_1), |
| 45 | + })); |
| 46 | + assert!((({ HolderImpl::run(&h.as_pointer(), 8,) }) == 16)); |
| 47 | + return 0; |
| 48 | +} |
| 49 | +pub trait HolderImpl { |
| 50 | + fn run(&self, x: i32) -> i32; |
| 51 | +} |
| 52 | +impl HolderImpl for Ptr<Holder> { |
| 53 | + fn run(&self, mut x: i32) -> i32 { |
| 54 | + return ({ (*self).with(|__s| __s.f.clone()).call(x) }); |
| 55 | + } |
| 56 | +} |
| 57 | +pub fn __cpp2rust_init_globals() {} |
0 commit comments