Skip to content

Commit ae20f0d

Browse files
authored
Merge pull request #881 from sdroege/into-glib-ptr
Use `IntoGlibPtr` instead of `to_glib_full()` in more places
2 parents 85e2582 + 0cf12f8 commit ae20f0d

File tree

12 files changed

+66
-74
lines changed

12 files changed

+66
-74
lines changed

gdk-pixbuf/src/subclass/pixbuf_animation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,5 @@ unsafe extern "C" fn animation_get_iter<T: PixbufAnimationImpl>(
164164
let total = Duration::from_secs((*start_time_ptr).tv_sec.try_into().unwrap())
165165
+ Duration::from_micros((*start_time_ptr).tv_usec.try_into().unwrap());
166166

167-
imp.iter(total).to_glib_full()
167+
imp.iter(total).into_glib_ptr()
168168
}

gio/src/dbus_connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl DBusConnection {
207207
let incoming = from_glib(incoming);
208208
let callback: &P = &*(user_data as *mut _);
209209
let res = (*callback)(&connection, &message, incoming);
210-
res.to_glib_full()
210+
res.into_glib_ptr()
211211
}
212212
let filter_function = Some(filter_function_func::<P> as _);
213213
unsafe extern "C" fn user_data_free_func_func<

gio/src/settings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'a> BindingBuilder<'a> {
118118
let user_data = &*(user_data as *const Mappings);
119119
let f = user_data.1.as_ref().unwrap();
120120
let value = &*(value as *const glib::Value);
121-
f(value, from_glib_none(variant_type)).to_glib_full()
121+
f(value, from_glib_none(variant_type)).into_glib_ptr()
122122
}
123123
unsafe extern "C" fn destroy_closure(ptr: *mut libc::c_void) {
124124
let _ = Box::<Mappings>::from_raw(ptr as *mut _);

gio/src/subclass/action_group.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ unsafe extern "C" fn action_group_get_action_parameter_type<T: ActionGroupImpl>(
432432
let ret = imp.action_parameter_type(&action_name);
433433

434434
if let Some(param_type) = ret {
435-
let param_type = param_type.to_glib_full();
435+
let param_type = param_type.into_glib_ptr();
436436
wrap.set_qdata(
437437
*ACTION_GROUP_GET_ACTION_PARAMETER_QUARK,
438438
PtrHolder(param_type, |ptr| glib::ffi::g_free(ptr as *mut _)),
@@ -458,7 +458,7 @@ unsafe extern "C" fn action_group_get_action_state_type<T: ActionGroupImpl>(
458458

459459
if let Some(state_type) = ret {
460460
let instance = imp.obj();
461-
let state_type = state_type.to_glib_full();
461+
let state_type = state_type.into_glib_ptr();
462462
instance.set_qdata(
463463
*ACTION_GROUP_GET_ACTION_STATE_TYPE_QUARK,
464464
PtrHolder(state_type, |ptr| glib::ffi::g_free(ptr as *mut _)),
@@ -483,7 +483,7 @@ unsafe extern "C" fn action_group_get_action_state_hint<T: ActionGroupImpl>(
483483
let ret = imp.action_state_hint(&action_name);
484484
if let Some(state_hint) = ret {
485485
let instance = imp.obj();
486-
let state_hint_ptr = state_hint.to_glib_full();
486+
let state_hint_ptr = state_hint.into_glib_ptr();
487487
instance.set_qdata(
488488
*ACTION_GROUP_GET_ACTION_STATE_HINT_QUARK,
489489
PtrHolder(state_hint_ptr, |ptr| glib::ffi::g_variant_unref(ptr)),
@@ -507,7 +507,7 @@ unsafe extern "C" fn action_group_get_action_state<T: ActionGroupImpl>(
507507
let ret = imp.action_state(&action_name);
508508
if let Some(state) = ret {
509509
let instance = imp.obj();
510-
let state_ptr = state.to_glib_full();
510+
let state_ptr = state.into_glib_ptr();
511511
instance.set_qdata(
512512
*ACTION_GROUP_GET_ACTION_STATE_QUARK,
513513
PtrHolder(state_ptr, |ptr| glib::ffi::g_variant_unref(ptr)),
@@ -644,7 +644,7 @@ unsafe extern "C" fn action_group_query_action<T: ActionGroupImpl>(
644644
}
645645
if !parameter_type.is_null() {
646646
if let Some(rs_parameter_type) = rs_parameter_type {
647-
let ret = rs_parameter_type.to_glib_full();
647+
let ret = rs_parameter_type.into_glib_ptr();
648648
instance.set_qdata(
649649
*ACTION_GROUP_QUERY_ACTION_PARAM_TYPE_QUARK,
650650
PtrHolder(ret, |ptr| glib::ffi::g_free(ptr as *mut _)),
@@ -656,7 +656,7 @@ unsafe extern "C" fn action_group_query_action<T: ActionGroupImpl>(
656656
}
657657
if !state_type.is_null() {
658658
if let Some(rs_state_type) = rs_state_type {
659-
let ret = rs_state_type.to_glib_full();
659+
let ret = rs_state_type.into_glib_ptr();
660660
instance.set_qdata(
661661
*ACTION_GROUP_QUERY_ACTION_STATE_TYPE_QUARK,
662662
PtrHolder(ret, |ptr| glib::ffi::g_free(ptr as *mut _)),
@@ -668,7 +668,7 @@ unsafe extern "C" fn action_group_query_action<T: ActionGroupImpl>(
668668
}
669669
if !state_hint.is_null() {
670670
if let Some(rs_state_hint) = rs_state_hint {
671-
let ret = rs_state_hint.to_glib_full();
671+
let ret = rs_state_hint.into_glib_ptr();
672672
instance.set_qdata(
673673
*ACTION_GROUP_QUERY_ACTION_STATE_HINT_QUARK,
674674
PtrHolder(ret, |ptr| glib::ffi::g_variant_unref(ptr)),
@@ -680,7 +680,7 @@ unsafe extern "C" fn action_group_query_action<T: ActionGroupImpl>(
680680
}
681681
if !state.is_null() {
682682
if let Some(rs_state) = rs_state {
683-
let ret = rs_state.to_glib_full();
683+
let ret = rs_state.into_glib_ptr();
684684
instance.set_qdata(
685685
*ACTION_GROUP_QUERY_ACTION_STATE_QUARK,
686686
PtrHolder(ret, |ptr| glib::ffi::g_variant_unref(ptr)),

gio/src/subclass/list_model.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,5 @@ where
142142
type_.name()
143143
);
144144
};
145-
item.to_glib_full()
145+
item.into_glib_ptr()
146146
}

gio/src/task.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ glib::wrapper! {
6161

6262
macro_rules! task_impl {
6363
($name:ident $(, @bound: $bound:tt)? $(, @safety: $safety:tt)?) => {
64-
impl <V: ValueType $(+ $bound)?> $name<V> {
64+
impl <V: Into<glib::Value> + ValueType $(+ $bound)?> $name<V> {
6565
#[doc(alias = "g_task_new")]
6666
#[allow(unused_unsafe)]
6767
pub unsafe fn new<S, P, Q>(
@@ -260,14 +260,15 @@ macro_rules! task_impl {
260260
},
261261
#[cfg(not(feature = "v2_64"))]
262262
Ok(v) => unsafe {
263+
let v: glib::Value = v.into();
263264
ffi::g_task_return_pointer(
264265
self.to_glib_none().0,
265-
v.to_value().to_glib_full() as *mut _,
266+
<glib::Value as glib::translate::IntoGlibPtr::<*mut glib::gobject_ffi::GValue>>::into_glib_ptr(v) as glib::ffi::gpointer,
266267
Some(value_free),
267268
)
268269
},
269270
Err(e) => unsafe {
270-
ffi::g_task_return_error(self.to_glib_none().0, e.to_glib_full() as *mut _);
271+
ffi::g_task_return_error(self.to_glib_none().0, e.into_glib_ptr());
271272
},
272273
}
273274
}

glib/src/boxed_inline.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,17 +527,15 @@ macro_rules! glib_boxed_inline_wrapper {
527527
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::IntoGlibPtr<*mut $ffi_name> for $name $(<$($generic),+>)? {
528528
#[inline]
529529
unsafe fn into_glib_ptr(self) -> *mut $ffi_name {
530-
let s = std::mem::ManuallyDrop::new(self);
531-
$crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(&*s).0 as *mut _
530+
$crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_full(&self) as *mut _
532531
}
533532
}
534533

535534
#[doc(hidden)]
536535
impl $(<$($generic $(: $bound $(+ $bound2)*)?),+>)? $crate::translate::IntoGlibPtr<*const $ffi_name> for $name $(<$($generic),+>)? {
537536
#[inline]
538537
unsafe fn into_glib_ptr(self) -> *const $ffi_name {
539-
let s = std::mem::ManuallyDrop::new(self);
540-
$crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(&*s).0 as *const _
538+
$crate::translate::ToGlibPtr::<*const $ffi_name>::to_glib_full(&self)
541539
}
542540
}
543541
};

glib/src/main_context_channel.rs

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ impl<T> Channel<T> {
199199
#[repr(C)]
200200
struct ChannelSource<T, F: FnMut(T) -> Continue + 'static> {
201201
source: ffi::GSource,
202-
source_funcs: Option<Box<ffi::GSourceFuncs>>,
203-
channel: Option<Channel<T>>,
204-
callback: Option<ThreadGuard<F>>,
202+
source_funcs: Box<ffi::GSourceFuncs>,
203+
channel: Channel<T>,
204+
callback: ThreadGuard<F>,
205205
}
206206

207207
unsafe extern "C" fn dispatch<T, F: FnMut(T) -> Continue + 'static>(
@@ -218,21 +218,13 @@ unsafe extern "C" fn dispatch<T, F: FnMut(T) -> Continue + 'static>(
218218

219219
// Get a reference to the callback. This will panic if we're called from a different
220220
// thread than where the source was attached to the main context.
221-
let callback = source
222-
.callback
223-
.as_mut()
224-
.expect("ChannelSource called before Receiver was attached")
225-
.get_mut();
221+
let callback = source.callback.get_mut();
226222

227223
// Now iterate over all items that we currently have in the channel until it is
228224
// empty again. If all senders are disconnected at some point we remove the GSource
229225
// from the main context it was attached to as it will never ever be called again.
230-
let channel = source
231-
.channel
232-
.as_ref()
233-
.expect("ChannelSource without Channel");
234226
loop {
235-
match channel.try_recv() {
227+
match source.channel.try_recv() {
236228
Err(mpsc::TryRecvError::Empty) => break,
237229
Err(mpsc::TryRecvError::Disconnected) => return ffi::G_SOURCE_REMOVE,
238230
Ok(item) => {
@@ -250,14 +242,12 @@ unsafe extern "C" fn dispatch<T, F: FnMut(T) -> Continue + 'static>(
250242
unsafe extern "C" fn dispose<T, F: FnMut(T) -> Continue + 'static>(source: *mut ffi::GSource) {
251243
let source = &mut *(source as *mut ChannelSource<T, F>);
252244

253-
if let Some(ref channel) = source.channel {
254-
// Set the source inside the channel to None so that all senders know that there
255-
// is no receiver left and wake up the condition variable if any
256-
let mut inner = (channel.0).0.lock().unwrap();
257-
inner.source = ChannelSourceState::Destroyed;
258-
if let Some(ChannelBound { ref cond, .. }) = (channel.0).1 {
259-
cond.notify_all();
260-
}
245+
// Set the source inside the channel to None so that all senders know that there
246+
// is no receiver left and wake up the condition variable if any
247+
let mut inner = (source.channel.0).0.lock().unwrap();
248+
inner.source = ChannelSourceState::Destroyed;
249+
if let Some(ChannelBound { ref cond, .. }) = (source.channel.0).1 {
250+
cond.notify_all();
261251
}
262252
}
263253

@@ -266,14 +256,8 @@ unsafe extern "C" fn finalize<T, F: FnMut(T) -> Continue + 'static>(source: *mut
266256

267257
// Drop all memory we own by taking it out of the Options
268258

269-
#[cfg(feature = "v2_64")]
270-
{
271-
let _ = source.channel.take().expect("Receiver without channel");
272-
}
273259
#[cfg(not(feature = "v2_64"))]
274260
{
275-
let channel = source.channel.take().expect("Receiver without channel");
276-
277261
// FIXME: This is the same as would otherwise be done in the dispose() function but
278262
// unfortunately it doesn't exist in older version of GLib. Doing it only here can
279263
// cause a channel sender to get a reference to the source with reference count 0
@@ -283,14 +267,14 @@ unsafe extern "C" fn finalize<T, F: FnMut(T) -> Continue + 'static>(source: *mut
283267
//
284268
// Set the source inside the channel to None so that all senders know that there
285269
// is no receiver left and wake up the condition variable if any
286-
let mut inner = (channel.0).0.lock().unwrap();
270+
let mut inner = (source.channel.0).0.lock().unwrap();
287271
inner.source = ChannelSourceState::Destroyed;
288-
if let Some(ChannelBound { ref cond, .. }) = (channel.0).1 {
272+
if let Some(ChannelBound { ref cond, .. }) = (source.channel.0).1 {
289273
cond.notify_all();
290274
}
291275
}
292-
293-
let _ = source.source_funcs.take();
276+
ptr::drop_in_place(&mut source.channel);
277+
ptr::drop_in_place(&mut source.source_funcs);
294278

295279
// Take the callback out of the source. This will panic if the value is dropped
296280
// from a different thread than where the callback was created so try to drop it
@@ -299,11 +283,10 @@ unsafe extern "C" fn finalize<T, F: FnMut(T) -> Continue + 'static>(source: *mut
299283
// This can only really happen if the caller to `attach()` gets the `Source` from the returned
300284
// `SourceId` and sends it to another thread or otherwise retrieves it from the main context,
301285
// but better safe than sorry.
302-
let callback = source
303-
.callback
304-
.take()
305-
.expect("channel source finalized twice");
306-
if !callback.is_owner() {
286+
if source.callback.is_owner() {
287+
ptr::drop_in_place(&mut source.callback);
288+
} else {
289+
let callback = ptr::read(&source.callback);
307290
let context =
308291
ffi::g_source_get_context(source as *mut ChannelSource<T, F> as *mut ffi::GSource);
309292
if !context.is_null() {
@@ -522,9 +505,9 @@ impl<T> Receiver<T> {
522505
// Store all our data inside our part of the GSource
523506
{
524507
let source = &mut *source;
525-
ptr::write(&mut source.channel, Some(channel));
526-
ptr::write(&mut source.callback, Some(ThreadGuard::new(func)));
527-
ptr::write(&mut source.source_funcs, Some(source_funcs));
508+
ptr::write(ptr::addr_of_mut!(source.channel), channel);
509+
ptr::write(ptr::addr_of_mut!(source.callback), ThreadGuard::new(func));
510+
ptr::write(ptr::addr_of_mut!(source.source_funcs), source_funcs);
528511
}
529512

530513
let source = Source::from_glib_full(mut_override(&(*source).source));

glib/src/param_spec.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl ParamSpec {
118118
pub fn downcast<T: ParamSpecType>(self) -> Result<T, ParamSpec> {
119119
unsafe {
120120
if self.type_() == T::static_type() {
121-
Ok(from_glib_full(self.to_glib_full()))
121+
Ok(from_glib_full(self.into_glib_ptr()))
122122
} else {
123123
Err(self)
124124
}
@@ -497,7 +497,7 @@ macro_rules! define_param_spec {
497497
#[inline]
498498
pub fn upcast(self) -> ParamSpec {
499499
unsafe {
500-
from_glib_full(ToGlibPtr::<*const $ffi_type>::to_glib_full(&self) as *mut gobject_ffi::GParamSpec)
500+
from_glib_full(IntoGlibPtr::<*mut $ffi_type>::into_glib_ptr(self) as *mut gobject_ffi::GParamSpec)
501501
}
502502
}
503503

@@ -1462,11 +1462,18 @@ impl ParamSpecValueArray {
14621462

14631463
#[doc(alias = "get_element_spec")]
14641464
#[inline]
1465-
pub fn element_spec(&self) -> Option<ParamSpec> {
1465+
pub fn element_spec(&self) -> Option<&ParamSpec> {
14661466
unsafe {
14671467
let ptr = ToGlibPtr::<*const gobject_ffi::GParamSpecValueArray>::to_glib_none(self).0;
14681468

1469-
from_glib_none((*ptr).element_spec)
1469+
if (*ptr).element_spec.is_null() {
1470+
None
1471+
} else {
1472+
Some(
1473+
&*(&(*ptr).element_spec as *const *mut gobject_ffi::GParamSpec
1474+
as *const ParamSpec),
1475+
)
1476+
}
14701477
}
14711478
}
14721479

glib/src/subclass/types.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,9 +927,7 @@ unsafe extern "C" fn finalize<T: ObjectSubclass>(obj: *mut gobject_ffi::GObject)
927927
let priv_ptr = ptr.offset(private_offset);
928928
let priv_storage = &mut *(priv_ptr as *mut PrivateStruct<T>);
929929
ptr::drop_in_place(&mut priv_storage.imp);
930-
if let Some(instance_data) = priv_storage.instance_data.take() {
931-
drop(instance_data);
932-
}
930+
ptr::drop_in_place(&mut priv_storage.instance_data);
933931

934932
// Chain up to the parent class' finalize implementation, if any.
935933
let parent_class = &*(data.as_ref().parent_class() as *const gobject_ffi::GObjectClass);

0 commit comments

Comments
 (0)