1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#![feature(get_type_id)]

use std::any::{Any, TypeId};
use std::mem;

pub trait QDowncastable<T: Any>: Any {
    fn is_type(&self) -> bool;
    fn downcast_ref(&self) -> Option<&T>;
    fn downcast_mut(&mut self) -> Option<&mut T>;    
    fn downcast_boxed(self: Box<Self>) -> Result<Box<T>, Box<Self>>;
}
    
#[doc(hidden)]
#[inline]
pub fn get_type_id<T: ?Sized + Any>(obj: &T) -> TypeId {
    obj.get_type_id()
}

#[doc(hidden)]
pub struct TraitObject {
    pub data: *mut (),
    pub vtable: *mut (),
}

/// Use this macro if `qdowncastable!` can't generate the full statement for your
/// type (due to assoc-types or type-params).  
#[macro_export]
macro_rules! qdowncastable_items {
    ($t_param:ty) => {
        fn is_type(&self) -> bool {
            use std::any::TypeId;
            use qdowncast;

            qdowncast::get_type_id(self) == TypeId::of::<$t_param>()
        }
        
        fn downcast_ref(&self) -> Option<&$t_param> {
            use qdowncast::{QDowncastable, TraitObject};
            use std::mem;

            if QDowncastable::<$t_param>::is_type(self) {
                unsafe {
                    let obj: TraitObject = mem::transmute(self);
                    Some(mem::transmute(obj.data))
                }
            } else {
                None
            }
        }
        
        fn downcast_mut(&mut self) -> Option<&mut $t_param> {
            use qdowncast::{QDowncastable, TraitObject};
            use std::mem;

            if QDowncastable::<$t_param>::is_type(self) {
                unsafe {
                    let obj: TraitObject = mem::transmute(self);
                    Some(mem::transmute(obj.data))
                }
            } else {
                None
            }
        }

        fn downcast_boxed(self: Box<Self>) -> Result<Box<$t_param>, Box<Self>> {
            use qdowncast::{QDowncastable, TraitObject};
            use std::mem;

            if QDowncastable::<$t_param>::is_type(&*self) {
                unsafe {
                    let obj: TraitObject = mem::transmute(self);
                    Ok(mem::transmute(obj.data))
                }
            } else {
                Err(self)            
            }
        }
    };
}

#[macro_export]
macro_rules! qdowncastable {
    ($base:ident) => {
        impl<__T> ::qdowncast::QDowncastable<__T> for $base 
            where __T: ::std::any::Any
        {
            qdowncastable_items!(__T);
        }
    };
}


/// Use this macro if `qdowncast_methods!` can't generate the full statement for your
/// type (due to assoc-types or type-params).  
#[macro_export]
macro_rules! qdowncast_method_items {
    () => {
        pub fn is<__T>(&self) -> bool 
            where __T: ::std::any::Any, Self: ::qdowncast::QDowncastable<__T>
        {
            ::qdowncast::QDowncastable::<__T>::is_type(self)
        }

        pub fn downcast_ref<__T>(&self) -> Option<&__T> 
            where __T: ::std::any::Any, Self: ::qdowncast::QDowncastable<__T>
        {
            ::qdowncast::QDowncastable::<__T>::downcast_ref(self)
        }

        pub fn downcast_mut<__T>(&mut self) -> Option<&mut __T> 
            where __T: ::std::any::Any, Self: ::qdowncast::QDowncastable<__T>
        {
            ::qdowncast::QDowncastable::<__T>::downcast_mut(self)
        }

        pub fn downcast_boxed<__T>(self: Box<Self>) ->  Result<Box<__T>, Box<Self>>
            where __T: ::std::any::Any, Self: ::qdowncast::QDowncastable<__T>
        {
            ::qdowncast::QDowncastable::<__T>::downcast_boxed(self)
        }
    };
}


/// Use this macro if you want to provide `downcast`-methods on your type without 
/// requiring your users to import `QDowncastable`.
#[macro_export]
macro_rules! qdowncast_methods {
    ($ty:ty) => {
        impl $ty {
            qdowncast_method_items!();
        }
    };
}