Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![deny(bare_trait_objects, elided_lifetimes_in_paths)]
- use ::std::*;
- struct Callback /* = */ (
- Box<dyn FnMut() + 'static>,
- );
- impl Callback {
- #[inline]
- fn new (callback: impl FnMut() + 'static) -> Self
- {
- Self(Box::new(callback))
- }
- #[allow(non_snake_case)]
- #[inline]
- fn as_FnMut (self: &'_ mut Self) -> &'_ mut (dyn FnMut() + 'static)
- {
- ops::DerefMut::deref_mut(self)
- }
- }
- impl ops::Deref for Callback {
- type Target = dyn FnMut() + 'static;
- #[inline]
- fn deref (self: &'_ Self) -> &'_ Self::Target
- {
- &*self.0
- }
- }
- impl ops::DerefMut for Callback {
- #[inline]
- fn deref_mut (self: &'_ mut Self) -> &'_ mut Self::Target
- {
- &mut *self.0
- }
- }
- fn test (callback: &'_ mut Callback)
- {
- let callback = callback.as_FnMut();
- callback();
- callback();
- callback();
- (&mut *callback)();
- }
- fn main ()
- {
- let mut callback = {
- let mut cnt = 0;
- Callback::new(move || {
- cnt += 1;
- println!("Test {}", cnt);
- })
- };
- test(&mut callback);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement