Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit 44e9f62

Browse files
authored
Merge pull request #445 from sdroege/properties-borrow
Minor subclass API usability improvements
2 parents 697f46c + 03e3314 commit 44e9f62

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/subclass/interface.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ffi;
66
use gobject_ffi;
77

8+
use std::borrow::Borrow;
89
use std::marker;
910
use std::mem;
1011
use std::ptr;
@@ -119,12 +120,13 @@ pub trait ObjectInterfaceExt: ObjectInterface {
119120
/// Install properties on the interface.
120121
///
121122
/// All implementors of the interface must provide these properties.
122-
fn install_properties(&mut self, properties: &[Property]) {
123+
fn install_properties<'a, T: Borrow<Property<'a>>>(&mut self, properties: &[T]) {
123124
if properties.is_empty() {
124125
return;
125126
}
126127

127128
for property in properties {
129+
let property = property.borrow();
128130
let pspec = (property.1)(property.0);
129131
unsafe {
130132
gobject_ffi::g_object_interface_install_property(

src/subclass/object.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use ffi;
99
use gobject_ffi;
1010

11+
use std::borrow::Borrow;
12+
use std::fmt;
1113
use std::mem;
1214
use std::ptr;
1315

@@ -133,8 +135,15 @@ unsafe extern "C" fn constructed<T: ObjectSubclass>(obj: *mut gobject_ffi::GObje
133135
}
134136

135137
/// Definition of a property.
138+
#[derive(Clone)]
136139
pub struct Property<'a>(pub &'a str, pub fn(&str) -> ::ParamSpec);
137140

141+
impl<'a> fmt::Debug for Property<'a> {
142+
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
143+
f.debug_tuple("Property").field(&self.0).finish()
144+
}
145+
}
146+
138147
/// Extension trait for `glib::Object`'s class struct.
139148
///
140149
/// This contains various class methods and allows subclasses to override the virtual methods.
@@ -143,14 +152,15 @@ pub unsafe trait ObjectClassSubclassExt: Sized + 'static {
143152
///
144153
/// The index in the properties array is going to be the index passed to the
145154
/// property setters and getters.
146-
fn install_properties(&mut self, properties: &[Property]) {
155+
fn install_properties<'a, T: Borrow<Property<'a>>>(&mut self, properties: &[T]) {
147156
if properties.is_empty() {
148157
return;
149158
}
150159

151160
let mut pspecs = Vec::with_capacity(properties.len());
152161

153162
for property in properties {
163+
let property = property.borrow();
154164
let pspec = (property.1)(property.0);
155165
pspecs.push(pspec);
156166
}

src/subclass/types.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ffi;
88
use gobject_ffi;
99

10+
use std::fmt;
1011
use std::marker;
1112
use std::mem;
1213
use std::ptr;
@@ -535,6 +536,15 @@ impl SignalInvocationHint {
535536
}
536537
}
537538

539+
impl fmt::Debug for SignalInvocationHint {
540+
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
541+
f.debug_struct("SignalInvocationHint")
542+
.field("detail", &self.detail())
543+
.field("run_type", &self.run_type())
544+
.finish()
545+
}
546+
}
547+
538548
pub(crate) unsafe fn add_signal_with_accumulator<F>(
539549
type_: ffi::GType,
540550
name: &str,
@@ -582,6 +592,14 @@ pub(crate) unsafe fn add_signal_with_accumulator<F>(
582592

583593
pub struct SignalClassHandlerToken(*mut gobject_ffi::GTypeInstance);
584594

595+
impl fmt::Debug for SignalClassHandlerToken {
596+
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
597+
f.debug_tuple("SignalClassHandlerToken")
598+
.field(&unsafe { ::Object::from_glib_borrow(self.0 as *mut gobject_ffi::GObject) })
599+
.finish()
600+
}
601+
}
602+
585603
pub(crate) unsafe fn add_signal_with_class_handler<F>(
586604
type_: ffi::GType,
587605
name: &str,

0 commit comments

Comments
 (0)