Advertisement
Guest User

Untitled

a guest
May 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. use std::rc::Rc;
  2. use std::cell::{RefCell, RefMut};
  3. use std::collections::HashMap;
  4.  
  5. struct GE {
  6. env: std::collections::HashMap<String, Rc<RefCell<FnMut() -> i32>>>,
  7. }
  8.  
  9. struct HasDrop { i: i32 }
  10.  
  11. impl Drop for HasDrop {
  12. fn drop(&mut self) {
  13. println!("Dropping!");
  14. }
  15. }
  16.  
  17. impl GE {
  18. fn add<T>(&mut self, funcname: &str, func: T) where T: 'static + FnMut() -> i32 {
  19. self.env.insert(String::from(funcname), Rc::new(RefCell::new(func)));
  20. }
  21. fn call(&mut self, funcname: &str) -> i32 {
  22. //let mut ret = 0;
  23. let f = Rc::get_mut(self.env.get_mut(funcname).unwrap()).unwrap().get_mut();
  24. //|fx| { ret = fx(); fx });
  25. //ret
  26. f()
  27. }
  28. }
  29.  
  30. fn o() {
  31. let mut g = GE { env: HashMap::new() };
  32. let fo = HasDrop { i: 20 };
  33. g.add("foo2", move || { fo.i });
  34. println!("i1: {}", g.call("foo2"));
  35. println!("i2: {}", g.call("foo2"));
  36. }
  37.  
  38. fn main() {
  39. println!("A");
  40. o();
  41. println!("B");
  42. o();
  43. println!("C");
  44. o();
  45. println!("D");
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement