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 Component : Any + Debug + 'static {
- fn upcast_Any_ref (self: &'_ Self)
- -> &'_ dyn Any
- ;
- fn upcast_Any_mut (self: &'_ mut Self)
- -> &'_ mut dyn Any
- ;
- fn upcast_Any_box (self: Box<Self>)
- -> Box<dyn Any>
- ;
- }
- impl<T : Any + Debug + 'static> Component for T {
- #[inline]
- fn upcast_Any_ref (self: &'_ Self)
- -> &'_ dyn Any
- {
- self
- }
- #[inline]
- fn upcast_Any_mut (self: &'_ mut Self)
- -> &'_ mut dyn Any
- {
- self
- }
- #[inline]
- fn upcast_Any_box (self: Box<Self>)
- -> Box<dyn Any>
- {
- self
- }
- }
- impl dyn Component + 'static {
- #[inline]
- fn downcast_ref<T : 'static> (self: &'_ Self)
- -> Option<&'_ T>
- {
- self.upcast_Any_ref().downcast_ref::<T>()
- }
- #[inline]
- fn downcast_mut<T : 'static> (self: &'_ mut Self)
- -> Option<&'_ mut T>
- {
- self.upcast_Any_mut().downcast_mut::<T>()
- }
- }
- #[derive(Debug)]
- struct Foo {
- bar: u8
- }
- fn main ()
- {
- let foo: Box<dyn Component> = Box::new(Foo { bar: 0 });
- let x = foo.downcast_ref::<Foo>();
- println!("{:?}", foo);
- println!("{:?}", x);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement