Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::rc::Rc;
- use std::cell::{RefCell, RefMut};
- use std::collections::HashMap;
- struct GE {
- env: std::collections::HashMap<String, Rc<RefCell<FnMut() -> i32>>>,
- }
- struct HasDrop { i: i32 }
- impl Drop for HasDrop {
- fn drop(&mut self) {
- println!("Dropping!");
- }
- }
- impl GE {
- fn add<T>(&mut self, funcname: &str, func: T) where T: 'static + FnMut() -> i32 {
- self.env.insert(String::from(funcname), Rc::new(RefCell::new(func)));
- }
- fn call(&mut self, funcname: &str) -> i32 {
- //let mut ret = 0;
- let f = Rc::get_mut(self.env.get_mut(funcname).unwrap()).unwrap().get_mut();
- //|fx| { ret = fx(); fx });
- //ret
- f()
- }
- }
- fn o() {
- let mut g = GE { env: HashMap::new() };
- let fo = HasDrop { i: 20 };
- g.add("foo2", move || { fo.i });
- println!("i1: {}", g.call("foo2"));
- println!("i2: {}", g.call("foo2"));
- }
- fn main() {
- println!("A");
- o();
- println!("B");
- o();
- println!("C");
- o();
- println!("D");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement