Skip to content

Commit

Permalink
changed glib/pango Boxed types to use wrapper attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jf2048 committed Feb 14, 2023
1 parent b17e301 commit 2538062
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 64 deletions.
41 changes: 18 additions & 23 deletions glib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@

use std::{borrow::Cow, convert::Infallible, error, ffi::CStr, fmt, str};

use crate::{translate::*, Quark};
use crate::{self as glib, translate::*, Quark};

wrapper! {
// rustdoc-stripper-ignore-next
/// A generic error capable of representing various error domains (types).
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
#[doc(alias = "GError")]
pub struct Error(Boxed<ffi::GError>);

match fn {
copy => |ptr| ffi::g_error_copy(ptr),
free => |ptr| ffi::g_error_free(ptr),
type_ => || ffi::g_error_get_type(),
}
}
// rustdoc-stripper-ignore-next
/// A generic error capable of representing various error domains (types).
#[glib_macros::wrapper(
copy = ffi::g_error_copy,
free = ffi::g_error_free,
type = ffi::g_error_get_type,
)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
#[doc(alias = "GError")]
pub struct Error(Boxed<ffi::GError>);

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

// rustdoc-stripper-ignore-next
/// Returns the error domain quark
pub fn domain(&self) -> Quark {
unsafe { from_glib(self.inner.domain) }
unsafe { from_glib(self.0.domain) }
}

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

// rustdoc-stripper-ignore-next
Expand All @@ -77,7 +74,7 @@ impl Error {
/// ```
pub fn kind<T: ErrorDomain>(&self) -> Option<T> {
if self.is::<T>() {
T::from(self.inner.code)
T::from(self.0.code)
} else {
None
}
Expand All @@ -90,7 +87,7 @@ impl Error {
/// trait, but you can use this method if you need to have the message as a `&str`.
pub fn message(&self) -> &str {
unsafe {
let bytes = CStr::from_ptr(self.inner.message).to_bytes();
let bytes = CStr::from_ptr(self.0.message).to_bytes();
str::from_utf8(bytes)
.unwrap_or_else(|err| str::from_utf8(&bytes[..err.valid_up_to()]).unwrap())
}
Expand All @@ -106,10 +103,8 @@ impl fmt::Display for Error {
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Error")
.field("domain", unsafe {
&crate::Quark::from_glib(self.inner.domain)
})
.field("code", &self.inner.code)
.field("domain", unsafe { &crate::Quark::from_glib(self.0.domain) })
.field("code", &self.0.code)
.field("message", &self.message())
.finish()
}
Expand Down
20 changes: 9 additions & 11 deletions glib/src/value_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::{cmp::Ordering, ops, slice};

use crate::{
self as glib,
prelude::*,
translate::*,
value::{
Expand All @@ -11,16 +12,13 @@ use crate::{
HasParamSpec, ParamSpecValueArray, ParamSpecValueArrayBuilder, Type, Value,
};

wrapper! {
#[derive(Debug)]
#[doc(alias = "GValueArray")]
pub struct ValueArray(Boxed<gobject_ffi::GValueArray>);

match fn {
copy => |ptr| gobject_ffi::g_value_array_copy(mut_override(ptr)),
free => |ptr| gobject_ffi::g_value_array_free(ptr),
}
}
#[glib_macros::wrapper(
copy = |ptr| gobject_ffi::g_value_array_copy(mut_override(ptr)),
free = gobject_ffi::g_value_array_free,
)]
#[derive(Debug)]
#[doc(alias = "GValueArray")]
pub struct ValueArray(Boxed<gobject_ffi::GValueArray>);

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

#[inline]
pub fn len(&self) -> usize {
self.inner.n_values as usize
self.0.n_values as usize
}

#[doc(alias = "get_nth")]
Expand Down
2 changes: 1 addition & 1 deletion pango/src/attr_color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ impl AttrColor {
}

pub fn color(&self) -> Color {
unsafe { from_glib_none((&self.inner.color) as *const ffi::PangoColor) }
unsafe { from_glib_none((&self.0.color) as *const ffi::PangoColor) }
}
}
2 changes: 1 addition & 1 deletion pango/src/attr_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ impl AttrFloat {
}

pub fn value(&self) -> f64 {
self.inner.value
self.0.value
}
}
2 changes: 1 addition & 1 deletion pango/src/attr_font_desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ impl AttrFontDesc {
}

pub fn desc(&self) -> FontDescription {
unsafe { from_glib_none(self.inner.desc) }
unsafe { from_glib_none(self.0.desc) }
}
}
2 changes: 1 addition & 1 deletion pango/src/attr_font_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ impl AttrFontFeatures {
}

pub fn features(&self) -> glib::GString {
unsafe { from_glib_none(self.inner.features) }
unsafe { from_glib_none(self.0.features) }
}
}
2 changes: 1 addition & 1 deletion pango/src/attr_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,6 @@ impl AttrInt {
}

pub fn value(&self) -> i32 {
self.inner.value
self.0.value
}
}
2 changes: 1 addition & 1 deletion pango/src/attr_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ impl AttrLanguage {
}

pub fn value(&self) -> Language {
unsafe { from_glib_none(self.inner.value) }
unsafe { from_glib_none(self.0.value) }
}
}
4 changes: 2 additions & 2 deletions pango/src/attr_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ impl AttrShape {
}

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

pub fn logical_rect(&self) -> Rectangle {
unsafe { from_glib_none(&self.inner.logical_rect as *const _) }
unsafe { from_glib_none(&self.0.logical_rect as *const _) }
}
}
4 changes: 2 additions & 2 deletions pango/src/attr_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ impl AttrSize {
}

pub fn size(&self) -> i32 {
self.inner.size
self.0.size
}

pub fn absolute(&self) -> bool {
unsafe { from_glib(self.inner.absolute as i32) }
unsafe { from_glib(self.0.absolute as i32) }
}
}
2 changes: 1 addition & 1 deletion pango/src/attr_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ impl AttrString {
}

pub fn value(&self) -> glib::GString {
unsafe { from_glib_none(self.inner.value) }
unsafe { from_glib_none(self.0.value) }
}
}
32 changes: 13 additions & 19 deletions pango/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,24 @@ macro_rules! define_attribute_struct {

#[cfg(any(feature = "v1_44", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(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(),
}
}
#[glib::wrapper_attr(
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,
)]
#[derive(Debug)]
pub struct $rust_type(Boxed<$ffi_type>);

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

#[cfg(not(any(feature = "v1_44", feature = "dox")))]
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),
}
}
#[glib::wrapper_attr(
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),
)]
#[derive(Debug)]
pub struct $rust_type(Boxed<$ffi_type>);

impl $rust_type {
#[doc(alias = "pango_attribute_equal")]
Expand Down

0 comments on commit 2538062

Please sign in to comment.