as_ptr() and as_mut_ptr() accessors are safe

Nothing unsafe about getting a pointer, deferencing it is unsafe.
This commit is contained in:
Guillaume Desmottes 2022-08-11 16:02:02 +02:00
parent 4e221c1b48
commit 2dcd5cf9eb
15 changed files with 31 additions and 47 deletions

View file

@ -24,7 +24,7 @@ impl fmt::Debug for FdMemory {
impl fmt::Debug for FdMemoryRef { impl fmt::Debug for FdMemoryRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FdMemory") f.debug_struct("FdMemory")
.field("ptr", unsafe { &self.as_ptr() }) .field("ptr", &self.as_ptr())
.field("allocator", &self.allocator()) .field("allocator", &self.allocator())
.field("parent", &self.parent()) .field("parent", &self.parent())
.field("maxsize", &self.maxsize()) .field("maxsize", &self.maxsize())

View file

@ -22,7 +22,7 @@ impl fmt::Debug for PhysMemory {
impl fmt::Debug for PhysMemoryRef { impl fmt::Debug for PhysMemoryRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FdMemory") f.debug_struct("FdMemory")
.field("ptr", unsafe { &self.as_ptr() }) .field("ptr", &self.as_ptr())
.field("allocator", &self.allocator()) .field("allocator", &self.allocator())
.field("parent", &self.parent()) .field("parent", &self.parent())
.field("maxsize", &self.maxsize()) .field("maxsize", &self.maxsize())

View file

@ -416,11 +416,11 @@ impl<'a, T> RTPBuffer<'a, T> {
} }
} }
pub unsafe fn as_ptr(&self) -> *const ffi::GstRTPBuffer { pub fn as_ptr(&self) -> *const ffi::GstRTPBuffer {
&self.rtp_buffer as *const ffi::GstRTPBuffer &self.rtp_buffer as *const ffi::GstRTPBuffer
} }
pub unsafe fn as_mut_ptr(&self) -> *mut ffi::GstRTPBuffer { pub fn as_mut_ptr(&self) -> *mut ffi::GstRTPBuffer {
&self.rtp_buffer as *const ffi::GstRTPBuffer as *mut ffi::GstRTPBuffer &self.rtp_buffer as *const ffi::GstRTPBuffer as *mut ffi::GstRTPBuffer
} }
} }

View file

@ -138,7 +138,7 @@ pub struct NavigationEventMessage {
impl PartialEq for NavigationEventMessage { impl PartialEq for NavigationEventMessage {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
unsafe { self.event.as_ptr() == other.event.as_ptr() } self.event.as_ptr() == other.event.as_ptr()
} }
} }

View file

@ -1051,7 +1051,7 @@ impl fmt::Debug for BufferRef {
} }
f.debug_struct("Buffer") f.debug_struct("Buffer")
.field("ptr", unsafe { &self.as_ptr() }) .field("ptr", &self.as_ptr())
.field("pts", &self.pts().display()) .field("pts", &self.pts().display())
.field("dts", &self.dts().display()) .field("dts", &self.dts().display())
.field("duration", &self.duration().display()) .field("duration", &self.duration().display())
@ -1306,15 +1306,11 @@ mod tests {
let mut buffer2 = buffer.clone(); let mut buffer2 = buffer.clone();
assert_eq!(buffer.get_mut(), None); assert_eq!(buffer.get_mut(), None);
unsafe {
assert_eq!(buffer2.as_ptr(), buffer.as_ptr()); assert_eq!(buffer2.as_ptr(), buffer.as_ptr());
}
{ {
let buffer2 = buffer2.make_mut(); let buffer2 = buffer2.make_mut();
unsafe {
assert_ne!(buffer2.as_ptr(), buffer.as_ptr()); assert_ne!(buffer2.as_ptr(), buffer.as_ptr());
}
buffer2.set_pts(Some(2 * ClockTime::NSECOND)); buffer2.set_pts(Some(2 * ClockTime::NSECOND));

View file

@ -64,11 +64,11 @@ impl BufferPoolConfigRef {
&mut *(ptr as *mut StructureRef as *mut BufferPoolConfigRef) &mut *(ptr as *mut StructureRef as *mut BufferPoolConfigRef)
} }
pub unsafe fn as_ptr(&self) -> *const ffi::GstStructure { pub fn as_ptr(&self) -> *const ffi::GstStructure {
self as *const Self as *const ffi::GstStructure self as *const Self as *const ffi::GstStructure
} }
pub unsafe fn as_mut_ptr(&self) -> *mut ffi::GstStructure { pub fn as_mut_ptr(&self) -> *mut ffi::GstStructure {
self as *const Self as *mut ffi::GstStructure self as *const Self as *mut ffi::GstStructure
} }
} }

View file

