SHARE
TWEET

Untitled

a guest Jul 23rd, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn make_wrapper<F, Arg>(f: F) -> Box<dyn Fn(&i32)>
  2. where
  3.     Arg: for<'a> CreateFromI32Ref<'a>,
  4.     F: Fn(Arg) + 'static
  5. {
  6.     Box::new(move |i32_ref: &i32| {
  7.         f(Arg::create_from_i32_ref(i32_ref))
  8.     })
  9. }
  10.  
  11. trait CreateFromI32Ref<'a>: 'a {
  12.     fn create_from_i32_ref(i32_ref: &'a i32) -> Self;
  13. }
  14.  
  15. impl<'a> CreateFromI32Ref<'a> for i32 {
  16.     fn create_from_i32_ref(i32_ref: &'a i32) -> i32 {
  17.         *i32_ref
  18.     }
  19. }
  20.  
  21. impl<'a: 'b, 'b: 'a> CreateFromI32Ref<'a> for &'b i32 {
  22.     fn create_from_i32_ref(i32_ref: &'a i32) -> &'b i32 {
  23.         i32_ref
  24.     }
  25. }
  26.  
  27. fn main() {
  28.     fn wrappee(arg: i32) { println!("{}", arg) }
  29.     make_wrapper(wrappee)(&100i32);
  30.    
  31.     fn wrappee2(arg: &i32) { println!("{}", *arg) }
  32.     make_wrapper(wrappee2)(&200i32);
  33. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top