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

View file

@ -12,7 +12,8 @@ use std::mem;
use ffi; use ffi;
use glib; 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 miniobject::*;
use TocEntryType; use TocEntryType;
@ -41,16 +42,7 @@ impl TocRef {
} }
pub fn find_entry(&self, uid: &str) -> Option<TocEntry> { pub fn find_entry(&self, uid: &str) -> Option<TocEntry> {
unsafe { unsafe { from_glib_none(ffi::gst_toc_find_entry(self.as_ptr(), uid.to_glib_none().0)) }
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,
))
}
} }
pub fn get_entries(&self) -> Vec<TocEntry> { pub fn get_entries(&self) -> Vec<TocEntry> {
@ -64,14 +56,7 @@ impl TocRef {
} }
pub fn get_tags(&self) -> Option<TagList> { pub fn get_tags(&self) -> Option<TagList> {
unsafe { unsafe { from_glib_none(ffi::gst_toc_get_tags(self.as_ptr())) }
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))
}
} }
pub fn set_tags(&mut self, tag_list: TagList) { pub fn set_tags(&mut self, tag_list: TagList) {
@ -158,14 +143,7 @@ impl TocEntryRef {
} }
pub fn get_parent(&self) -> Option<TocEntry> { pub fn get_parent(&self) -> Option<TocEntry> {
unsafe { unsafe { from_glib_none(ffi::gst_toc_entry_get_parent(self.as_mut_ptr())) }
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))
}
} }
pub fn get_start_stop_times(&self) -> Option<(i64, i64)> { pub fn get_start_stop_times(&self) -> Option<(i64, i64)> {
@ -192,14 +170,7 @@ impl TocEntryRef {
} }
pub fn get_tags(&self) -> Option<TagList> { pub fn get_tags(&self) -> Option<TagList> {
unsafe { unsafe { from_glib_none(ffi::gst_toc_entry_get_tags(self.as_ptr())) }
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))
}
} }
pub fn set_tags(&mut self, tag_list: TagList) { pub fn set_tags(&mut self, tag_list: TagList) {