Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. #![allow(bad_style, unused)]#![warn(unused_must_use)]
  2.  
  3. use ::core::{any::Any, fmt::Debug};
  4.  
  5. trait MyAny : Any + 'static {
  6. /// trait method (!= an inherent method of dyn Any)
  7. fn downcast_ref_Foo (self: &'_ Self)
  8. -> Option<&'_ Foo>
  9. ;
  10. }
  11. impl MyAny for Foo
  12. {
  13. fn downcast_ref_Foo (self: &'_ Self)
  14. -> Option<&'_ Foo>
  15. {
  16. Some(self)
  17. }
  18. }
  19.  
  20. #[derive(Debug)]
  21. struct Foo {
  22. bar: u8
  23. }
  24.  
  25. trait Component : MyAny + Debug {}
  26.  
  27. impl Component for Foo {}
  28.  
  29. fn main ()
  30. {
  31. let foo: Box<dyn Component> = Box::new(Foo { bar: 0 });
  32. let x: Option<&Foo> = foo.downcast_ref_Foo();
  33.  
  34. println!("{:?}", foo);
  35. println!("{:?}", x);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement