|
| 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 runtime_only_0(x: i32) -> i32 { |
| 10 | + let x: Value<i32> = Rc::new(RefCell::new(x)); |
| 11 | + return ((*x.borrow()) * 2); |
| 12 | +} |
| 13 | +pub fn first_1(p: Ptr<i32>) -> i32 { |
| 14 | + let p: Value<Ptr<i32>> = Rc::new(RefCell::new(p)); |
| 15 | + return ((*p.borrow()).read()); |
| 16 | +} |
| 17 | +pub fn scaled_2(x: i32) -> i32 { |
| 18 | + let x: Value<i32> = Rc::new(RefCell::new(x)); |
| 19 | + if ((*x.borrow()) < 0) { |
| 20 | + return ({ runtime_only_0(-(*x.borrow())) }); |
| 21 | + } |
| 22 | + return (*x.borrow()); |
| 23 | +} |
| 24 | +pub fn half_3(x: f64) -> f64 { |
| 25 | + let x: Value<f64> = Rc::new(RefCell::new(x)); |
| 26 | + return ((*x.borrow()) / 2.0E+0); |
| 27 | +} |
| 28 | +#[derive(Default)] |
| 29 | +pub struct P { |
| 30 | + pub v: Value<i32>, |
| 31 | +} |
| 32 | +impl Clone for P { |
| 33 | + fn clone(&self) -> Self { |
| 34 | + let __this: Value<P> = Rc::new(RefCell::new(Self { |
| 35 | + v: Rc::new(RefCell::new((*self.v.borrow()))), |
| 36 | + })); |
| 37 | + let this: Ptr<P> = __this.as_pointer(); |
| 38 | + Rc::try_unwrap(__this).ok().unwrap().into_inner() |
| 39 | + } |
| 40 | +} |
| 41 | +impl ByteRepr for P { |
| 42 | + fn byte_size() -> usize { |
| 43 | + 4 |
| 44 | + } |
| 45 | + fn to_bytes(&self, buf: &mut [u8]) { |
| 46 | + (*self.v.borrow()).to_bytes(&mut buf[0..4]); |
| 47 | + } |
| 48 | + fn from_bytes(buf: &[u8]) -> Self { |
| 49 | + Self { |
| 50 | + v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))), |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | +pub fn main() { |
| 55 | + std::process::exit(main_0()); |
| 56 | +} |
| 57 | +fn main_0() -> i32 { |
| 58 | + let arr: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([7, 8]))); |
| 59 | + assert!((({ first_1((arr.as_pointer() as Ptr::<i32>),) }) == 7)); |
| 60 | + assert!((({ first_1((arr.as_pointer() as Ptr::<i32>).offset((1) as isize),) }) == 8)); |
| 61 | + assert!((({ scaled_2(3,) }) == 3)); |
| 62 | + assert!((({ scaled_2(-3_i32,) }) == 6)); |
| 63 | + assert!((({ half_3(5.0E+0,) }) == 2.5E+0)); |
| 64 | + let p: Value<P> = Rc::new(RefCell::new(P { |
| 65 | + v: Rc::new(RefCell::new(9)), |
| 66 | + })); |
| 67 | + assert!((({ PImpl::get(&p.as_pointer(),) }) == 9)); |
| 68 | + let k: Value<i32> = Rc::new(RefCell::new(({ scaled_2(4) }))); |
| 69 | + assert!(((*k.borrow()) == 4)); |
| 70 | + return 0; |
| 71 | +} |
| 72 | +pub trait PImpl { |
| 73 | + fn get(&self) -> i32; |
| 74 | +} |
| 75 | +impl PImpl for Ptr<P> { |
| 76 | + fn get(&self) -> i32 { |
| 77 | + return (*(*(*self).upgrade().deref()).v.borrow()); |
| 78 | + } |
| 79 | +} |
0 commit comments