Skip to content

Commit 1cbe818

Browse files
committed
changed glib/pango Boxed types to use wrapper attribute
1 parent dadbe8b commit 1cbe818

12 files changed

+51
-64
lines changed

glib/src/error.rs

+18-23
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@
55
66
use std::{borrow::Cow, convert::Infallible, error, ffi::CStr, fmt, str};
77

8-
use crate::{translate::*, Quark};
8+
use crate::{self as glib, translate::*, Quark};
99

10-
wrapper! {
11-
// rustdoc-stripper-ignore-next
12-
/// A generic error capable of representing various error domains (types).
13-
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
14-
#[doc(alias = "GError")]
15-
pub struct Error(Boxed<ffi::GError>);
16-
17-
match fn {
18-
copy => |ptr| ffi::g_error_copy(ptr),
19-
free => |ptr| ffi::g_error_free(ptr),
20-
type_ => || ffi::g_error_get_type(),
21-
}
22-
}
10+
// rustdoc-stripper-ignore-next
11+
/// A generic error capable of representing various error domains (types).
12+
#[glib_macros::wrapper(
13+
copy = ffi::g_error_copy,
14+
free = ffi::g_error_free,
15+
type = ffi::g_error_get_type,
16+
)]
17+
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
18+
#[doc(alias = "GError")]
19+
pub struct Error(Boxed<ffi::GError>);
2320

2421
unsafe impl Send for Error {}
2522
unsafe impl Sync for Error {}
@@ -42,20 +39,20 @@ impl Error {
4239
// rustdoc-stripper-ignore-next
4340
/// Checks if the error domain matches `T`.
4441
pub fn is<T: ErrorDomain>(&self) -> bool {
45-
self.inner.domain == T::domain().into_glib()
42+
self.0.domain == T::domain().into_glib()
4643
}
4744

4845
// rustdoc-stripper-ignore-next
4946
/// Returns the error domain quark
5047
pub fn domain(&self) -> Quark {
51-
unsafe { from_glib(self.inner.domain) }
48+
unsafe { from_glib(self.0.domain) }
5249
}
5350

5451
// rustdoc-stripper-ignore-next
5552
/// Checks if the error matches the specified domain and error code.
5653
#[doc(alias = "g_error_matches")]
5754
pub fn matches<T: ErrorDomain>(&self, err: T) -> bool {
58-
self.is::<T>() && self.inner.code == err.code()
55+
self.is::<T>() && self.0.code == err.code()
5956
}
6057

6158
// rustdoc-stripper-ignore-next
@@ -77,7 +74,7 @@ impl Error {
7774
/// ```
7875
pub fn kind<T: ErrorDomain>(&self) -> Option<T> {
7976
if self.is::<T>() {
80-
T::from(self.inner.code)
77+
T::from(self.0.code)
8178
} else {
8279
None
8380
}
@@ -90,7 +87,7 @@ impl Error {
9087
/// trait, but you can use this method if you need to have the message as a `&str`.
9188
pub fn message(&self) -> &str {
9289
unsafe {
93-
let bytes = CStr::from_ptr(self.inner.message).to_bytes();
90+
let bytes = CStr::from_ptr(self.0.message).to_bytes();
9491
str::from_utf8(bytes)
9592
.unwrap_or_else(|err| str::from_utf8(&bytes[..err.valid_up_to()]).unwrap())
9693
}
@@ -106,10 +103,8 @@ impl fmt::Display for Error {
106103
impl fmt::Debug for Error {
107104
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108105
f.debug_struct("Error")
109-
.field("domain", unsafe {
110-
&crate::Quark::from_glib(self.inner.domain)
111-
})
112-
.field("code", &self.inner.code)
106+
.field("domain", unsafe { &crate::Quark::from_glib(self.0.domain) })
107+
.field("code", &self.0.code)
113108
.field("message", &self.message())
114109
.finish()
115110
}

glib/src/value_array.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::{cmp::Ordering, ops, slice};
44

55
use crate::{
6+
self as glib,
67
prelude::*,
78
translate::*,
89
value::{
@@ -11,16 +12,13 @@ use crate::{
1112
HasParamSpec, ParamSpecValueArray, ParamSpecValueArrayBuilder, Type, Value,
1213
};
1314

14-
wrapper! {
15-
#[derive(Debug)]
16-
#[doc(alias = "GValueArray")]
17-
pub struct ValueArray(Boxed<gobject_ffi::GValueArray>);
18-
19-
match fn {
20-
copy => |ptr| gobject_ffi::g_value_array_copy(mut_override(ptr)),
21-
free => |ptr| gobject_ffi::g_value_array_free(ptr),
22-
}
23-
}
15+
#[glib_macros::wrapper(
16+
copy = |ptr| gobject_ffi::g_value_array_copy(mut_override(ptr)),
17+
free = gobject_ffi::g_value_array_free,
18+
)]
19+
#[derive(Debug)]
20+
#[doc(alias = "GValueArray")]
21+
pub struct ValueArray(Boxed<gobject_ffi::GValueArray>);
2422

2523
impl ValueArray {
2624
#[doc(alias = "g_value_array_new")]
@@ -43,7 +41,7 @@ impl ValueArray {
4341

4442
#[inline]
4543
pub fn len(&self) -> usize {
46-
self.inner.n_values as usize
44+
self.0.n_values as usize
4745
}
4846

4947
#[doc(alias = "get_nth")]

pango/src/attr_color.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ impl AttrColor {
4545
}
4646

4747
pub fn color(&self) -> Color {
48-
unsafe { from_glib_none((&self.inner.color) as *const ffi::PangoColor) }
48+
unsafe { from_glib_none((&self.0.color) as *const ffi::PangoColor) }
4949
}
5050
}

pango/src/attr_float.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ impl AttrFloat {
2929
}
3030

3131
pub fn value(&self) -> f64 {
32-
self.inner.value
32+
self.0.value
3333
}
3434
}

pango/src/attr_font_desc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ impl AttrFontDesc {
1313
}
1414

1515
pub fn desc(&self) -> FontDescription {
16-
unsafe { from_glib_none(self.inner.desc) }
16+
unsafe { from_glib_none(self.0.desc) }
1717
}
1818
}

pango/src/attr_font_features.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ impl AttrFontFeatures {
1717
}
1818

1919
pub fn features(&self) -> glib::GString {
20-
unsafe { from_glib_none(self.inner.features) }
20+
unsafe { from_glib_none(self.0.features) }
2121
}
2222
}

pango/src/attr_int.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,6 @@ impl AttrInt {
156156
}
157157

158158
pub fn value(&self) -> i32 {
159-
self.inner.value
159+
self.0.value
160160
}
161161
}

pango/src/attr_language.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ impl AttrLanguage {
1717
}
1818

1919
pub fn value(&self) -> Language {
20-
unsafe { from_glib_none(self.inner.value) }
20+
unsafe { from_glib_none(self.0.value) }
2121
}
2222
}

pango/src/attr_shape.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ impl AttrShape {
1818
}
1919

2020
pub fn ink_rect(&self) -> Rectangle {
21-
unsafe { from_glib_none(&self.inner.ink_rect as *const _) }
21+
unsafe { from_glib_none(&self.0.ink_rect as *const _) }
2222
}
2323

2424
pub fn logical_rect(&self) -> Rectangle {
25-
unsafe { from_glib_none(&self.inner.logical_rect as *const _) }
25+
unsafe { from_glib_none(&self.0.logical_rect as *const _) }
2626
}
2727
}

pango/src/attr_size.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ impl AttrSize {
2222
}
2323

2424
pub fn size(&self) -> i32 {
25-
self.inner.size
25+
self.0.size
2626
}
2727

2828
pub fn absolute(&self) -> bool {
29-
unsafe { from_glib(self.inner.absolute as i32) }
29+
unsafe { from_glib(self.0.absolute as i32) }
3030
}
3131
}

