Use ptr::NonNull in various places

This commit is contained in:
Sebastian Dröge 2018-04-01 11:29:15 +03:00
parent b6a80b59fa
commit fca0287dec
6 changed files with 113 additions and 73 deletions

View file

@ -50,7 +50,7 @@ impl Error for IteratorError {
// Implemented manually so that we can use generics for the item // Implemented manually so that we can use generics for the item
pub struct Iterator<T> { pub struct Iterator<T> {
iter: *mut ffi::GstIterator, iter: ptr::NonNull<ffi::GstIterator>,
borrowed: bool, borrowed: bool,
phantom: PhantomData<T>, phantom: PhantomData<T>,
} }
@ -492,7 +492,7 @@ impl<T> Drop for Iterator<T> {
fn drop(&mut self) { fn drop(&mut self) {
if !self.borrowed { if !self.borrowed {
unsafe { unsafe {
ffi::gst_iterator_free(self.iter); ffi::gst_iterator_free(self.iter.as_ptr());
} }
} }
} }
@ -545,7 +545,7 @@ impl<'a, T: 'static> glib::translate::ToGlibPtr<'a, *const ffi::GstIterator> for
type Storage = &'a Iterator<T>; type Storage = &'a Iterator<T>;
fn to_glib_none(&'a self) -> glib::translate::Stash<'a, *const ffi::GstIterator, Self> { fn to_glib_none(&'a self) -> glib::translate::Stash<'a, *const ffi::GstIterator, Self> {
glib::translate::Stash(self.iter, self) glib::translate::Stash(self.iter.as_ptr(), self)
} }
fn to_glib_full(&self) -> *const ffi::GstIterator { fn to_glib_full(&self) -> *const ffi::GstIterator {
@ -561,7 +561,7 @@ impl<'a, T: 'static> glib::translate::ToGlibPtrMut<'a, *mut ffi::GstIterator> fo
fn to_glib_none_mut( fn to_glib_none_mut(
&'a mut self, &'a mut self,
) -> glib::translate::StashMut<'a, *mut ffi::GstIterator, Self> { ) -> glib::translate::StashMut<'a, *mut ffi::GstIterator, Self> {
glib::translate::StashMut(self.iter, self) glib::translate::StashMut(self.iter.as_ptr(), self)
} }
} }
@ -593,12 +593,13 @@ impl<T: StaticType> glib::translate::FromGlibPtrNone<*mut ffi::GstIterator> for
impl<T: StaticType> glib::translate::FromGlibPtrBorrow<*mut ffi::GstIterator> for Iterator<T> { impl<T: StaticType> glib::translate::FromGlibPtrBorrow<*mut ffi::GstIterator> for Iterator<T> {
#[inline] #[inline]
unsafe fn from_glib_borrow(ptr: *mut ffi::GstIterator) -> Self { unsafe fn from_glib_borrow(ptr: *mut ffi::GstIterator) -> Self {
assert!(!ptr.is_null());
assert_ne!( assert_ne!(
gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().to_glib()), gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().to_glib()),
glib_ffi::GFALSE glib_ffi::GFALSE
); );
Self { Self {
iter: ptr, iter: ptr::NonNull::new_unchecked(ptr),
borrowed: true, borrowed: true,
phantom: PhantomData, phantom: PhantomData,
} }
@ -609,12 +610,13 @@ impl<T: StaticType> glib::translate::FromGlibPtrBorrow<*mut ffi::GstIterator> fo
impl<T: StaticType> glib::translate::FromGlibPtrFull<*mut ffi::GstIterator> for Iterator<T> { impl<T: StaticType> glib::translate::FromGlibPtrFull<*mut ffi::GstIterator> for Iterator<T> {
#[inline] #[inline]
unsafe fn from_glib_full(ptr: *mut ffi::GstIterator) -> Self { unsafe fn from_glib_full(ptr: *mut ffi::GstIterator) -> Self {
assert!(!ptr.is_null());
assert_ne!( assert_ne!(
gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().to_glib()), gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().to_glib()),
glib_ffi::GFALSE glib_ffi::GFALSE
); );
Self { Self {
iter: ptr, iter: ptr::NonNull::new_unchecked(ptr),
borrowed: false, borrowed: false,
phantom: PhantomData, phantom: PhantomData,
} }

View file

@ -19,7 +19,7 @@ use glib::IsA;
use glib::translate::{from_glib, ToGlib, ToGlibPtr}; use glib::translate::{from_glib, ToGlib, ToGlibPtr};
#[derive(PartialEq, Eq, Clone, Copy)] #[derive(PartialEq, Eq, Clone, Copy)]
pub struct DebugCategory(*mut ffi::GstDebugCategory); pub struct DebugCategory(ptr::NonNull<ffi::GstDebugCategory>);
impl DebugCategory { impl DebugCategory {
pub fn new<'a, P: Into<Option<&'a str>>>( pub fn new<'a, P: Into<Option<&'a str>>>(
@ -38,11 +38,13 @@ impl DebugCategory {
// Gets the category if it exists already // Gets the category if it exists already
unsafe { unsafe {
DebugCategory(_gst_debug_category_new( let ptr = _gst_debug_category_new(
name.to_glib_none().0, name.to_glib_none().0,
color.to_glib(), color.to_glib(),
description.to_glib_none().0, description.to_glib_none().0,
)) );
assert!(!ptr.is_null());
DebugCategory(ptr::NonNull::new_unchecked(ptr))
} }
} }
@ -57,34 +59,34 @@ impl DebugCategory {
if cat.is_null() { if cat.is_null() {
None None
} else { } else {
Some(DebugCategory(cat)) Some(DebugCategory(ptr::NonNull::new_unchecked(cat)))
} }
} }
} }
pub fn get_threshold(&self) -> ::DebugLevel { pub fn get_threshold(&self) -> ::DebugLevel {
from_glib(unsafe { ffi::gst_debug_category_get_threshold(self.0) }) from_glib(unsafe { ffi::gst_debug_category_get_threshold(self.0.as_ptr()) })
} }
pub fn set_threshold(&self, threshold: ::DebugLevel) { pub fn set_threshold(&self, threshold: ::DebugLevel) {
unsafe { ffi::gst_debug_category_set_threshold(self.0, threshold.to_glib()) } unsafe { ffi::gst_debug_category_set_threshold(self.0.as_ptr(), threshold.to_glib()) }
} }
pub fn reset_threshold(&self) { pub fn reset_threshold(&self) {
unsafe { ffi::gst_debug_category_reset_threshold(self.0) } unsafe { ffi::gst_debug_category_reset_threshold(self.0.as_ptr()) }
} }
pub fn get_color(&self) -> ::DebugColorFlags { pub fn get_color(&self) -> ::DebugColorFlags {
unsafe { unsafe {
from_glib(mem::transmute::<u32, ffi::GstDebugColorFlags>( from_glib(mem::transmute::<u32, ffi::GstDebugColorFlags>(
ffi::gst_debug_category_get_color(self.0), ffi::gst_debug_category_get_color(self.0.as_ptr()),
)) ))
} }
} }
pub fn get_name(&self) -> &str { pub fn get_name(&self) -> &str {
unsafe { unsafe {
CStr::from_ptr(ffi::gst_debug_category_get_name(self.0)) CStr::from_ptr(ffi::gst_debug_category_get_name(self.0.as_ptr()))
.to_str() .to_str()
.unwrap() .unwrap()
} }
@ -92,7 +94,7 @@ impl DebugCategory {
pub fn get_description(&self) -> Option<&str> { pub fn get_description(&self) -> Option<&str> {
unsafe { unsafe {
let ptr = ffi::gst_debug_category_get_name(self.0); let ptr = ffi::gst_debug_category_get_name(self.0.as_ptr());
if ptr.is_null() { if ptr.is_null() {
None None
@ -113,7 +115,7 @@ impl DebugCategory {
args: fmt::Arguments, args: fmt::Arguments,
) { ) {
unsafe { unsafe {
if level.to_glib() as i32 > (*self.0).threshold { if level.to_glib() as i32 > self.0.as_ref().threshold {
return; return;
} }
} }
@ -125,7 +127,7 @@ impl DebugCategory {
unsafe { unsafe {
ffi::gst_debug_log( ffi::gst_debug_log(
self.0, self.0.as_ptr(),
level.to_glib(), level.to_glib(),
file.to_glib_none().0, file.to_glib_none().0,
module.to_glib_none().0, module.to_glib_none().0,

View file

@ -22,7 +22,7 @@ use glib::translate::{c_ptr_array_len, from_glib, from_glib_full, from_glib_none
use glib; use glib;
pub struct GstRc<T: MiniObject> { pub struct GstRc<T: MiniObject> {
obj: *mut T, obj: ptr::NonNull<T>,
borrowed: bool, borrowed: bool,
phantom: PhantomData<T>, phantom: PhantomData<T>,
} }
@ -34,7 +34,7 @@ impl<T: MiniObject> GstRc<T> {
ffi::gst_mini_object_ref(ptr as *mut ffi::GstMiniObject); ffi::gst_mini_object_ref(ptr as *mut ffi::GstMiniObject);
GstRc { GstRc {
obj: T::from_mut_ptr(ptr as *mut T::GstType) as *mut T, obj: ptr::NonNull::new_unchecked(T::from_mut_ptr(ptr as *mut T::GstType) as *mut T),
borrowed: false, borrowed: false,
phantom: PhantomData, phantom: PhantomData,
} }
@ -44,7 +44,7 @@ impl<T: MiniObject> GstRc<T> {
assert!(!ptr.is_null()); assert!(!ptr.is_null());
GstRc { GstRc {
obj: T::from_mut_ptr(ptr as *mut T::GstType) as *mut T, obj: ptr::NonNull::new_unchecked(T::from_mut_ptr(ptr as *mut T::GstType) as *mut T),
borrowed: false, borrowed: false,
phantom: PhantomData, phantom: PhantomData,
} }
@ -54,7 +54,7 @@ impl<T: MiniObject> GstRc<T> {
assert!(!ptr.is_null()); assert!(!ptr.is_null());
GstRc { GstRc {
obj: T::from_mut_ptr(ptr as *mut T::GstType) as *mut T, obj: ptr::NonNull::new_unchecked(T::from_mut_ptr(ptr as *mut T::GstType) as *mut T),
borrowed: true, borrowed: true,
phantom: PhantomData, phantom: PhantomData,
} }
@ -63,22 +63,23 @@ impl<T: MiniObject> GstRc<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 &mut *self.obj; return self.obj.as_mut();
} }
self.obj = T::from_mut_ptr( let ptr = T::from_mut_ptr(
ffi::gst_mini_object_make_writable(self.as_mut_ptr() as *mut ffi::GstMiniObject) ffi::gst_mini_object_make_writable(self.as_mut_ptr() as *mut ffi::GstMiniObject)
as *mut T::GstType, as *mut T::GstType,
); );
self.obj = ptr::NonNull::new_unchecked(ptr);
assert!(self.is_writable()); assert!(self.is_writable());
&mut *self.obj self.obj.as_mut()
} }
} }
pub fn get_mut(&mut self) -> Option<&mut T> { pub fn get_mut(&mut self) -> Option<&mut T> {
if self.is_writable() { if self.is_writable() {
Some(unsafe { &mut *self.obj }) Some(unsafe { self.obj.as_mut() })
} else { } else {
None None
} }
@ -107,7 +108,7 @@ impl<T: MiniObject> ops::Deref for GstRc<T> {
impl<T: MiniObject> AsRef<T> for GstRc<T> { impl<T: MiniObject> AsRef<T> for GstRc<T> {
fn as_ref(&self) -> &T { fn as_ref(&self) -> &T {
unsafe { &*self.obj } unsafe { self.obj.as_ref() }
} }
} }

View file

@ -15,12 +15,14 @@ use gobject_ffi;
use glib; use glib;
use glib::translate::{from_glib_full, FromGlibPtrNone, ToGlibPtr, ToGlibPtrMut}; use glib::translate::{from_glib_full, FromGlibPtrNone, ToGlibPtr, ToGlibPtrMut};
use std::ptr;
#[repr(C)] #[repr(C)]
pub struct StaticCaps(*mut ffi::GstStaticCaps); pub struct StaticCaps(ptr::NonNull<ffi::GstStaticCaps>);
impl StaticCaps { impl StaticCaps {
pub fn get(&self) -> Caps { pub fn get(&self) -> Caps {
unsafe { from_glib_full(ffi::gst_static_caps_get(self.0)) } unsafe { from_glib_full(ffi::gst_static_caps_get(self.0.as_ptr())) }
} }
} }
@ -72,7 +74,7 @@ impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstStaticCaps> for StaticCap
type Storage = &'a StaticCaps; type Storage = &'a StaticCaps;
fn to_glib_none(&'a self) -> glib::translate::Stash<'a, *const ffi::GstStaticCaps, Self> { fn to_glib_none(&'a self) -> glib::translate::Stash<'a, *const ffi::GstStaticCaps, Self> {
glib::translate::Stash(self.0, self) glib::translate::Stash(self.0.as_ptr(), self)
} }
fn to_glib_full(&self) -> *const ffi::GstStaticCaps { fn to_glib_full(&self) -> *const ffi::GstStaticCaps {
@ -84,7 +86,8 @@ impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstStaticCaps> for StaticCap
impl glib::translate::FromGlibPtrNone<*const ffi::GstStaticCaps> for StaticCaps { impl glib::translate::FromGlibPtrNone<*const ffi::GstStaticCaps> for StaticCaps {
#[inline] #[inline]
unsafe fn from_glib_none(ptr: *const ffi::GstStaticCaps) -> Self { unsafe fn from_glib_none(ptr: *const ffi::GstStaticCaps) -> Self {
StaticCaps(ptr as *mut _) assert!(!ptr.is_null());
StaticCaps(ptr::NonNull::new_unchecked(ptr as *mut _))
} }
} }
@ -92,7 +95,8 @@ impl glib::translate::FromGlibPtrNone<*const ffi::GstStaticCaps> for StaticCaps
impl glib::translate::FromGlibPtrNone<*mut ffi::GstStaticCaps> for StaticCaps { impl glib::translate::FromGlibPtrNone<*mut ffi::GstStaticCaps> for StaticCaps {
#[inline] #[inline]
unsafe fn from_glib_none(ptr: *mut ffi::GstStaticCaps) -> Self { unsafe fn from_glib_none(ptr: *mut ffi::GstStaticCaps) -> Self {
StaticCaps(ptr) assert!(!ptr.is_null());
StaticCaps(ptr::NonNull::new_unchecked(ptr))
} }
} }
@ -100,7 +104,8 @@ impl glib::translate::FromGlibPtrNone<*mut ffi::GstStaticCaps> for StaticCaps {
impl glib::translate::FromGlibPtrBorrow<*mut ffi::GstStaticCaps> for StaticCaps { impl glib::translate::FromGlibPtrBorrow<*mut ffi::GstStaticCaps> for StaticCaps {
#[inline] #[inline]
unsafe fn from_glib_borrow(ptr: *mut ffi::GstStaticCaps) -> Self { unsafe fn from_glib_borrow(ptr: *mut ffi::GstStaticCaps) -> Self {
StaticCaps(ptr) assert!(!ptr.is_null());
StaticCaps(ptr::NonNull::new_unchecked(ptr))
} }
} }

View file

@ -17,28 +17,34 @@ use std::ffi::CStr;
use glib; use glib;
use glib::translate::{from_glib, from_glib_full, FromGlibPtrNone, ToGlibPtr, ToGlibPtrMut}; use glib::translate::{from_glib, from_glib_full, FromGlibPtrNone, ToGlibPtr, ToGlibPtrMut};
use std::ptr;
#[repr(C)] #[repr(C)]
pub struct StaticPadTemplate(*mut ffi::GstStaticPadTemplate); pub struct StaticPadTemplate(ptr::NonNull<ffi::GstStaticPadTemplate>);
impl StaticPadTemplate { impl StaticPadTemplate {
pub fn get(&self) -> PadTemplate { pub fn get(&self) -> PadTemplate {
unsafe { from_glib_full(ffi::gst_static_pad_template_get(self.0)) } unsafe { from_glib_full(ffi::gst_static_pad_template_get(self.0.as_ptr())) }
} }
pub fn get_caps(&self) -> Caps { pub fn get_caps(&self) -> Caps {
unsafe { from_glib_full(ffi::gst_static_pad_template_get_caps(self.0)) } unsafe { from_glib_full(ffi::gst_static_pad_template_get_caps(self.0.as_ptr())) }
} }
pub fn name_template<'a>(&self) -> &'a str { pub fn name_template<'a>(&self) -> &'a str {
unsafe { CStr::from_ptr((*self.0).name_template).to_str().unwrap() } unsafe {
CStr::from_ptr(self.0.as_ref().name_template)
.to_str()
.unwrap()
}
} }
pub fn direction(&self) -> ::PadDirection { pub fn direction(&self) -> ::PadDirection {
unsafe { from_glib((*self.0).direction) } unsafe { from_glib(self.0.as_ref().direction) }
} }
pub fn presence(&self) -> ::PadPresence { pub fn presence(&self) -> ::PadPresence {
unsafe { from_glib((*self.0).presence) } unsafe { from_glib(self.0.as_ref().presence) }
} }
} }
@ -94,7 +100,7 @@ impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstStaticPadTemplate> for St
fn to_glib_none( fn to_glib_none(
&'a self, &'a self,
) -> glib::translate::Stash<'a, *const ffi::GstStaticPadTemplate, Self> { ) -> glib::translate::Stash<'a, *const ffi::GstStaticPadTemplate, Self> {
glib::translate::Stash(self.0, self) glib::translate::Stash(self.0.as_ptr(), self)
} }
fn to_glib_full(&self) -> *const ffi::GstStaticPadTemplate { fn to_glib_full(&self) -> *const ffi::GstStaticPadTemplate {
@ -106,7 +112,8 @@ impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstStaticPadTemplate> for St
impl glib::translate::FromGlibPtrNone<*const ffi::GstStaticPadTemplate> for StaticPadTemplate { impl glib::translate::FromGlibPtrNone<*const ffi::GstStaticPadTemplate> for StaticPadTemplate {
#[inline] #[inline]
unsafe fn from_glib_none(ptr: *const ffi::GstStaticPadTemplate) -> Self { unsafe fn from_glib_none(ptr: *const ffi::GstStaticPadTemplate) -> Self {
StaticPadTemplate(ptr as *mut _) assert!(!ptr.is_null());
StaticPadTemplate(ptr::NonNull::new_unchecked(ptr as *mut _))
} }
} }
@ -114,7 +121,8 @@ impl glib::translate::FromGlibPtrNone<*const ffi::GstStaticPadTemplate> for Stat
impl glib::translate::FromGlibPtrNone<*mut ffi::GstStaticPadTemplate> for StaticPadTemplate { impl glib::translate::FromGlibPtrNone<*mut ffi::GstStaticPadTemplate> for StaticPadTemplate {
#[inline] #[inline]
unsafe fn from_glib_none(ptr: *mut ffi::GstStaticPadTemplate) -> Self { unsafe fn from_glib_none(ptr: *mut ffi::GstStaticPadTemplate) -> Self {
StaticPadTemplate(ptr) assert!(!ptr.is_null());
StaticPadTemplate(ptr::NonNull::new_unchecked(ptr))
} }
} }
@ -122,7 +130,8 @@ impl glib::translate::FromGlibPtrNone<*mut ffi::GstStaticPadTemplate> for Static
impl glib::translate::FromGlibPtrBorrow<*mut ffi::GstStaticPadTemplate> for StaticPadTemplate { impl glib::translate::FromGlibPtrBorrow<*mut ffi::GstStaticPadTemplate> for StaticPadTemplate {
#[inline] #[inline]
unsafe fn from_glib_borrow(ptr: *mut ffi::GstStaticPadTemplate) -> Self { unsafe fn from_glib_borrow(ptr: *mut ffi::GstStaticPadTemplate) -> Self {
StaticPadTemplate(ptr) assert!(!ptr.is_null());
StaticPadTemplate(ptr::NonNull::new_unchecked(ptr))
} }
} }

View file

@ -25,7 +25,7 @@ use ffi;
use glib_ffi::gpointer; use glib_ffi::gpointer;
use gobject_ffi; use gobject_ffi;
pub struct Structure(*mut StructureRef, PhantomData<StructureRef>); pub struct Structure(ptr::NonNull<StructureRef>, PhantomData<StructureRef>);
unsafe impl Send for Structure {} unsafe impl Send for Structure {}
impl Structure { impl Structure {
@ -36,10 +36,11 @@ impl Structure {
pub fn new_empty(name: &str) -> Structure { pub fn new_empty(name: &str) -> Structure {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
Structure( unsafe {
unsafe { ffi::gst_structure_new_empty(name.to_glib_none().0) as *mut StructureRef }, let ptr = ffi::gst_structure_new_empty(name.to_glib_none().0) as *mut StructureRef;
PhantomData, assert!(!ptr.is_null());
) Structure(ptr::NonNull::new_unchecked(ptr), PhantomData)
}
} }
pub fn new(name: &str, values: &[(&str, &ToSendValue)]) -> Structure { pub fn new(name: &str, values: &[(&str, &ToSendValue)]) -> Structure {
@ -60,13 +61,16 @@ impl Structure {
if structure.is_null() { if structure.is_null() {
None None
} else { } else {
Some(Structure(structure as *mut StructureRef, PhantomData)) Some(Structure(
ptr::NonNull::new_unchecked(structure as *mut StructureRef),
PhantomData,
))
} }
} }
} }
pub unsafe fn into_ptr(self) -> *mut ffi::GstStructure { pub unsafe fn into_ptr(self) -> *mut ffi::GstStructure {
let ptr = self.0 as *mut StructureRef as *mut ffi::GstStructure; let ptr = self.0.as_ptr() as *mut StructureRef as *mut ffi::GstStructure;
mem::forget(self); mem::forget(self);
ptr ptr
@ -77,13 +81,13 @@ impl Deref for Structure {
type Target = StructureRef; type Target = StructureRef;
fn deref(&self) -> &StructureRef { fn deref(&self) -> &StructureRef {
unsafe { &*self.0 } unsafe { self.0.as_ref() }
} }
} }
impl DerefMut for Structure { impl DerefMut for Structure {
fn deref_mut(&mut self) -> &mut StructureRef { fn deref_mut(&mut self) -> &mut StructureRef {
unsafe { &mut *self.0 } unsafe { self.0.as_mut() }
} }
} }
@ -101,16 +105,17 @@ impl AsMut<StructureRef> for Structure {
impl Clone for Structure { impl Clone for Structure {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Structure( unsafe {
unsafe { ffi::gst_structure_copy(&(*self.0).0) as *mut StructureRef }, let ptr = ffi::gst_structure_copy(&self.0.as_ref().0) as *mut StructureRef;
PhantomData, assert!(!ptr.is_null());
) Structure(ptr::NonNull::new_unchecked(ptr), PhantomData)
}
} }
} }
impl Drop for Structure { impl Drop for Structure {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { ffi::gst_structure_free(&mut (*self.0).0) } unsafe { ffi::gst_structure_free(&mut self.0.as_mut().0) }
} }
} }
@ -151,13 +156,13 @@ impl str::FromStr for Structure {
impl Borrow<StructureRef> for Structure { impl Borrow<StructureRef> for Structure {
fn borrow(&self) -> &StructureRef { fn borrow(&self) -> &StructureRef {
unsafe { &*self.0 } unsafe { self.0.as_ref() }
} }
} }
impl BorrowMut<StructureRef> for Structure { impl BorrowMut<StructureRef> for Structure {
fn borrow_mut(&mut self) -> &mut StructureRef { fn borrow_mut(&mut self) -> &mut StructureRef {
unsafe { &mut *self.0 } unsafe { self.0.as_mut() }
} }
} }
@ -165,10 +170,11 @@ impl ToOwned for StructureRef {
type Owned = Structure; type Owned = Structure;
fn to_owned(&self) -> Structure { fn to_owned(&self) -> Structure {
Structure( unsafe {
unsafe { ffi::gst_structure_copy(&self.0) as *mut StructureRef }, let ptr = ffi::gst_structure_copy(&self.0) as *mut StructureRef;
PhantomData, assert!(!ptr.is_null());
) Structure(ptr::NonNull::new_unchecked(ptr), PhantomData)
}
} }
} }
@ -182,11 +188,11 @@ impl<'a> ToGlibPtr<'a, *const ffi::GstStructure> for Structure {
type Storage = &'a Self; type Storage = &'a Self;
fn to_glib_none(&'a self) -> Stash<'a, *const ffi::GstStructure, Self> { fn to_glib_none(&'a self) -> Stash<'a, *const ffi::GstStructure, Self> {
Stash(unsafe { &(*self.0).0 }, self) unsafe { Stash(&self.0.as_ref().0, self) }
} }
fn to_glib_full(&self) -> *const ffi::GstStructure { fn to_glib_full(&self) -> *const ffi::GstStructure {
unsafe { ffi::gst_structure_copy(&(*self.0).0) } unsafe { ffi::gst_structure_copy(&self.0.as_ref().0) }
} }
} }
@ -194,11 +200,11 @@ impl<'a> ToGlibPtr<'a, *mut ffi::GstStructure> for Structure {
type Storage = &'a Self; type Storage = &'a Self;
fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::GstStructure, Self> { fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::GstStructure, Self> {
Stash(unsafe { &mut (*self.0).0 }, self) unsafe { Stash(&self.0.as_ref().0 as *const _ as *mut _, self) }
} }
fn to_glib_full(&self) -> *mut ffi::GstStructure { fn to_glib_full(&self) -> *mut ffi::GstStructure {
unsafe { ffi::gst_structure_copy(&(*self.0).0) } unsafe { ffi::gst_structure_copy(&self.0.as_ref().0) }
} }
} }
@ -206,14 +212,17 @@ impl<'a> ToGlibPtrMut<'a, *mut ffi::GstStructure> for Structure {
type Storage = &'a mut Self; type Storage = &'a mut Self;
fn to_glib_none_mut(&'a mut self) -> StashMut<*mut ffi::GstStructure, Self> { fn to_glib_none_mut(&'a mut self) -> StashMut<*mut ffi::GstStructure, Self> {
StashMut(unsafe { &mut (*self.0).0 }, self) unsafe { StashMut(&mut self.0.as_mut().0, self) }
} }
} }
impl FromGlibPtrNone<*const ffi::GstStructure> for Structure { impl FromGlibPtrNone<*const ffi::GstStructure> for Structure {
unsafe fn from_glib_none(ptr: *const ffi::GstStructure) -> Self { unsafe fn from_glib_none(ptr: *const ffi::GstStructure) -> Self {
assert!(!ptr.is_null());
let ptr = ffi::gst_structure_copy(ptr);
assert!(!ptr.is_null());
Structure( Structure(
ffi::gst_structure_copy(ptr) as *mut StructureRef, ptr::NonNull::new_unchecked(ptr as *mut StructureRef),
PhantomData, PhantomData,
) )
} }
@ -221,8 +230,11 @@ impl FromGlibPtrNone<*const ffi::GstStructure> for Structure {
impl FromGlibPtrNone<*mut ffi::GstStructure> for Structure { impl FromGlibPtrNone<*mut ffi::GstStructure> for Structure {
unsafe fn from_glib_none(ptr: *mut ffi::GstStructure) -> Self { unsafe fn from_glib_none(ptr: *mut ffi::GstStructure) -> Self {
assert!(!ptr.is_null());
let ptr = ffi::gst_structure_copy(ptr);
assert!(!ptr.is_null());
Structure( Structure(
ffi::gst_structure_copy(ptr) as *mut StructureRef, ptr::NonNull::new_unchecked(ptr as *mut StructureRef),
PhantomData, PhantomData,
) )
} }
@ -230,26 +242,35 @@ impl FromGlibPtrNone<*mut ffi::GstStructure> for Structure {
impl FromGlibPtrFull<*const ffi::GstStructure> for Structure { impl FromGlibPtrFull<*const ffi::GstStructure> for Structure {
unsafe fn from_glib_full(ptr: *const ffi::GstStructure) -> Self { unsafe fn from_glib_full(ptr: *const ffi::GstStructure) -> Self {
Structure(ptr as *mut StructureRef, PhantomData) assert!(!ptr.is_null());
Structure(
ptr::NonNull::new_unchecked(ptr as *mut StructureRef),
PhantomData,
)
} }
} }
impl FromGlibPtrFull<*mut ffi::GstStructure> for Structure { impl FromGlibPtrFull<*mut ffi::GstStructure> for Structure {
unsafe fn from_glib_full(ptr: *mut ffi::GstStructure) -> Self { unsafe fn from_glib_full(ptr: *mut ffi::GstStructure) -> Self {
Structure(ptr as *mut StructureRef, PhantomData) assert!(!ptr.is_null());
Structure(
ptr::NonNull::new_unchecked(ptr as *mut StructureRef),
PhantomData,
)
} }
} }
impl<'a> glib::value::FromValueOptional<'a> for Structure { impl<'a> glib::value::FromValueOptional<'a> for Structure {
unsafe fn from_value_optional(v: &'a glib::Value) -> Option<Self> { unsafe fn from_value_optional(v: &'a glib::Value) -> Option<Self> {
let ptr = gobject_ffi::g_value_get_boxed(v.to_glib_none().0); let ptr = gobject_ffi::g_value_get_boxed(v.to_glib_none().0);
assert!(!ptr.is_null());
from_glib_none(ptr as *const ffi::GstStructure) from_glib_none(ptr as *const ffi::GstStructure)
} }
} }
impl glib::value::SetValue for Structure { impl glib::value::SetValue for Structure {
unsafe fn set_value(v: &mut glib::Value, s: &Self) { unsafe fn set_value(v: &mut glib::Value, s: &Self) {
gobject_ffi::g_value_set_boxed(v.to_glib_none_mut().0, s.0 as gpointer); gobject_ffi::g_value_set_boxed(v.to_glib_none_mut().0, s.0.as_ptr() as gpointer);
} }
} }