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
//! macros for implementing traitcast for widgets
use super::*;

/// should match the non-stabilized std::raw::TraitObject and represents a erased fat pointer
#[repr(C)]
#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct TraitObject {
    pub data: *mut (),
    pub vtable: *mut (),
}

/// This macro is used inside Widget/WidgetMut impls
/// 
/// Example:
/// impl_traitcast!(
///     dyn IButton => |s| s;
///     dyn IButtonState => |s| &s.state;
/// );
#[macro_export]
macro_rules! impl_traitcast {
    ($( $trait:ty => |$id:pat| $access:expr; )*) => {
        unsafe fn _as_trait_ref(&self, t: std::any::TypeId) -> Option<$crate::util::traitcast::TraitObject> {
            $(
                if t == <$trait as $crate::widget::cast::Statize>::_typeid() {
                    let $id = self;
                    let senf: &$trait = $access;
                    let senf = std::mem::transmute::<&$trait,$crate::util::traitcast::TraitObject>(senf);
                    return Some(senf);
                }
            );*
            None
        }
    }
}

/// This macro is used inside WidgetMut impls
/// 
/// Example:
/// impl_traitcast_mut!(
///     dyn IButton => |s| s;
///     dyn IButtonState => |s| &mut s.state;
/// );
#[macro_export]
macro_rules! impl_traitcast_mut {
    ($( $trait:ty => |$id:pat| $access:expr; )*) => {
        unsafe fn _as_trait_mut(&mut self, t: std::any::TypeId) -> Option<$crate::util::traitcast::TraitObject> {
            $(
                if t == <$trait as $crate::widget::cast::Statize>::_typeid() {
                    let $id = self;
                    let senf: &mut $trait = $access;
                    let senf = std::mem::transmute::<&mut $trait,$crate::util::traitcast::TraitObject>(senf);
                    return Some(senf);
                }
            );*
            None
        }
    }
}

macro_rules! impl_statize_lte {
    ($trait:ident) => {
        unsafe impl<'w,E> Statize for dyn $trait<'w,E> where E: Env {
            type Statur = dyn $trait<'static,E>;
        }
        unsafe impl<'l,'s,E> Statize for &'s dyn $trait<'l,E> where E: Env, 'l: 's {
            type Statur = &'static dyn $trait<'static,E>;
        }
        unsafe impl<'l,'s,E> Statize for &'s mut dyn $trait<'l,E> where E: Env, 'l: 's {
            type Statur = &'static mut dyn $trait<'static,E>;
        }
    };
}