Use from_glib_none/full directly on potential NULL pointers if Option<_> is returned

The implementation for Option<_> is doing exactly what we did manually
here.
This commit is contained in:
Sebastian Dröge 2017-12-16 15:07:26 +02:00
parent 37ab39282c
commit 18f99073c1
3 changed files with 22 additions and 76 deletions

View file

@ -57,11 +57,12 @@ impl GstRc<BufferRef> {
pub fn with_size(size: usize) -> Option<Self> {
assert_initialized_main_thread!();
let raw = unsafe { ffi::gst_buffer_new_allocate(ptr::null_mut(), size, ptr::null_mut()) };
if raw.is_null() {
None
} else {
Some(unsafe { from_glib_full(raw) })
unsafe {
from_glib_full(ffi::gst_buffer_new_allocate(
ptr::null_mut(),
size,
ptr::null_mut(),
))
}
}
@ -73,14 +74,14 @@ impl GstRc<BufferRef> {
pub fn from_mut_slice<T: AsMut<[u8]> + Send + 'static>(slice: T) -> Option<Self> {
assert_initialized_main_thread!();
let raw = unsafe {
unsafe {
let mut b = Box::new(slice);
let (size, data) = {
let slice = (*b).as_mut();
(slice.len(), slice.as_mut_ptr())
};
let user_data = Box::into_raw(b);
ffi::gst_buffer_new_wrapped_full(
from_glib_full(ffi::gst_buffer_new_wrapped_full(
ffi::GstMemoryFlags::empty(),
data as glib_ffi::gpointer,
size,
@ -88,27 +89,21 @@ impl GstRc<BufferRef> {
size,
user_data as glib_ffi::gpointer,
Some(Self::drop_box::<T>),
)
};
if raw.is_null() {
None
} else {
Some(unsafe { from_glib_full(raw) })
))
}
}
pub fn from_slice<T: AsRef<[u8]> + Send + 'static>(slice: T) -> Option<Self> {
assert_initialized_main_thread!();
let raw = unsafe {
unsafe {
let b = Box::new(slice);
let (size, data) = {
let slice = (*b).as_ref();
(slice.len(), slice.as_ptr())
};
let user_data = Box::into_raw(b);
ffi::gst_buffer_new_wrapped_full(
from_glib_full(ffi::gst_buffer_new_wrapped_full(
ffi::GST_MEMORY_FLAG_READONLY,
data as glib_ffi::gpointer,
size,
@ -116,13 +111,7 @@ impl GstRc<BufferRef> {
size,
user_data as glib_ffi::gpointer,
Some(Self::drop_box::<T>),
)
};
if raw.is_null() {
None
} else {
Some(unsafe { from_glib_full(raw) })
))
}
}
@ -212,19 +201,13 @@ impl BufferRef {
pub fn copy_region(&self, offset: usize, size: Option<usize>) -> Option<Buffer> {
let size_real = size.unwrap_or(usize::MAX);
let ptr = unsafe {
ffi::gst_buffer_copy_region(
unsafe {
from_glib_full(ffi::gst_buffer_copy_region(
self.as_mut_ptr(),
ffi::GST_BUFFER_COPY_ALL,
offset,
size_real,
)
};
if ptr.is_null() {
None
} else {
Some(unsafe { from_glib_full(ptr) })
))
}
}

View file

@ -55,15 +55,7 @@ impl GstRc<CapsRef> {
pub fn from_string(value: &str) -> Option<Self> {
assert_initialized_main_thread!();
unsafe {
let caps_ptr = ffi::gst_caps_from_string(value.to_glib_none().0);
if caps_ptr.is_null() {
None
} else {
Some(from_glib_full(caps_ptr))
}
}
unsafe { from_glib_full(ffi::gst_caps_from_string(value.to_glib_none().0)) }
}
pub fn fixate(caps: Self) -> Self {

View file

@ -12,7 +12,8 @@ use std::mem;
use ffi;
use glib;
use glib::translate::{from_glib, from_glib_full, FromGlibPtrContainer, ToGlib, ToGlibPtr};
use glib::translate::{from_glib, from_glib_full, from_glib_none, FromGlibPtrContainer, ToGlib,
ToGlibPtr};
use miniobject::*;
use TocEntryType;
@ -41,16 +42,7 @@ impl TocRef {
}
pub fn find_entry(&self, uid: &str) -> Option<TocEntry> {
unsafe {
let toc_entry = ffi::gst_toc_find_entry(self.as_ptr(), uid.to_glib_none().0);
if toc_entry.is_null() {
return None;
}
Some(TocEntry::from_glib_none(
toc_entry as *const ffi::GstTocEntry,
))
}
unsafe { from_glib_none(ffi::gst_toc_find_entry(self.as_ptr(), uid.to_glib_none().0)) }
}
pub fn get_entries(&self) -> Vec<TocEntry> {
@ -64,14 +56,7 @@ impl TocRef {
}
pub fn get_tags(&self) -> Option<TagList> {
unsafe {
let tags = ffi::gst_toc_get_tags(self.as_ptr());
if tags.is_null() {
return None;
}
Some(TagList::from_glib_none(tags as *const ffi::GstTagList))
}
unsafe { from_glib_none(ffi::gst_toc_get_tags(self.as_ptr())) }
}
pub fn set_tags(&mut self, tag_list: TagList) {
@ -158,14 +143,7 @@ impl TocEntryRef {
}
pub fn get_parent(&self) -> Option<TocEntry> {
unsafe {
let parent = ffi::gst_toc_entry_get_parent(self.as_mut_ptr());
if parent.is_null() {
return None;
}
Some(TocEntry::from_glib_none(parent as *const ffi::GstTocEntry))
}
unsafe { from_glib_none(ffi::gst_toc_entry_get_parent(self.as_mut_ptr())) }
}
pub fn get_start_stop_times(&self) -> Option<(i64, i64)> {
@ -192,14 +170,7 @@ impl TocEntryRef {
}
pub fn get_tags(&self) -> Option<TagList> {
unsafe {
let tags = ffi::gst_toc_entry_get_tags(self.as_ptr());
if tags.is_null() {
return None;
}
Some(TagList::from_glib_none(tags as *const ffi::GstTagList))
}
unsafe { from_glib_none(ffi::gst_toc_entry_get_tags(self.as_ptr())) }
}
pub fn set_tags(&mut self, tag_list: TagList) {