SHARE
TWEET

Untitled

a guest Jun 20th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![deny(bare_trait_objects, elided_lifetimes_in_paths)]
  2.  
  3. use ::std::*;
  4.  
  5. struct Callback /* = */ (
  6.     Box<dyn FnMut() + 'static>,
  7. );
  8.  
  9. impl Callback {
  10.     #[inline]
  11.     fn new (callback: impl FnMut() + 'static) -> Self
  12.     {
  13.         Self(Box::new(callback))
  14.     }
  15.    
  16.     #[allow(non_snake_case)]
  17.     #[inline]
  18.     fn as_FnMut (self: &'_ mut Self) -> &'_ mut (dyn FnMut() + 'static)
  19.     {
  20.         ops::DerefMut::deref_mut(self)
  21.     }
  22. }
  23.  
  24. impl ops::Deref for Callback {
  25.     type Target = dyn FnMut() + 'static;
  26.    
  27.     #[inline]
  28.     fn deref (self: &'_ Self) -> &'_ Self::Target
  29.     {
  30.         &*self.0
  31.     }
  32. }
  33.  
  34. impl ops::DerefMut for Callback {
  35.     #[inline]
  36.     fn deref_mut (self: &'_ mut Self) -> &'_ mut Self::Target
  37.     {
  38.         &mut *self.0
  39.     }
  40. }
  41.  
  42. fn test (callback: &'_ mut Callback)
  43. {
  44.     let callback = callback.as_FnMut();
  45.    
  46.     callback();
  47.     callback();
  48.     callback();
  49.     (&mut *callback)();
  50. }
  51.  
  52. fn main ()
  53. {
  54.     let mut callback = {
  55.         let mut cnt = 0;
  56.         Callback::new(move || {
  57.             cnt += 1;
  58.             println!("Test {}", cnt);
  59.         })
  60.     };
  61.     test(&mut callback);
  62. }
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