From 8d0fa62275b8ded84ef77e7bb502ae55107597e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Mon, 9 Mar 2020 12:55:14 +0200 Subject: [PATCH] Minor cleanup in various places --- gstreamer-check/src/harness.rs | 32 +++++----------- gstreamer/src/event.rs | 69 +++++++++------------------------- gstreamer/src/iterator.rs | 24 ++++++------ gstreamer/src/pad.rs | 4 +- gstreamer/src/structure.rs | 10 ++--- 5 files changed, 44 insertions(+), 95 deletions(-) diff --git a/gstreamer-check/src/harness.rs b/gstreamer-check/src/harness.rs index c082e7070..f4d2e543c 100644 --- a/gstreamer-check/src/harness.rs +++ b/gstreamer-check/src/harness.rs @@ -730,7 +730,7 @@ impl Harness { None } else { Some(Ref( - Some(Harness( + mem::ManuallyDrop::new(Harness( ptr::NonNull::new_unchecked(sink_harness), PhantomData, )), @@ -747,7 +747,7 @@ impl Harness { None } else { Some(Ref( - Some(Harness( + mem::ManuallyDrop::new(Harness( ptr::NonNull::new_unchecked(src_harness), PhantomData, )), @@ -764,7 +764,7 @@ impl Harness { None } else { Some(RefMut( - Some(Harness( + mem::ManuallyDrop::new(Harness( ptr::NonNull::new_unchecked(sink_harness), PhantomData, )), @@ -781,7 +781,7 @@ impl Harness { None } else { Some(RefMut( - Some(Harness( + mem::ManuallyDrop::new(Harness( ptr::NonNull::new_unchecked(src_harness), PhantomData, )), @@ -793,44 +793,30 @@ impl Harness { } #[derive(Debug)] -pub struct Ref<'a>(Option, PhantomData<&'a Harness>); +pub struct Ref<'a>(mem::ManuallyDrop, PhantomData<&'a Harness>); impl<'a> ops::Deref for Ref<'a> { type Target = Harness; fn deref(&self) -> &Harness { - self.0.as_ref().unwrap() - } -} - -impl<'a> Drop for Ref<'a> { - fn drop(&mut self) { - // We only really borrow - mem::forget(self.0.take()) + &*self.0 } } #[derive(Debug)] -pub struct RefMut<'a>(Option, PhantomData<&'a mut Harness>); +pub struct RefMut<'a>(mem::ManuallyDrop, PhantomData<&'a mut Harness>); impl<'a> ops::Deref for RefMut<'a> { type Target = Harness; fn deref(&self) -> &Harness { - self.0.as_ref().unwrap() + &*self.0 } } impl<'a> ops::DerefMut for RefMut<'a> { fn deref_mut(&mut self) -> &mut Harness { - self.0.as_mut().unwrap() - } -} - -impl<'a> Drop for RefMut<'a> { - fn drop(&mut self) { - // We only really borrow - mem::forget(self.0.take()) + &mut *self.0 } } diff --git a/gstreamer/src/event.rs b/gstreamer/src/event.rs index 787f27423..18ac1fcbc 100644 --- a/gstreamer/src/event.rs +++ b/gstreamer/src/event.rs @@ -1435,11 +1435,8 @@ impl<'a> NavigationBuilder<'a> { } event_builder_generic_impl!(|s: &mut Self| { - let structure = s.structure.take(); - let ev = gst_sys::gst_event_new_navigation(structure.to_glib_none().0); - mem::forget(structure); - - ev + let structure = s.structure.take().unwrap(); + gst_sys::gst_event_new_navigation(structure.into_ptr()) }); } @@ -1553,14 +1550,8 @@ impl<'a> CustomUpstreamBuilder<'a> { } event_builder_generic_impl!(|s: &mut Self| { - let structure = s.structure.take(); - let ev = gst_sys::gst_event_new_custom( - gst_sys::GST_EVENT_CUSTOM_UPSTREAM, - structure.to_glib_none().0, - ); - mem::forget(structure); - - ev + let structure = s.structure.take().unwrap(); + gst_sys::gst_event_new_custom(gst_sys::GST_EVENT_CUSTOM_UPSTREAM, structure.into_ptr()) }); } @@ -1578,14 +1569,8 @@ impl<'a> CustomDownstreamBuilder<'a> { } event_builder_generic_impl!(|s: &mut Self| { - let structure = s.structure.take(); - let ev = gst_sys::gst_event_new_custom( - gst_sys::GST_EVENT_CUSTOM_DOWNSTREAM, - structure.to_glib_none().0, - ); - mem::forget(structure); - - ev + let structure = s.structure.take().unwrap(); + gst_sys::gst_event_new_custom(gst_sys::GST_EVENT_CUSTOM_DOWNSTREAM, structure.into_ptr()) }); } @@ -1603,14 +1588,11 @@ impl<'a> CustomDownstreamOobBuilder<'a> { } event_builder_generic_impl!(|s: &mut Self| { - let structure = s.structure.take(); - let ev = gst_sys::gst_event_new_custom( + let structure = s.structure.take().unwrap(); + gst_sys::gst_event_new_custom( gst_sys::GST_EVENT_CUSTOM_DOWNSTREAM_OOB, - structure.to_glib_none().0, - ); - mem::forget(structure); - - ev + structure.into_ptr(), + ) }); } @@ -1628,14 +1610,11 @@ impl<'a> CustomDownstreamStickyBuilder<'a> { } event_builder_generic_impl!(|s: &mut Self| { - let structure = s.structure.take(); - let ev = gst_sys::gst_event_new_custom( + let structure = s.structure.take().unwrap(); + gst_sys::gst_event_new_custom( gst_sys::GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, - structure.to_glib_none().0, - ); - mem::forget(structure); - - ev + structure.into_ptr(), + ) }); } @@ -1653,14 +1632,8 @@ impl<'a> CustomBothBuilder<'a> { } event_builder_generic_impl!(|s: &mut Self| { - let structure = s.structure.take(); - let ev = gst_sys::gst_event_new_custom( - gst_sys::GST_EVENT_CUSTOM_BOTH, - structure.to_glib_none().0, - ); - mem::forget(structure); - - ev + let structure = s.structure.take().unwrap(); + gst_sys::gst_event_new_custom(gst_sys::GST_EVENT_CUSTOM_BOTH, structure.into_ptr()) }); } @@ -1678,14 +1651,8 @@ impl<'a> CustomBothOobBuilder<'a> { } event_builder_generic_impl!(|s: &mut Self| { - let structure = s.structure.take(); - let ev = gst_sys::gst_event_new_custom( - gst_sys::GST_EVENT_CUSTOM_BOTH_OOB, - structure.to_glib_none().0, - ); - mem::forget(structure); - - ev + let structure = s.structure.take().unwrap(); + gst_sys::gst_event_new_custom(gst_sys::GST_EVENT_CUSTOM_BOTH_OOB, structure.into_ptr()) }); } diff --git a/gstreamer/src/iterator.rs b/gstreamer/src/iterator.rs index 51ca4048e..aabf952e7 100644 --- a/gstreamer/src/iterator.rs +++ b/gstreamer/src/iterator.rs @@ -43,6 +43,12 @@ impl Iterator where for<'a> T: FromValueOptional<'a> + 'static, { + pub unsafe fn into_ptr(self) -> *mut gst_sys::GstIterator { + let it = self.to_glib_none().0; + mem::forget(self); + it as *mut _ + } + #[allow(clippy::should_implement_trait)] pub fn next(&mut self) -> Result, IteratorError> { unsafe { @@ -72,9 +78,6 @@ where F: Fn(T) -> bool + Send + Sync + 'static, { unsafe { - let it = self.to_glib_none().0; - mem::forget(self); - let func_box: Box bool + Send + Sync + 'static> = Box::new(func); let mut closure_value = glib::Value::from_type(from_glib(filter_boxed_get_type::())); gobject_sys::g_value_take_boxed( @@ -83,7 +86,7 @@ where ); from_glib_full(gst_sys::gst_iterator_filter( - it as *mut _, + self.into_ptr(), Some(filter_trampoline::), closure_value.to_glib_none().0, )) @@ -189,7 +192,7 @@ where { let it = it as *mut RsIterator; - (*it).imp = Some(imp); + ptr::write(&mut (*it).imp, imp); } from_glib_full(it) @@ -212,7 +215,7 @@ where for<'a> T: FromValueOptional<'a> + StaticType + ToValue + Send + 'static, { iter: gst_sys::GstIterator, - imp: Option, + imp: I, phantom: PhantomData, } @@ -241,7 +244,7 @@ where for<'a> T: FromValueOptional<'a> + StaticType + ToValue + Send + 'static, { let it = it as *mut RsIterator; - let _ = (*it).imp.take(); + ptr::drop_in_place(&mut (*it).imp); } unsafe extern "C" fn rs_iterator_next>( @@ -252,11 +255,10 @@ where for<'a> T: FromValueOptional<'a> + StaticType + ToValue + Send + 'static, { let it = it as *mut RsIterator; - match (*it).imp.as_mut().map(|imp| imp.next()).unwrap() { + match (*it).imp.next() { Some(Ok(value)) => { let value = value.to_value(); - ptr::write(result, ptr::read(value.to_glib_none().0)); - mem::forget(value); + ptr::write(result, value.into_raw()); gst_sys::GST_ITERATOR_OK } None => gst_sys::GST_ITERATOR_DONE, @@ -272,7 +274,7 @@ where for<'a> T: FromValueOptional<'a> + StaticType + ToValue + Send + 'static, { let it = it as *mut RsIterator; - (*it).imp.as_mut().map(|imp| imp.resync()).unwrap(); + (*it).imp.resync(); } #[derive(Clone)] diff --git a/gstreamer/src/pad.rs b/gstreamer/src/pad.rs index 1b7062a38..5ccda4f19 100644 --- a/gstreamer/src/pad.rs +++ b/gstreamer/src/pad.rs @@ -1316,10 +1316,8 @@ where &Pad::from_glib_borrow(pad).unsafe_cast(), Option::<::Object>::from_glib_borrow(parent).as_ref(), ); - let ptr = ret.to_glib_none().0; - mem::forget(ret); - ptr as *mut _ + ret.into_ptr() } unsafe extern "C" fn trampoline_link_function< diff --git a/gstreamer/src/structure.rs b/gstreamer/src/structure.rs index f9da0d662..4a1d40e9b 100644 --- a/gstreamer/src/structure.rs +++ b/gstreamer/src/structure.rs @@ -420,14 +420,10 @@ impl StructureRef { self.set_value(name, value); } - pub fn set_value(&mut self, name: &str, mut value: SendValue) { + pub fn set_value(&mut self, name: &str, value: SendValue) { unsafe { - gst_sys::gst_structure_take_value( - &mut self.0, - name.to_glib_none().0, - value.to_glib_none_mut().0, - ); - mem::forget(value); + let mut value = value.into_raw(); + gst_sys::gst_structure_take_value(&mut self.0, name.to_glib_none().0, &mut value); } }