@ -201,7 +201,7 @@ impl fmt::Debug for BufferListRef {
.unwrap_or((ClockTime::NONE, ClockTime::NONE)); .unwrap_or((ClockTime::NONE, ClockTime::NONE));
f.debug_struct("BufferList") f.debug_struct("BufferList")
.field("ptr", unsafe { &self.as_ptr() }) .field("ptr", &self.as_ptr())
.field("buffers", &self.len()) .field("buffers", &self.len())
.field("pts", &pts.display()) .field("pts", &pts.display())
.field("dts", &dts.display()) .field("dts", &dts.display())

View file

@ -304,11 +304,11 @@ impl CapsFeaturesRef {
&mut *(ptr as *mut CapsFeaturesRef) &mut *(ptr as *mut CapsFeaturesRef)
} }
pub unsafe fn as_ptr(&self) -> *const ffi::GstCapsFeatures { pub fn as_ptr(&self) -> *const ffi::GstCapsFeatures {
self as *const Self as *const ffi::GstCapsFeatures self as *const Self as *const ffi::GstCapsFeatures
} }
pub unsafe fn as_mut_ptr(&self) -> *mut ffi::GstCapsFeatures { pub fn as_mut_ptr(&self) -> *mut ffi::GstCapsFeatures {
self as *const Self as *mut ffi::GstCapsFeatures self as *const Self as *mut ffi::GstCapsFeatures
} }

View file

