gobject_subclass/
anyimpl.rs

1// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8//
9// Heavily inspired by the mopa trait
10
11use 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// warning: constraints is defined as a repetition to minimize code duplication.
24// multiple items will generate invalide code.
25#[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}