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();
This commit is contained in:
Sebastian Dröge 2020-03-07 00:31:30 +02:00
parent a07d6f2823
commit 9d5ae8ed14
3 changed files with 50 additions and 37 deletions

View file

@ -159,13 +159,11 @@ impl Buffer {
} }
} }
pub fn append(buffer: Self, other: Self) -> Self { pub fn append(&mut self, other: Self) {
skip_assert_initialized!(); skip_assert_initialized!();
unsafe { unsafe {
from_glib_full(gst_sys::gst_buffer_append( let ptr = gst_sys::gst_buffer_append(self.as_mut_ptr(), other.into_ptr());
buffer.into_ptr(), self.replace_ptr(ptr);
other.into_ptr(),
))
} }
} }
} }

View file

@ -70,54 +70,64 @@ impl Caps {
caps caps
} }
pub fn fixate(caps: Self) -> Self { pub fn fixate(&mut 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 {
skip_assert_initialized!(); skip_assert_initialized!();
unsafe { unsafe {
from_glib_full(gst_sys::gst_caps_merge_structure( let ptr = gst_sys::gst_caps_fixate(self.as_mut_ptr());
caps.into_ptr(), self.replace_ptr(ptr);
structure.into_ptr(),
))
} }
} }
pub fn merge_structure_full( pub fn merge(&mut self, other: Self) {
caps: Self,
structure: Structure,
features: Option<CapsFeatures>,
) -> Self {
skip_assert_initialized!(); skip_assert_initialized!();
unsafe { unsafe {
from_glib_full(gst_sys::gst_caps_merge_structure_full( let ptr = gst_sys::gst_caps_merge(self.as_mut_ptr(), other.into_ptr());
caps.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<CapsFeatures>) {
skip_assert_initialized!();
unsafe {
let ptr = gst_sys::gst_caps_merge_structure_full(
self.as_mut_ptr(),
structure.into_ptr(), structure.into_ptr(),
features.map(|f| f.into_ptr()).unwrap_or(ptr::null_mut()), 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!(); 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!(); 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!(); 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);
}
} }
} }

View file

@ -61,16 +61,21 @@ impl<T: MiniObject> GstRc<T> {
} }
} }
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 { pub fn make_mut(&mut self) -> &mut T {
unsafe { unsafe {
if self.is_writable() { if self.is_writable() {
return self.obj.as_mut(); 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 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()); assert!(self.is_writable());
self.obj.as_mut() self.obj.as_mut()