Guest User

Untitled

a guest
Jul 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. //#![warn(bare_trait_objects)]
  2.  
  3. pub trait Dynamic {
  4. fn aaa(&mut self);
  5. }
  6.  
  7. pub trait Callback {
  8. fn id(&self) -> &'static str;
  9. }
  10.  
  11. #[macro_export]
  12. macro_rules! callback {
  13. ($id: ident, $($typ:tt)+) => {
  14. pub struct $id(Box<$($typ)+>);
  15.  
  16. impl Callback for $id {
  17. fn id(&self) -> &'static str {
  18. stringify!($id)
  19. }
  20. }
  21.  
  22. impl <T> From<T> for $id where T: $($typ)+ + Sized + 'static {
  23. fn from(t: T) -> $id {
  24. $id(Box::new(t))
  25. }
  26. }
  27. impl AsRef<$($typ)+> for $id {
  28. //fn as_ref(&self) -> & ($($typ)+ + 'static) { //this compiles (no dyn)
  29. fn as_ref(&self) -> &dyn ($($typ)+ + 'static) { //this fails (dyn)
  30. self.0.as_ref()
  31. }
  32. }
  33. impl AsMut<$($typ)+> for $id {
  34. //fn as_mut(&mut self) -> &mut ($($typ)+ + 'static) { //same here
  35. fn as_mut(&mut self) -> &mut dyn ($($typ)+ + 'static) {
  36. self.0.as_mut()
  37. }
  38. }
  39. }
  40. }
  41.  
  42. callback!(Resize, FnMut(&mut dyn Dynamic, u16, u16));
  43. callback!(Click, FnMut(&mut dyn Dynamic));
  44.  
  45. fn main() {}
Add Comment
Please, Sign In to add comment