Guest User

Untitled

a guest
Oct 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. use std::any::Any;
  2.  
  3. fn test1<T: 'static>(f: fn(&mut T)) -> Box<Any + 'static> {
  4. Box::new(f)
  5. }
  6.  
  7. struct Foo<T>(Box<dyn for<'a> Fn(&'a mut T) + 'static>);
  8.  
  9. fn test2<T: 'static, F: Fn(&mut T) + 'static>(f: F) -> Box<Any + 'static> {
  10. Box::new(Foo(Box::new(f)))
  11. }
  12.  
  13.  
  14.  
  15. fn thing(i: &mut i32) {
  16. println!("{}", i);
  17. }
  18.  
  19. fn main() {
  20. let x = test1(thing);
  21. let y = test2(thing);
  22.  
  23. let a = x.downcast_ref::<fn(&mut i32)>().expect("cast x");
  24. a(&mut 3);
  25.  
  26. let b = y.downcast_ref::<Foo<i32>>().expect("cast y");
  27. (b.0)(&mut 3);
  28. }
Add Comment
Please, Sign In to add comment