Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.35 KB | None | 0 0
  1. use std::cell::RefCell;
  2. use std::rc::Rc;
  3. use std::sync::RwLock;
  4. use std::thread;
  5. use std::sync::Arc;
  6. use std::sync::Mutex;
  7.  
  8. struct Graph {
  9.     node_0: Rc<RefCell<Node0>>,
  10.     node_1: Rc<RefCell<Node1>>
  11. }
  12. unsafe impl Send for Graph {}
  13. unsafe impl Sync for Graph {}
  14.  
  15. #[derive(Debug)]
  16. struct Node0 {
  17.     outgoing_ports: Vec<Rc<RefCell<OutgoingPort>>>
  18. }
  19.  
  20. #[derive(Debug)]
  21. struct Node1 {
  22.     incoming_port: Rc<RefCell<IncomingPort>>
  23. }
  24.  
  25. #[derive(Debug)]
  26. struct OutgoingPort {
  27.     destinations: Vec<Rc<RefCell<IncomingPort>>>
  28. }
  29.  
  30. #[derive(Debug)]
  31. struct IncomingPort {
  32.     sources: Vec<Rc<RefCell<OutgoingPort>>>
  33. }
  34.  
  35. fn main() {
  36.     println!("Hello, world!");
  37.  
  38.     let p0 = Rc::new(RefCell::new(OutgoingPort {destinations: vec!()}));
  39.     let n0 = Rc::new(RefCell::new(Node0 { outgoing_ports: vec!(p0.clone()) }));
  40.     let p1 = Rc::new(RefCell::new(IncomingPort { sources: vec!()}));
  41.     let n1 = Rc::new(RefCell::new(Node1 { incoming_port: p1.clone()} ));
  42.  
  43.     {
  44.         let mut p0_mut = p0.borrow_mut();
  45.         p0_mut.destinations.push(p1.clone());
  46.     }
  47.     {
  48.         let mut p1_mut = p1.borrow_mut();
  49.         p1_mut.sources.push(p0.clone())
  50.     }
  51.  
  52.     let lock = Arc::new(RwLock::new(Graph {node_0: n0, node_1: n1}));
  53.  
  54.     thread::spawn(move || {
  55.         do_stuff(lock.clone());
  56.     });
  57. }
  58.  
  59. fn do_stuff(g: Arc<RwLock<Graph>>) {
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement