|
| 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 Inner { |
| 11 | + pub value: Value<i32>, |
| 12 | +} |
| 13 | +impl Clone for Inner { |
| 14 | + fn clone(&self) -> Self { |
| 15 | + let mut this = Self { |
| 16 | + value: Rc::new(RefCell::new((*self.value.borrow()))), |
| 17 | + }; |
| 18 | + this |
| 19 | + } |
| 20 | +} |
| 21 | +impl ByteRepr for Inner {} |
| 22 | +#[derive(Default)] |
| 23 | +pub struct Outer { |
| 24 | + pub p: Value<Ptr<Inner>>, |
| 25 | +} |
| 26 | +impl Clone for Outer { |
| 27 | + fn clone(&self) -> Self { |
| 28 | + let mut this = Self { |
| 29 | + p: Rc::new(RefCell::new((*self.p.borrow()).clone())), |
| 30 | + }; |
| 31 | + this |
| 32 | + } |
| 33 | +} |
| 34 | +impl ByteRepr for Outer {} |
| 35 | +thread_local!( |
| 36 | + pub static alpha: Value<Inner> = Rc::new(RefCell::new(Inner { |
| 37 | + value: Rc::new(RefCell::new(1)), |
| 38 | + })); |
| 39 | +); |
| 40 | +thread_local!( |
| 41 | + pub static beta: Value<Inner> = Rc::new(RefCell::new(Inner { |
| 42 | + value: Rc::new(RefCell::new(2)), |
| 43 | + })); |
| 44 | +); |
| 45 | +thread_local!( |
| 46 | + pub static shared: Value<Inner> = Rc::new(RefCell::new(Inner { |
| 47 | + value: Rc::new(RefCell::new(42)), |
| 48 | + })); |
| 49 | +); |
| 50 | +thread_local!( |
| 51 | + pub static items: Value<Box<[Ptr<Inner>]>> = Rc::new(RefCell::new(Box::new([ |
| 52 | + (alpha.with(Value::clone).as_pointer()), |
| 53 | + (beta.with(Value::clone).as_pointer()), |
| 54 | + ]))); |
| 55 | +); |
| 56 | +thread_local!( |
| 57 | + pub static obj: Value<Outer> = Rc::new(RefCell::new(Outer { |
| 58 | + p: Rc::new(RefCell::new((shared.with(Value::clone).as_pointer()))), |
| 59 | + })); |
| 60 | +); |
| 61 | +pub fn main() { |
| 62 | + std::process::exit(main_0()); |
| 63 | +} |
| 64 | +fn main_0() -> i32 { |
| 65 | + assert!( |
| 66 | + ((*(*(*items.with(Value::clone).borrow())[(0) as usize] |
| 67 | + .upgrade() |
| 68 | + .deref()) |
| 69 | + .value |
| 70 | + .borrow()) |
| 71 | + == 1) |
| 72 | + ); |
| 73 | + assert!( |
| 74 | + ((*(*(*items.with(Value::clone).borrow())[(1) as usize] |
| 75 | + .upgrade() |
| 76 | + .deref()) |
| 77 | + .value |
| 78 | + .borrow()) |
| 79 | + == 2) |
| 80 | + ); |
| 81 | + assert!( |
| 82 | + ((*(*(*(*obj.with(Value::clone).borrow()).p.borrow()) |
| 83 | + .upgrade() |
| 84 | + .deref()) |
| 85 | + .value |
| 86 | + .borrow()) |
| 87 | + == 42) |
| 88 | + ); |
| 89 | + thread_local!( |
| 90 | + static cache: Value<Box<[Ptr<Inner>]>> = Rc::new(RefCell::new(Box::new([ |
| 91 | + (alpha.with(Value::clone).as_pointer()), |
| 92 | + (beta.with(Value::clone).as_pointer()), |
| 93 | + ]))); |
| 94 | + ); |
| 95 | + assert!( |
| 96 | + ((*(*(*cache.with(Value::clone).borrow())[(0) as usize] |
| 97 | + .upgrade() |
| 98 | + .deref()) |
| 99 | + .value |
| 100 | + .borrow()) |
| 101 | + == 1) |
| 102 | + ); |
| 103 | + assert!( |
| 104 | + ((*(*(*cache.with(Value::clone).borrow())[(1) as usize] |
| 105 | + .upgrade() |
| 106 | + .deref()) |
| 107 | + .value |
| 108 | + .borrow()) |
| 109 | + == 2) |
| 110 | + ); |
| 111 | + return 0; |
| 112 | +} |
0 commit comments