Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(bad_style, unused)]#![warn(unused_must_use)]
- use ::core::{any::Any, fmt::Debug};
- trait MyAny : Any + 'static {
- /// trait method (!= an inherent method of dyn Any)
- fn downcast_ref_Foo (self: &'_ Self)
- -> Option<&'_ Foo>
- ;
- }
- impl MyAny for Foo
- {
- fn downcast_ref_Foo (self: &'_ Self)
- -> Option<&'_ Foo>
- {
- Some(self)
- }
- }
- #[derive(Debug)]
- struct Foo {
- bar: u8
- }
- trait Component : MyAny + Debug {}
- impl Component for Foo {}
- fn main ()
- {
- let foo: Box<dyn Component> = Box::new(Foo { bar: 0 });
- let x: Option<&Foo> = foo.downcast_ref_Foo();
- println!("{:?}", foo);
- println!("{:?}", x);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement