Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::any::Any;
- fn test1<T: 'static>(f: fn(&mut T)) -> Box<Any + 'static> {
- Box::new(f)
- }
- struct Foo<T>(Box<dyn for<'a> Fn(&'a mut T) + 'static>);
- fn test2<T: 'static, F: Fn(&mut T) + 'static>(f: F) -> Box<Any + 'static> {
- Box::new(Foo(Box::new(f)))
- }
- fn thing(i: &mut i32) {
- println!("{}", i);
- }
- fn main() {
- let x = test1(thing);
- let y = test2(thing);
- let a = x.downcast_ref::<fn(&mut i32)>().expect("cast x");
- a(&mut 3);
- let b = y.downcast_ref::<Foo<i32>>().expect("cast y");
- (b.0)(&mut 3);
- }
Add Comment
Please, Sign In to add comment