From 2dcd5cf9ebc12bd3d136e6efee1c165cc4dac511 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Thu, 11 Aug 2022 16:02:02 +0200 Subject: [PATCH] as_ptr() and as_mut_ptr() accessors are safe Nothing unsafe about getting a pointer, deferencing it is unsafe. --- gstreamer-allocators/src/fd_allocator.rs | 2 +- gstreamer-allocators/src/phys_memory.rs | 2 +- gstreamer-rtp/src/rtp_buffer.rs | 4 ++-- gstreamer-video/src/video_message.rs | 2 +- gstreamer/src/buffer.rs | 10 +++------- gstreamer/src/buffer_pool.rs | 4 ++-- gstreamer/src/bufferlist.rs | 2 +- gstreamer/src/caps_features.rs | 4 ++-- gstreamer/src/event.rs | 2 +- gstreamer/src/memory.rs | 2 +- gstreamer/src/message.rs | 2 +- gstreamer/src/meta.rs | 16 ++++------------ gstreamer/src/miniobject.rs | 12 ++++++------ gstreamer/src/query.rs | 10 +++------- gstreamer/src/structure.rs | 4 ++-- 15 files changed, 31 insertions(+), 47 deletions(-) diff --git a/gstreamer-allocators/src/fd_allocator.rs b/gstreamer-allocators/src/fd_allocator.rs index 72d4207a5..bffca225c 100644 --- a/gstreamer-allocators/src/fd_allocator.rs +++ b/gstreamer-allocators/src/fd_allocator.rs @@ -24,7 +24,7 @@ impl fmt::Debug for FdMemory { impl fmt::Debug for FdMemoryRef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("FdMemory") - .field("ptr", unsafe { &self.as_ptr() }) + .field("ptr", &self.as_ptr()) .field("allocator", &self.allocator()) .field("parent", &self.parent()) .field("maxsize", &self.maxsize()) diff --git a/gstreamer-allocators/src/phys_memory.rs b/gstreamer-allocators/src/phys_memory.rs index 5591b1db5..8d34a1e39 100644 --- a/gstreamer-allocators/src/phys_memory.rs +++ b/gstreamer-allocators/src/phys_memory.rs @@ -22,7 +22,7 @@ impl fmt::Debug for PhysMemory { impl fmt::Debug for PhysMemoryRef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("FdMemory") - .field("ptr", unsafe { &self.as_ptr() }) + .field("ptr", &self.as_ptr()) .field("allocator", &self.allocator()) .field("parent", &self.parent()) .field("maxsize", &self.maxsize()) diff --git a/gstreamer-rtp/src/rtp_buffer.rs b/gstreamer-rtp/src/rtp_buffer.rs index 982bde40b..408eeed16 100644 --- a/gstreamer-rtp/src/rtp_buffer.rs +++ b/gstreamer-rtp/src/rtp_buffer.rs @@ -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 } - 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 } } diff --git a/gstreamer-video/src/video_message.rs b/gstreamer-video/src/video_message.rs index a061ca65b..e7a414053 100644 --- a/gstreamer-video/src/video_message.rs +++ b/gstreamer-video/src/video_message.rs @@ -138,7 +138,7 @@ pub struct NavigationEventMessage { impl PartialEq for NavigationEventMessage { fn eq(&self, other: &Self) -> bool { - unsafe { self.event.as_ptr() == other.event.as_ptr() } + self.event.as_ptr() == other.event.as_ptr() } } diff --git a/gstreamer/src/buffer.rs b/gstreamer/src/buffer.rs index 4c1746327..335efe0ac 100644 --- a/gstreamer/src/buffer.rs +++ b/gstreamer/src/buffer.rs @@ -1051,7 +1051,7 @@ impl fmt::Debug for BufferRef { } f.debug_struct("Buffer") - .field("ptr", unsafe { &self.as_ptr() }) + .field("ptr", &self.as_ptr()) .field("pts", &self.pts().display()) .field("dts", &self.dts().display()) .field("duration", &self.duration().display()) @@ -1306,15 +1306,11 @@ mod tests { let mut buffer2 = buffer.clone(); 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(); - unsafe { - assert_ne!(buffer2.as_ptr(), buffer.as_ptr()); - } + assert_ne!(buffer2.as_ptr(), buffer.as_ptr()); buffer2.set_pts(Some(2 * ClockTime::NSECOND)); diff --git a/gstreamer/src/buffer_pool.rs b/gstreamer/src/buffer_pool.rs index febf0cb69..f1a916e83 100644 --- a/gstreamer/src/buffer_pool.rs +++ b/gstreamer/src/buffer_pool.rs @@ -64,11 +64,11 @@ impl 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 } - 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 } } diff --git a/gstreamer/src/bufferlist.rs b/gstreamer/src/bufferlist.rs index c7938a476..c7133f574 100644 --- a/gstreamer/src/bufferlist.rs +++ b/gstreamer/src/bufferlist.rs @@ -201,7 +201,7 @@ impl fmt::Debug for BufferListRef { .unwrap_or((ClockTime::NONE, ClockTime::NONE)); f.debug_struct("BufferList") - .field("ptr", unsafe { &self.as_ptr() }) + .field("ptr", &self.as_ptr()) .field("buffers", &self.len()) .field("pts", &pts.display()) .field("dts", &dts.display()) diff --git a/gstreamer/src/caps_features.rs b/gstreamer/src/caps_features.rs index 56b2114f7..880028f6c 100644 --- a/gstreamer/src/caps_features.rs +++ b/gstreamer/src/caps_features.rs @@ -304,11 +304,11 @@ impl 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 } - 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 } diff --git a/gstreamer/src/event.rs b/gstreamer/src/event.rs index 9c637189d..de8789ce7 100644 --- a/gstreamer/src/event.rs +++ b/gstreamer/src/event.rs @@ -271,7 +271,7 @@ impl fmt::Debug for Event { impl fmt::Debug for EventRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Event") - .field("ptr", unsafe { &self.as_ptr() }) + .field("ptr", &self.as_ptr()) .field("type", &self.type_().name()) .field("seqnum", &self.seqnum()) .field("structure", &self.structure()) diff --git a/gstreamer/src/memory.rs b/gstreamer/src/memory.rs index 127e655bc..714189ac1 100644 --- a/gstreamer/src/memory.rs +++ b/gstreamer/src/memory.rs @@ -38,7 +38,7 @@ impl fmt::Debug for Memory { impl fmt::Debug for MemoryRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Memory") - .field("ptr", unsafe { &self.as_ptr() }) + .field("ptr", &self.as_ptr()) .field("allocator", &self.allocator()) .field("parent", &self.parent()) .field("maxsize", &self.maxsize()) diff --git a/gstreamer/src/message.rs b/gstreamer/src/message.rs index 718d813a9..806919eb3 100644 --- a/gstreamer/src/message.rs +++ b/gstreamer/src/message.rs @@ -150,7 +150,7 @@ impl fmt::Debug for MessageRef { }; f.debug_struct("Message") - .field("ptr", unsafe { &self.as_ptr() }) + .field("ptr", &self.as_ptr()) .field("type", &unsafe { let type_ = ffi::gst_message_type_get_name((*self.as_ptr()).type_); CStr::from_ptr(type_).to_str().unwrap() diff --git a/gstreamer/src/meta.rs b/gstreamer/src/meta.rs index c4be2261e..91bfb27e2 100644 --- a/gstreamer/src/meta.rs +++ b/gstreamer/src/meta.rs @@ -681,9 +681,7 @@ mod tests { let parent = crate::Buffer::new(); { 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()); } { @@ -697,9 +695,7 @@ mod tests { { let metas = buffer.iter_meta::().collect::>(); 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 @@ -708,9 +704,7 @@ mod tests { .iter_meta_mut::() .collect::>(); 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()); } { @@ -719,9 +713,7 @@ mod tests { .unwrap() .meta_mut::() .unwrap(); - unsafe { - assert_eq!(meta.parent().as_ptr(), parent.as_ptr()); - } + assert_eq!(meta.parent().as_ptr(), parent.as_ptr()); meta.remove().unwrap(); } diff --git a/gstreamer/src/miniobject.rs b/gstreamer/src/miniobject.rs index 5cf5dd1a7..82a9ecd2d 100644 --- a/gstreamer/src/miniobject.rs +++ b/gstreamer/src/miniobject.rs @@ -149,7 +149,7 @@ macro_rules! mini_object_wrapper ( type Storage = &'a 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 { @@ -164,7 +164,7 @@ macro_rules! mini_object_wrapper ( type Storage = &'a 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 { @@ -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> { 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 { - 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 } - 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 } @@ -567,7 +567,7 @@ impl fmt::Debug for MiniObject { impl fmt::Debug for MiniObjectRef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("MiniObject") - .field("ptr", unsafe { &self.as_ptr() }) + .field("ptr", &self.as_ptr()) .field("type", &self.type_()) .finish() } diff --git a/gstreamer/src/query.rs b/gstreamer/src/query.rs index 24123142d..9a77b8f90 100644 --- a/gstreamer/src/query.rs +++ b/gstreamer/src/query.rs @@ -125,7 +125,7 @@ impl fmt::Debug for Query { impl fmt::Debug for QueryRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Query") - .field("ptr", unsafe { &self.as_ptr() }) + .field("ptr", &self.as_ptr()) .field("type", &unsafe { let type_ = ffi::gst_query_type_get_name((*self.as_ptr()).type_); CStr::from_ptr(type_).to_str().unwrap() @@ -1647,9 +1647,7 @@ mod tests { QueryView::Position(p) => { let pos = p.result(); 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"), } @@ -1701,8 +1699,6 @@ mod tests { crate::init().unwrap(); let p = Position::new(crate::Format::Time); - unsafe { - assert!(!p.as_mut_ptr().is_null()); - } + assert!(!p.as_mut_ptr().is_null()); } } diff --git a/gstreamer/src/structure.rs b/gstreamer/src/structure.rs index fad29b6db..35a53d9c7 100644 --- a/gstreamer/src/structure.rs +++ b/gstreamer/src/structure.rs @@ -367,11 +367,11 @@ impl 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 } - 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 }