SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
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