@ -271,7 +271,7 @@ impl fmt::Debug for Event {
impl fmt::Debug for EventRef { impl fmt::Debug for EventRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Event") f.debug_struct("Event")
.field("ptr", unsafe { &self.as_ptr() }) .field("ptr", &self.as_ptr())
.field("type", &self.type_().name()) .field("type", &self.type_().name())
.field("seqnum", &self.seqnum()) .field("seqnum", &self.seqnum())
.field("structure", &self.structure()) .field("structure", &self.structure())

View file

@ -38,7 +38,7 @@ impl fmt::Debug for Memory {
impl fmt::Debug for MemoryRef { impl fmt::Debug for MemoryRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Memory") f.debug_struct("Memory")
.field("ptr", unsafe { &self.as_ptr() }) .field("ptr", &self.as_ptr())
.field("allocator", &self.allocator()) .field("allocator", &self.allocator())
.field("parent", &self.parent()) .field("parent", &self.parent())
.field("maxsize", &self.maxsize()) .field("maxsize", &self.maxsize())

View file

@ -150,7 +150,7 @@ impl fmt::Debug for MessageRef {
}; };
f.debug_struct("Message") f.debug_struct("Message")
.field("ptr", unsafe { &self.as_ptr() }) .field("ptr", &self.as_ptr())
.field("type", &unsafe { .field("type", &unsafe {
let type_ = ffi::gst_message_type_get_name((*self.as_ptr()).type_); let type_ = ffi::gst_message_type_get_name((*self.as_ptr()).type_);
CStr::from_ptr(type_).to_str().unwrap() CStr::from_ptr(type_).to_str().unwrap()

View file

@ -681,10 +681,8 @@ mod tests {
let parent = crate::Buffer::new(); let parent = crate::Buffer::new();
{ {
let meta = ParentBufferMeta::add(buffer.get_mut().unwrap(), &parent); let meta = ParentBufferMeta::add(buffer.get_mut().unwrap(), &parent);
unsafe {
assert_eq!(meta.parent().as_ptr(), parent.as_ptr()); assert_eq!(meta.parent().as_ptr(), parent.as_ptr());
} }
}
{ {
let metas = buffer.iter_meta::<Meta>(); let metas = buffer.iter_meta::<Meta>();
@ -697,10 +695,8 @@ mod tests {
{ {
let metas = buffer.iter_meta::<ParentBufferMeta>().collect::<Vec<_>>(); let metas = buffer.iter_meta::<ParentBufferMeta>().collect::<Vec<_>>();
assert_eq!(metas.len(), 1); assert_eq!(metas.len(), 1);
unsafe {
assert_eq!(metas[0].parent().as_ptr(), parent.as_ptr()); assert_eq!(metas[0].parent().as_ptr(), parent.as_ptr());
} }
}
{ {
let metas = buffer let metas = buffer
.get_mut() .get_mut()
@ -708,10 +704,8 @@ mod tests {
.iter_meta_mut::<ParentBufferMeta>() .iter_meta_mut::<ParentBufferMeta>()
.collect::<Vec<_>>(); .collect::<Vec<_>>();
assert_eq!(metas.len(), 1); assert_eq!(metas.len(), 1);
unsafe {
assert_eq!(metas[0].parent().as_ptr(), parent.as_ptr()); assert_eq!(metas[0].parent().as_ptr(), parent.as_ptr());
} }
}
{ {
let meta = buffer let meta = buffer
@ -719,9 +713,7 @@ mod tests {
.unwrap() .unwrap()
.meta_mut::<ParentBufferMeta>() .meta_mut::<ParentBufferMeta>()
.unwrap(); .unwrap();
unsafe {
assert_eq!(meta.parent().as_ptr(), parent.as_ptr()); assert_eq!(meta.parent().as_ptr(), parent.as_ptr());
}
meta.remove().unwrap(); meta.remove().unwrap();
} }

View file

@ -149,7 +149,7 @@ macro_rules! mini_object_wrapper (
type Storage = &'a Self; type Storage = &'a Self;
fn to_glib_none(&'a self) -> $crate::glib::translate::Stash<'a, *const $ffi_name, Self> { fn to_glib_none(&'a self) -> $crate::glib::translate::Stash<'a, *const $ffi_name, Self> {
$crate::glib::translate::Stash(unsafe { self.as_ptr() }, self) $crate::glib::translate::Stash( self.as_ptr() , self)
} }
fn to_glib_full(&self) -> *const $ffi_name { fn to_glib_full(&self) -> *const $ffi_name {
@ -164,7 +164,7 @@ macro_rules! mini_object_wrapper (
type Storage = &'a Self; type Storage = &'a Self;
fn to_glib_none(&'a self) -> $crate::glib::translate::Stash<'a, *mut $ffi_name, Self> { fn to_glib_none(&'a self) -> $crate::glib::translate::Stash<'a, *mut $ffi_name, Self> {
$crate::glib::translate::Stash(unsafe { self.as_mut_ptr() }, self) $crate::glib::translate::Stash( self.as_mut_ptr() , self)
} }
fn to_glib_full(&self) -> *mut $ffi_name { fn to_glib_full(&self) -> *mut $ffi_name {
@ -180,7 +180,7 @@ macro_rules! mini_object_wrapper (
fn to_glib_none_mut(&'a mut self) -> $crate::glib::translate::StashMut<*mut $ffi_name, Self> { fn to_glib_none_mut(&'a mut self) -> $crate::glib::translate::StashMut<*mut $ffi_name, Self> {
self.make_mut(); self.make_mut();
$crate::glib::translate::StashMut(unsafe { self.as_mut_ptr() }, self) $crate::glib::translate::StashMut( self.as_mut_ptr() , self)
} }
} }
@ -389,11 +389,11 @@ macro_rules! mini_object_wrapper (
} }
impl $ref_name { impl $ref_name {
pub unsafe fn as_ptr(&self) -> *const $ffi_name { pub fn as_ptr(&self) -> *const $ffi_name {
self as *const Self as *const $ffi_name self as *const Self as *const $ffi_name
} }
pub unsafe fn as_mut_ptr(&self) -> *mut $ffi_name { pub fn as_mut_ptr(&self) -> *mut $ffi_name {
self as *const Self as *mut $ffi_name self as *const Self as *mut $ffi_name
} }
@ -567,7 +567,7 @@ impl fmt::Debug for MiniObject {
impl fmt::Debug for MiniObjectRef { impl fmt::Debug for MiniObjectRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MiniObject") f.debug_struct("MiniObject")
.field("ptr", unsafe { &self.as_ptr() }) .field("ptr", &self.as_ptr())
.field("type", &self.type_()) .field("type", &self.type_())
.finish() .finish()
} }

View file

@ -125,7 +125,7 @@ impl fmt::Debug for Query {
impl fmt::Debug for QueryRef { impl fmt::Debug for QueryRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Query") f.debug_struct("Query")
.field("ptr", unsafe { &self.as_ptr() }) .field("ptr", &self.as_ptr())
.field("type", &unsafe { .field("type", &unsafe {
let type_ = ffi::gst_query_type_get_name((*self.as_ptr()).type_); let type_ = ffi::gst_query_type_get_name((*self.as_ptr()).type_);
CStr::from_ptr(type_).to_str().unwrap() CStr::from_ptr(type_).to_str().unwrap()
@ -1647,10 +1647,8 @@ mod tests {
QueryView::Position(p) => { QueryView::Position(p) => {
let pos = p.result(); let pos = p.result();
assert_eq!(pos.try_into(), Ok(Some(3 * ClockTime::SECOND))); assert_eq!(pos.try_into(), Ok(Some(3 * ClockTime::SECOND)));
unsafe {
assert!(!p.as_mut_ptr().is_null()); assert!(!p.as_mut_ptr().is_null());
} }
}
_ => panic!("Wrong concrete Query in Query"), _ => panic!("Wrong concrete Query in Query"),
} }
} }
@ -1701,8 +1699,6 @@ mod tests {
crate::init().unwrap(); crate::init().unwrap();
let p = Position::new(crate::Format::Time); let p = Position::new(crate::Format::Time);
unsafe {
assert!(!p.as_mut_ptr().is_null()); assert!(!p.as_mut_ptr().is_null());
} }
} }
}

View file

@ -367,11 +367,11 @@ impl StructureRef {
&mut *(ptr as *mut StructureRef) &mut *(ptr as *mut StructureRef)
} }
pub unsafe fn as_ptr(&self) -> *const ffi::GstStructure { pub fn as_ptr(&self) -> *const ffi::GstStructure {
self as *const Self as *const ffi::GstStructure self as *const Self as *const ffi::GstStructure
} }
pub unsafe fn as_mut_ptr(&self) -> *mut ffi::GstStructure { pub fn as_mut_ptr(&self) -> *mut ffi::GstStructure {
self as *const Self as *mut ffi::GstStructure self as *const Self as *mut ffi::GstStructure
} }