gsk4/
render_node.rs

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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::translate::*;

use crate::{ffi, prelude::*, ParseLocation, RenderNode, RenderNodeType};

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

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

    /// Loads data previously created via [`serialize()`][Self::serialize()].
    ///
    /// For a discussion of the supported format, see that function.
    /// ## `bytes`
    /// the bytes containing the data
    /// ## `error_func`
    /// callback on parsing errors
    ///
    /// # Returns
    ///
    /// a new render node
    #[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 mut 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 = user_data as *mut P;
            (*callback)(&start, &end, &error);
        }
        let error_func = Some(error_func_func::<P> as _);
        let super_callback0: &mut P = &mut error_func_data;
        unsafe {
            from_glib_full(ffi::gsk_render_node_deserialize(
                bytes.to_glib_none().0,
                error_func,
                super_callback0 as *mut _ as *mut _,
            ))
        }
    }

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

    #[inline]
    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 {
    #[inline]
    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 {
            #[inline]
            fn as_ref(&self) -> &crate::RenderNode {
                self
            }
        }

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

            #[inline]
            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;

            #[inline]
            fn upcast(self) -> crate::RenderNode {
                unsafe {
                    glib::translate::from_glib_full(
                        glib::translate::IntoGlibPtr::<*mut $ffi_type>::into_glib_ptr(self)
                            as *mut crate::ffi::GskRenderNode,
                    )
                }
            }

            #[inline]
            fn upcast_ref(&self) -> &crate::RenderNode {
                self
            }
        }

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

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

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

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

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

            #[inline]
            fn value_type(&self) -> glib::Type {
                use glib::prelude::StaticType;
                Self::static_type()
            }
        }

        #[cfg(feature = "v4_6")]
        #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
        impl glib::value::ToValueOptional for $rust_type {
            #[inline]
            fn to_value_optional(s: Option<&Self>) -> glib::Value {
                skip_assert_initialized!();
                let mut value = glib::Value::for_value_type::<Self>();
                unsafe {
                    crate::ffi::gsk_value_set_render_node(
                        glib::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
                        s.map(|s| s.as_ptr()).unwrap_or(std::ptr::null_mut()) as *mut _,
                    )
                }
                value
            }
        }
    };
}