pango/
attribute.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
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::translate::*;

use crate::{ffi, AttrClass, AttrType, Attribute};

impl Attribute {
    #[doc(alias = "get_attr_class")]
    #[inline]
    pub fn attr_class(&self) -> AttrClass {
        unsafe { from_glib_none((*self.as_ptr()).klass) }
    }

    #[inline]
    pub fn type_(&self) -> AttrType {
        unsafe { from_glib((*(*self.as_ptr()).klass).type_) }
    }

    #[doc(alias = "get_start_index")]
    #[inline]
    pub fn start_index(&self) -> u32 {
        unsafe { (*self.as_ptr()).start_index }
    }

    #[doc(alias = "get_end_index")]
    #[inline]
    pub fn end_index(&self) -> u32 {
        unsafe { (*self.as_ptr()).end_index }
    }

    #[inline]
    pub fn set_start_index(&mut self, index: u32) {
        unsafe {
            (*self.as_ptr()).start_index = index;
        }
    }

    #[inline]
    pub fn set_end_index(&mut self, index: u32) {
        unsafe {
            (*self.as_ptr()).end_index = index;
        }
    }

    #[inline]
    pub fn downcast<T: IsAttribute>(self) -> Result<T, Attribute> {
        unsafe {
            if T::ATTR_TYPES.contains(&self.attr_class().type_()) {
                Ok(from_glib_full(glib::translate::IntoGlibPtr::<
                    *mut ffi::PangoAttribute,
                >::into_glib_ptr(self)))
            } else {
                Err(self)
            }
        }
    }

    #[inline]
    pub fn downcast_ref<T: IsAttribute>(&self) -> Option<&T> {
        unsafe {
            if T::ATTR_TYPES.contains(&self.attr_class().type_()) {
                Some(&*(self as *const Attribute as *const T))
            } else {
                None
            }
        }
    }
}

#[allow(clippy::missing_safety_doc)]
pub unsafe trait IsAttribute:
    FromGlibPtrFull<*const ffi::PangoAttribute>
    + FromGlibPtrFull<*mut ffi::PangoAttribute>
    + std::convert::AsRef<crate::Attribute>
    + 'static
{
    const ATTR_TYPES: &'static [AttrType];
    fn upcast(self) -> Attribute;
    fn upcast_ref(&self) -> &Attribute;
}

macro_rules! define_attribute_struct {
    ($rust_type:ident, $ffi_type:path, $attr_types:expr) => {

        #[cfg(feature = "v1_44")]
        glib::wrapper! {
            #[derive(Debug)]
            pub struct $rust_type(Boxed<$ffi_type>);

            match fn {
                copy => |ptr| ffi::pango_attribute_copy(ptr as *const ffi::PangoAttribute) as *mut $ffi_type,
                free => |ptr| ffi::pango_attribute_destroy(ptr as *mut ffi::PangoAttribute),
                type_ => || ffi::pango_attribute_get_type(),
            }
        }

        unsafe impl Send for $rust_type {}
        unsafe impl Sync for $rust_type {}

        #[cfg(not(feature = "v1_44"))]
        glib::wrapper! {
            #[derive(Debug)]
            pub struct $rust_type(Boxed<$ffi_type>);

            match fn {
                copy => |ptr| ffi::pango_attribute_copy(ptr as *const ffi::PangoAttribute) as *mut $ffi_type,
                free => |ptr| ffi::pango_attribute_destroy(ptr as *mut ffi::PangoAttribute),
            }
        }

        impl $rust_type {
            #[doc(alias = "pango_attribute_equal")]
            fn equal<T:  crate::attribute::IsAttribute>(&self, attr2: &T) -> bool {
                unsafe {
                    glib::translate::from_glib(ffi::pango_attribute_equal(self.as_ptr() as *const ffi::PangoAttribute,
                       glib::translate::ToGlibPtr::to_glib_none(attr2.upcast_ref()).0,
                    ))
                }
            }
        }

        impl PartialEq for $rust_type {
            #[inline]
            fn eq(&self, other: &Self) -> bool {
                self.equal(other)
            }
        }

        impl Eq for $rust_type {}

        unsafe impl crate::attribute::IsAttribute for $rust_type {
            const ATTR_TYPES: &'static [crate::AttrType] = $attr_types;

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

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

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

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

        impl std::convert::AsRef<crate::Attribute> for $rust_type {
            #[inline]
            fn as_ref(&self) -> &crate::Attribute {
                &*self
            }
        }

        impl From<$rust_type> for crate::Attribute {
            #[inline]
            fn from(attr: $rust_type) -> crate::Attribute {
                crate::IsAttribute::upcast(attr)
            }
        }

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

            #[inline]
            fn deref(&self) -> &Self::Target {
                unsafe { &*(self as *const $rust_type as *const crate::Attribute) }
            }
        }

        impl std::ops::DerefMut for $rust_type {
            #[inline]
            fn deref_mut(&mut self) -> &mut crate::Attribute {
                unsafe { &mut *(self as *mut $rust_type as *mut crate::Attribute) }
            }
        }
    }
}