gobject_subclass/
anyimpl.rs1use std::any::{Any, TypeId};
12
13pub trait AnyImpl: Any {
14 fn get_type_id(&self) -> TypeId;
15}
16
17impl<T: Any> AnyImpl for T {
18 fn get_type_id(&self) -> TypeId {
19 TypeId::of::<T>()
20 }
21}
22
23#[macro_export]
26macro_rules! any_impl {
27
28 ($bound:ident, $trait:ident $(, $constraint:ident)*) => {
29 impl<T: $bound> $trait<T>
30 $(
31 where T::InstanceStructType: $constraint
32 )*
33 {
34 #[inline]
35 pub fn downcast_ref<U: $trait<T>>(&self) -> Option<&U> {
36 if self.is::<U>() {
37 unsafe { Some(self.downcast_ref_unchecked()) }
38 } else {
39 None
40 }
41 }
42
43 #[inline]
44 pub unsafe fn downcast_ref_unchecked<U: $trait<T>>(&self) -> &U {
45 &*(self as *const Self as *const U)
46 }
47
48 #[inline]
49 pub fn is<U: $trait<T>>(&self) -> bool {
50 use std::any::TypeId;
51 TypeId::of::<U>() == $crate::anyimpl::AnyImpl::get_type_id(self)
52 }
53 }
54 };
55
56 ($trait:ident) => {
57 impl $trait {
58 #[inline]
59 pub fn downcast_ref<U: $trait>(&self) -> Option<&U> {
60 if self.is::<U>() {
61 unsafe { Some(self.downcast_ref_unchecked()) }
62 } else {
63 None
64 }
65 }
66
67 #[inline]
68 pub unsafe fn downcast_ref_unchecked<U: $trait>(&self) -> &U {
69 &*(self as *const Self as *const U)
70 }
71
72 #[inline]
73 pub fn is<U: $trait>(&self) -> bool {
74 use std::any::TypeId;
75 TypeId::of::<U>() == $crate::anyimpl::AnyImpl::get_type_id(self)
76 }
77 }
78 };
79}