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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{ParseLocation, RenderNode, RenderNodeType};
use glib::translate::*;
use glib::StaticType;

impl RenderNode {
    pub fn is<T: IsRenderNode>(&self) -> bool {
        T::NODE_TYPE == self.node_type()
    }

    pub fn type_(&self) -> glib::Type {
        unsafe {
            let ptr = self.to_glib_none().0;
            from_glib((*(*(ptr as *mut glib::gobject_ffi::GTypeInstance)).g_class).g_type)
        }
    }

    #[doc(alias = "gsk_render_node_deserialize")]
    pub fn deserialize(bytes: &glib::Bytes) -> Option<Self> {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::gsk_render_node_deserialize(
                bytes.to_glib_none().0,
                None,
                std::ptr::null_mut(),
            ))
        }
    }

    #[doc(alias = "gsk_render_node_deserialize")]
    pub fn deserialize_with_error_func<P: FnMut(&ParseLocation, &ParseLocation, &glib::Error)>(
        bytes: &glib::Bytes,
        error_func: P,
    ) -> Option<Self> {
        assert_initialized_main_thread!();
        let error_func_data: P = error_func;
        unsafe extern "C" fn error_func_func<
            P: FnMut(&ParseLocation, &ParseLocation, &glib::Error),
        >(
            start: *const ffi::GskParseLocation,
            end: *const ffi::GskParseLocation,
            error: *const glib::ffi::GError,
            user_data: glib::ffi::gpointer,
        ) {
            let start = from_glib_borrow(start);
            let end = from_glib_borrow(end);
            let error = from_glib_borrow(error);
            let callback: *mut P = user_data as *const _ as usize as *mut P;
            (*callback)(&start, &end, &error);
        }
        let error_func = Some(error_func_func::<P> as _);
        let super_callback0: &P = &error_func_data;
        unsafe {
            from_glib_full(ffi::gsk_render_node_deserialize(
                bytes.to_glib_none().0,
                error_func,
                super_callback0 as *const _ as usize as *mut _,
            ))
        }
    }

    pub fn downcast<T: IsRenderNode>(self) -> Result<T, Self> {
        unsafe {
            if self.is::<T>() {
                Ok(from_glib_full(self.to_glib_full()))
            } else {
                Err(self)
            }
        }
    }

    pub fn downcast_ref<T: IsRenderNode>(&self) -> Option<&T> {
        unsafe {
            if self.is::<T>() {
                Some(&*(self as *const RenderNode as *const T))
            } else {
                None
            }
        }
    }
}

impl std::fmt::Debug for RenderNode {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("RenderNode")
            .field("bounds", &self.bounds())
            .field("node_type", &self.node_type())
            .finish()
    }
}

// rustdoc-stripper-ignore-next
/// A common trait implemented by the various [`RenderNode`](crate::RenderNode) types.
///
/// # Safety
///
/// The user is not supposed to implement this trait.
pub unsafe trait IsRenderNode:
    StaticType
    + FromGlibPtrFull<*mut ffi::GskRenderNode>
    + std::convert::AsRef<crate::RenderNode>
    + 'static
{
    const NODE_TYPE: RenderNodeType;
    fn upcast(self) -> RenderNode;
    fn upcast_ref(&self) -> &RenderNode;
}

#[doc(hidden)]
impl AsRef<RenderNode> for RenderNode {
    fn as_ref(&self) -> &Self {
        self
    }
}

macro_rules! define_render_node {
    ($rust_type:ident, $ffi_type:path,  $node_type:path) => {
        impl std::convert::AsRef<crate::RenderNode> for $rust_type {
            fn as_ref(&self) -> &crate::RenderNode {
                &*self
            }
        }

        impl std::ops::Deref for $rust_type {
            type Target = crate::RenderNode;

            fn deref(&self) -> &Self::Target {
                unsafe { &*(self as *const $rust_type as *const crate::RenderNode) }
            }
        }

        unsafe impl crate::render_node::IsRenderNode for $rust_type {
            const NODE_TYPE: RenderNodeType = $node_type;

            fn upcast(self) -> crate::RenderNode {
                unsafe {
                    glib::translate::from_glib_full(
                        glib::translate::ToGlibPtr::to_glib_full(&self) as *mut ffi::GskRenderNode
                    )
                }
            }

            fn upcast_ref(&self) -> &crate::RenderNode {
                &*self
            }
        }

        #[doc(hidden)]
        impl glib::translate::FromGlibPtrFull<*mut ffi::GskRenderNode> for $rust_type {
            unsafe fn from_glib_full(ptr: *mut ffi::GskRenderNode) -> Self {
                glib::translate::from_glib_full(ptr as *mut $ffi_type)
            }
        }

        #[cfg(any(feature = "v4_6", feature = "dox"))]
        #[cfg_attr(feature = "dox", doc(cfg(feature = "v4_6")))]
        impl glib::value::ValueType for $rust_type {
            type Type = Self;
        }

        #[cfg(any(feature = "v4_6", feature = "dox"))]
        #[cfg_attr(feature = "dox", doc(cfg(feature = "v4_6")))]
        unsafe impl<'a> glib::value::FromValue<'a> for $rust_type {
            type Checker = glib::value::GenericValueTypeOrNoneChecker<Self>;

            unsafe fn from_value(value: &'a glib::Value) -> Self {
                skip_assert_initialized!();
                glib::translate::from_glib_full(ffi::gsk_value_dup_render_node(
                    glib::translate::ToGlibPtr::to_glib_none(value).0,
                ))
            }
        }

        #[cfg(any(feature = "v4_6", feature = "dox"))]
        #[cfg_attr(feature = "dox", doc(cfg(feature = "v4_6")))]
        impl glib::value::ToValue for $rust_type {
            fn to_value(&self) -> glib::Value {
                let mut value = glib::Value::for_value_type::<Self>();
                unsafe {
                    ffi::gsk_value_set_render_node(
                        glib::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
                        glib::translate::ToGlibPtr::to_glib_none(self).0 as *mut _,
                    )
                }
                value
            }

            fn value_type(&self) -> glib::Type {
                use glib::StaticType;
                Self::static_type()
            }
        }

        #[cfg(any(feature = "v4_6", feature = "dox"))]
        #[cfg_attr(feature = "dox", doc(cfg(feature = "v4_6")))]
        impl glib::value::ToValueOptional for $rust_type {
            fn to_value_optional(s: Option<&Self>) -> glib::Value {
                skip_assert_initialized!();
                let mut value = glib::Value::for_value_type::<Self>();
                unsafe {
                    ffi::gsk_value_set_render_node(
                        glib::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
                        glib::translate::ToGlibPtr::to_glib_none(&s).0 as *mut _,
                    )
                }
                value
            }
        }
    };
}