pango/src/attr_string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ impl AttrString {
1313
}
1414

1515
pub fn value(&self) -> glib::GString {
16-
unsafe { from_glib_none(self.inner.value) }
16+
unsafe { from_glib_none(self.0.value) }
1717
}
1818
}

pango/src/attribute.rs

+13-19
Original file line numberDiff line numberDiff line change
@@ -84,30 +84,24 @@ macro_rules! define_attribute_struct {
8484

8585
#[cfg(any(feature = "v1_44", feature = "dox"))]
8686
#[cfg_attr(feature = "dox", doc(cfg(feature = "v1_44")))]
87-
glib::wrapper! {
88-
#[derive(Debug)]
89-
pub struct $rust_type(Boxed<$ffi_type>);
90-
91-
match fn {
92-
copy => |ptr| ffi::pango_attribute_copy(ptr as *const ffi::PangoAttribute) as *mut $ffi_type,
93-
free => |ptr| ffi::pango_attribute_destroy(ptr as *mut ffi::PangoAttribute),
94-
type_ => || ffi::pango_attribute_get_type(),
95-
}
96-
}
87+
#[glib::wrapper_attr(
88+
copy = |ptr| ffi::pango_attribute_copy(ptr as *const ffi::PangoAttribute) as *mut $ffi_type,
89+
free = |ptr| ffi::pango_attribute_destroy(ptr as *mut ffi::PangoAttribute),
90+
type = ffi::pango_attribute_get_type,
91+
)]
92+
#[derive(Debug)]
93+
pub struct $rust_type(Boxed<$ffi_type>);
9794

9895
unsafe impl Send for $rust_type {}
9996
unsafe impl Sync for $rust_type {}
10097

10198
#[cfg(not(any(feature = "v1_44", feature = "dox")))]
102-
glib::wrapper! {
103-
#[derive(Debug)]
104-
pub struct $rust_type(Boxed<$ffi_type>);
105-
106-
match fn {
107-
copy => |ptr| ffi::pango_attribute_copy(ptr as *const ffi::PangoAttribute) as *mut $ffi_type,
108-
free => |ptr| ffi::pango_attribute_destroy(ptr as *mut ffi::PangoAttribute),
109-
}
110-
}
99+
#[glib::wrapper_attr(
100+
copy = |ptr| ffi::pango_attribute_copy(ptr as *const ffi::PangoAttribute) as *mut $ffi_type,
101+
free = |ptr| ffi::pango_attribute_destroy(ptr as *mut ffi::PangoAttribute),
102+
)]
103+
#[derive(Debug)]
104+
pub struct $rust_type(Boxed<$ffi_type>);
111105

112106
impl $rust_type {
113107
#[doc(alias = "pango_attribute_equal")]

0 commit comments

Comments
 (0)