|
| 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 | +#[derive(Default)] |
| 10 | +pub struct Point { |
| 11 | + pub x: Value<i32>, |
| 12 | +} |
| 13 | +impl Clone for Point { |
| 14 | + fn clone(&self) -> Self { |
| 15 | + let mut this = Self { |
| 16 | + x: Rc::new(RefCell::new((*self.x.borrow()))), |
| 17 | + }; |
| 18 | + this |
| 19 | + } |
| 20 | +} |
| 21 | +impl ByteRepr for Point { |
| 22 | + fn byte_size() -> usize { |
| 23 | + 4 |
| 24 | + } |
| 25 | + fn to_bytes(&self, buf: &mut [u8]) { |
| 26 | + (*self.x.borrow()).to_bytes(&mut buf[0..4]); |
| 27 | + } |
| 28 | + fn from_bytes(buf: &[u8]) -> Self { |
| 29 | + Self { |
| 30 | + x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))), |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | +#[derive(Default)] |
| 35 | +pub struct Box_int_ { |
| 36 | + pub val: Value<i32>, |
| 37 | +} |
| 38 | +impl Box_int_ { |
| 39 | + pub fn twice(&self) -> i32 { |
| 40 | + return ((*self.val.borrow()) + (*self.val.borrow())); |
| 41 | + } |
| 42 | +} |
| 43 | +impl Clone for Box_int_ { |
| 44 | + fn clone(&self) -> Self { |
| 45 | + let mut this = Self { |
| 46 | + val: Rc::new(RefCell::new((*self.val.borrow()))), |
| 47 | + }; |
| 48 | + this |
| 49 | + } |
| 50 | +} |
| 51 | +impl ByteRepr for Box_int_ { |
| 52 | + fn byte_size() -> usize { |
| 53 | + 4 |
| 54 | + } |
| 55 | + fn to_bytes(&self, buf: &mut [u8]) { |
| 56 | + (*self.val.borrow()).to_bytes(&mut buf[0..4]); |
| 57 | + } |
| 58 | + fn from_bytes(buf: &[u8]) -> Self { |
| 59 | + Self { |
| 60 | + val: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))), |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | +#[derive(Default)] |
| 65 | +pub struct Box_Point_ { |
| 66 | + pub val: Value<Point>, |
| 67 | +} |
| 68 | +impl Box_Point_ { |
| 69 | + pub fn get(&self) -> Point { |
| 70 | + return (*self.val.borrow()).clone(); |
| 71 | + } |
| 72 | +} |
| 73 | +impl Clone for Box_Point_ { |
| 74 | + fn clone(&self) -> Self { |
| 75 | + let mut this = Self { |
| 76 | + val: Rc::new(RefCell::new((*self.val.borrow()).clone())), |
| 77 | + }; |
| 78 | + this |
| 79 | + } |
| 80 | +} |
| 81 | +impl ByteRepr for Box_Point_ { |
| 82 | + fn byte_size() -> usize { |
| 83 | + 4 |
| 84 | + } |
| 85 | + fn to_bytes(&self, buf: &mut [u8]) { |
| 86 | + (*self.val.borrow()).to_bytes(&mut buf[0..4]); |
| 87 | + } |
| 88 | + fn from_bytes(buf: &[u8]) -> Self { |
| 89 | + Self { |
| 90 | + val: Rc::new(RefCell::new(<Point>::from_bytes(&buf[0..4]))), |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +pub fn main() { |
| 95 | + std::process::exit(main_0()); |
| 96 | +} |
| 97 | +fn main_0() -> i32 { |
| 98 | + let i: Value<Box_int_> = Rc::new(RefCell::new(Box_int_ { |
| 99 | + val: Rc::new(RefCell::new(3)), |
| 100 | + })); |
| 101 | + assert!((({ (*i.borrow()).twice() }) == 6)); |
| 102 | + let p: Value<Box_Point_> = Rc::new(RefCell::new(Box_Point_ { |
| 103 | + val: Rc::new(RefCell::new(Point { |
| 104 | + x: Rc::new(RefCell::new(4)), |
| 105 | + })), |
| 106 | + })); |
| 107 | + assert!(((*({ (*p.borrow()).get() }).x.borrow()) == 4)); |
| 108 | + return 0; |
| 109 | +} |
0 commit comments