From 9d5ae8ed14056ac8abbf9da2dd23d1a60d9496c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sat, 7 Mar 2020 00:31:30 +0200 Subject: [PATCH] gstreamer: Don't take mini objects by value for append() and similar operations Compared to let foo = Foo::bar(foo); the new form is easier to follow foo.bar(); --- gstreamer/src/buffer.rs | 8 ++--- gstreamer/src/caps.rs | 68 +++++++++++++++++++++---------------- gstreamer/src/miniobject.rs | 11 ++++-- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/gstreamer/src/buffer.rs b/gstreamer/src/buffer.rs index 39c526f21..ae91ebc70 100644 --- a/gstreamer/src/buffer.rs +++ b/gstreamer/src/buffer.rs @@ -159,13 +159,11 @@ impl Buffer { } } - pub fn append(buffer: Self, other: Self) -> Self { + pub fn append(&mut self, other: Self) { skip_assert_initialized!(); unsafe { - from_glib_full(gst_sys::gst_buffer_append( - buffer.into_ptr(), - other.into_ptr(), - )) + let ptr = gst_sys::gst_buffer_append(self.as_mut_ptr(), other.into_ptr()); + self.replace_ptr(ptr); } } } diff --git a/gstreamer/src/caps.rs b/gstreamer/src/caps.rs index a50640cf4..69e146c23 100644 --- a/gstreamer/src/caps.rs +++ b/gstreamer/src/caps.rs @@ -70,54 +70,64 @@ impl Caps { caps } - pub fn fixate(caps: Self) -> Self { - skip_assert_initialized!(); - unsafe { from_glib_full(gst_sys::gst_caps_fixate(caps.into_ptr())) } - } - - pub fn merge(caps: Self, other: Self) -> Self { - skip_assert_initialized!(); - unsafe { from_glib_full(gst_sys::gst_caps_merge(caps.into_ptr(), other.into_ptr())) } - } - - pub fn merge_structure(caps: Self, structure: Structure) -> Self { + pub fn fixate(&mut self) { skip_assert_initialized!(); unsafe { - from_glib_full(gst_sys::gst_caps_merge_structure( - caps.into_ptr(), - structure.into_ptr(), - )) + let ptr = gst_sys::gst_caps_fixate(self.as_mut_ptr()); + self.replace_ptr(ptr); } } - pub fn merge_structure_full( - caps: Self, - structure: Structure, - features: Option, - ) -> Self { + pub fn merge(&mut self, other: Self) { skip_assert_initialized!(); unsafe { - from_glib_full(gst_sys::gst_caps_merge_structure_full( - caps.into_ptr(), + let ptr = gst_sys::gst_caps_merge(self.as_mut_ptr(), other.into_ptr()); + self.replace_ptr(ptr); + } + } + + pub fn merge_structure(&mut self, structure: Structure) { + skip_assert_initialized!(); + unsafe { + let ptr = gst_sys::gst_caps_merge_structure(self.as_mut_ptr(), structure.into_ptr()); + self.replace_ptr(ptr); + } + } + + pub fn merge_structure_full(&mut self, structure: Structure, features: Option) { + skip_assert_initialized!(); + unsafe { + let ptr = gst_sys::gst_caps_merge_structure_full( + self.as_mut_ptr(), structure.into_ptr(), features.map(|f| f.into_ptr()).unwrap_or(ptr::null_mut()), - )) + ); + self.replace_ptr(ptr); } } - pub fn normalize(caps: Self) -> Self { + pub fn normalize(&mut self) { skip_assert_initialized!(); - unsafe { from_glib_full(gst_sys::gst_caps_normalize(caps.into_ptr())) } + unsafe { + let ptr = gst_sys::gst_caps_normalize(self.as_mut_ptr()); + self.replace_ptr(ptr); + } } - pub fn simplify(caps: Self) -> Self { + pub fn simplify(&mut self) { skip_assert_initialized!(); - unsafe { from_glib_full(gst_sys::gst_caps_simplify(caps.into_ptr())) } + unsafe { + let ptr = gst_sys::gst_caps_simplify(self.as_mut_ptr()); + self.replace_ptr(ptr); + } } - pub fn truncate(caps: Self) -> Self { + pub fn truncate(&mut self) { skip_assert_initialized!(); - unsafe { from_glib_full(gst_sys::gst_caps_truncate(caps.into_ptr())) } + unsafe { + let ptr = gst_sys::gst_caps_truncate(self.as_mut_ptr()); + self.replace_ptr(ptr); + } } } diff --git a/gstreamer/src/miniobject.rs b/gstreamer/src/miniobject.rs index 83fa96f1c..43ec39377 100644 --- a/gstreamer/src/miniobject.rs +++ b/gstreamer/src/miniobject.rs @@ -61,16 +61,21 @@ impl GstRc { } } + pub unsafe fn replace_ptr(&mut self, ptr: *mut T::GstType) { + assert!(!ptr.is_null()); + self.obj = ptr::NonNull::new_unchecked(ptr as *mut T); + } + pub fn make_mut(&mut self) -> &mut T { unsafe { if self.is_writable() { return self.obj.as_mut(); } - let ptr = T::from_mut_ptr(gst_sys::gst_mini_object_make_writable( + let ptr = gst_sys::gst_mini_object_make_writable( self.as_mut_ptr() as *mut gst_sys::GstMiniObject - ) as *mut T::GstType); - self.obj = ptr::NonNull::new_unchecked(ptr); + ); + self.replace_ptr(ptr as *mut T::GstType); assert!(self.is_writable()); self.obj.as_mut()