From 1b022a6b7c6f122b2a0b5fea50749dabacad9e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 4 Jan 2023 20:08:39 +0200 Subject: [PATCH] Get rid of unnecessary option wrapping Part-of: --- gstreamer-audio/src/audio_buffer.rs | 30 ++++++++--------- gstreamer-video/src/video_frame.rs | 51 +++++++++++++---------------- gstreamer/src/buffer.rs | 21 ++++++------ gstreamer/src/memory.rs | 38 ++++++++++++++------- 4 files changed, 74 insertions(+), 66 deletions(-) diff --git a/gstreamer-audio/src/audio_buffer.rs b/gstreamer-audio/src/audio_buffer.rs index 8302ab4ab..376c882c4 100644 --- a/gstreamer-audio/src/audio_buffer.rs +++ b/gstreamer-audio/src/audio_buffer.rs @@ -10,7 +10,7 @@ pub enum Writable {} pub struct AudioBuffer { // Has to be boxed because it contains self-references audio_buffer: Box, - buffer: Option, + buffer: gst::Buffer, info: crate::AudioInfo, phantom: PhantomData, } @@ -34,8 +34,8 @@ impl AudioBuffer { &self.info } - pub fn into_buffer(mut self) -> gst::Buffer { - self.buffer.take().unwrap() + pub fn into_buffer(self) -> gst::Buffer { + unsafe { ptr::read(&mem::ManuallyDrop::new(self).buffer) } } pub fn format(&self) -> crate::AudioFormat { @@ -114,9 +114,9 @@ impl AudioBuffer { let info = self.info.clone(); AudioBufferRef { audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::from(&*self.audio_buffer)), - buffer: Some(self.buffer()), info, unmap: false, + phantom: PhantomData, } } @@ -159,7 +159,7 @@ impl AudioBuffer { ); Ok(Self { audio_buffer, - buffer: Some(buffer), + buffer, info, phantom: PhantomData, }) @@ -194,7 +194,7 @@ impl AudioBuffer { ); Ok(Self { audio_buffer, - buffer: Some(buffer), + buffer, info, phantom: PhantomData, }) @@ -225,9 +225,9 @@ impl AudioBuffer { let info = self.info.clone(); AudioBufferRef { audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::from(&mut *self.audio_buffer)), - buffer: Some(self.buffer_mut()), info, unmap: false, + phantom: PhantomData, } } @@ -266,9 +266,9 @@ impl ops::DerefMut for AudioBufferPtr { pub struct AudioBufferRef { // Has to be boxed because it contains self-references audio_buffer: AudioBufferPtr, - buffer: Option, info: crate::AudioInfo, unmap: bool, + phantom: PhantomData, } impl AudioBufferRef { @@ -360,14 +360,13 @@ impl<'a> AudioBufferRef<&'a gst::BufferRef> { let info = crate::AudioInfo::from_glib_none( &(*audio_buffer).info as *const _ as *mut ffi::GstAudioInfo, ); - let buffer = gst::BufferRef::from_ptr((*audio_buffer).buffer); Borrowed::new(Self { audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::new_unchecked( audio_buffer as *mut _, )), - buffer: Some(buffer), info, unmap: false, + phantom: PhantomData, }) } @@ -396,16 +395,16 @@ impl<'a> AudioBufferRef<&'a gst::BufferRef> { ); Ok(Self { audio_buffer: AudioBufferPtr::Owned(audio_buffer), - buffer: Some(buffer), info, unmap: true, + phantom: PhantomData, }) } } } pub fn buffer(&self) -> &gst::BufferRef { - self.buffer.as_ref().unwrap() + unsafe { gst::BufferRef::from_ptr(self.audio_buffer.buffer) } } } @@ -416,12 +415,11 @@ impl<'a> AudioBufferRef<&'a mut gst::BufferRef> { let info = crate::AudioInfo::from_glib_none( &(*audio_buffer).info as *const _ as *mut ffi::GstAudioInfo, ); - let buffer = gst::BufferRef::from_mut_ptr((*audio_buffer).buffer); Borrowed::new(Self { audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::new_unchecked(audio_buffer)), - buffer: Some(buffer), info, unmap: false, + phantom: PhantomData, }) } @@ -450,16 +448,16 @@ impl<'a> AudioBufferRef<&'a mut gst::BufferRef> { ); Ok(Self { audio_buffer: AudioBufferPtr::Owned(audio_buffer), - buffer: Some(buffer), info, unmap: true, + phantom: PhantomData, }) } } } pub fn buffer_mut(&mut self) -> &mut gst::BufferRef { - self.buffer.as_mut().unwrap() + unsafe { gst::BufferRef::from_mut_ptr(self.audio_buffer.buffer) } } pub fn plane_data_mut(&mut self, plane: u32) -> Result<&mut [u8], glib::BoolError> { diff --git a/gstreamer-video/src/video_frame.rs b/gstreamer-video/src/video_frame.rs index b29413e7b..2f380a8e8 100644 --- a/gstreamer-video/src/video_frame.rs +++ b/gstreamer-video/src/video_frame.rs @@ -9,7 +9,7 @@ pub enum Writable {} pub struct VideoFrame { frame: ffi::GstVideoFrame, - buffer: Option, + buffer: gst::Buffer, info: crate::VideoInfo, phantom: PhantomData, } @@ -41,8 +41,9 @@ impl VideoFrame { self.frame.id } - pub fn into_buffer(mut self) -> gst::Buffer { - self.buffer.take().unwrap() + pub fn into_buffer(self) -> gst::Buffer { + let s = mem::ManuallyDrop::new(self); + unsafe { ptr::read(&s.buffer) } } #[doc(alias = "gst_video_frame_copy")] @@ -223,7 +224,7 @@ impl VideoFrame { let buffer = gst::Buffer::from_glib_none(frame.buffer); Self { frame, - buffer: Some(buffer), + buffer, info, phantom: PhantomData, } @@ -234,9 +235,9 @@ impl VideoFrame { let info = self.info.clone(); VideoFrameRef { frame, - buffer: Some(self.buffer()), info, unmap: false, + phantom: PhantomData, } } @@ -245,9 +246,7 @@ impl VideoFrame { } pub fn into_raw(self) -> ffi::GstVideoFrame { - let mut s = mem::ManuallyDrop::new(self); - s.buffer = None; - s.frame + mem::ManuallyDrop::new(self).frame } } @@ -284,7 +283,7 @@ impl VideoFrame { let info = crate::VideoInfo(ptr::read(&frame.info)); Ok(Self { frame, - buffer: Some(buffer), + buffer, info, phantom: PhantomData, }) @@ -318,7 +317,7 @@ impl VideoFrame { let info = crate::VideoInfo(ptr::read(&frame.info)); Ok(Self { frame, - buffer: Some(buffer), + buffer, info, phantom: PhantomData, }) @@ -358,7 +357,7 @@ impl VideoFrame { let info = crate::VideoInfo(ptr::read(&frame.info)); Ok(Self { frame, - buffer: Some(buffer), + buffer, info, phantom: PhantomData, }) @@ -394,7 +393,7 @@ impl VideoFrame { let info = crate::VideoInfo(ptr::read(&frame.info)); Ok(Self { frame, - buffer: Some(buffer), + buffer, info, phantom: PhantomData, }) @@ -452,9 +451,9 @@ impl VideoFrame { let info = self.info.clone(); VideoFrameRef { frame, - buffer: Some(self.buffer_mut()), info, unmap: false, + phantom: PhantomData, } } @@ -466,9 +465,9 @@ impl VideoFrame { #[derive(Debug)] pub struct VideoFrameRef { frame: ffi::GstVideoFrame, - buffer: Option, info: crate::VideoInfo, unmap: bool, + phantom: PhantomData, } impl VideoFrameRef { @@ -667,23 +666,21 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> { let frame = ptr::read(frame); let info = crate::VideoInfo(ptr::read(&frame.info)); - let buffer = gst::BufferRef::from_ptr(frame.buffer); Borrowed::new(Self { frame, - buffer: Some(buffer), info, unmap: false, + phantom: PhantomData, }) } pub unsafe fn from_glib_full(frame: ffi::GstVideoFrame) -> Self { let info = crate::VideoInfo(ptr::read(&frame.info)); - let buffer = gst::BufferRef::from_ptr(frame.buffer); Self { frame, - buffer: Some(buffer), info, unmap: true, + phantom: PhantomData, } } @@ -711,9 +708,9 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> { let info = crate::VideoInfo(ptr::read(&frame.info)); Ok(Self { frame, - buffer: Some(buffer), info, unmap: true, + phantom: PhantomData, }) } } @@ -745,16 +742,16 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> { let info = crate::VideoInfo(ptr::read(&frame.info)); Ok(Self { frame, - buffer: Some(buffer), info, unmap: true, + phantom: PhantomData, }) } } } pub fn buffer(&self) -> &gst::BufferRef { - self.buffer.as_ref().unwrap() + unsafe { gst::BufferRef::from_ptr(self.frame.buffer) } } } @@ -764,23 +761,21 @@ impl<'a> VideoFrameRef<&'a mut gst::BufferRef> { let frame = ptr::read(frame); let info = crate::VideoInfo(ptr::read(&frame.info)); - let buffer = gst::BufferRef::from_mut_ptr(frame.buffer); Self { frame, - buffer: Some(buffer), info, unmap: false, + phantom: PhantomData, } } pub unsafe fn from_glib_full_mut(frame: ffi::GstVideoFrame) -> Self { let info = crate::VideoInfo(ptr::read(&frame.info)); - let buffer = gst::BufferRef::from_mut_ptr(frame.buffer); Self { frame, - buffer: Some(buffer), info, unmap: true, + phantom: PhantomData, } } @@ -810,9 +805,9 @@ impl<'a> VideoFrameRef<&'a mut gst::BufferRef> { let info = crate::VideoInfo(ptr::read(&frame.info)); Ok(Self { frame, - buffer: Some(buffer), info, unmap: true, + phantom: PhantomData, }) } } @@ -846,16 +841,16 @@ impl<'a> VideoFrameRef<&'a mut gst::BufferRef> { let info = crate::VideoInfo(ptr::read(&frame.info)); Ok(Self { frame, - buffer: Some(buffer), info, unmap: true, + phantom: PhantomData, }) } } } pub fn buffer_mut(&mut self) -> &mut gst::BufferRef { - self.buffer.as_mut().unwrap() + unsafe { gst::BufferRef::from_mut_ptr(self.frame.buffer) } } pub fn comp_data_mut(&mut self, component: u32) -> Result<&mut [u8], glib::BoolError> { diff --git a/gstreamer/src/buffer.rs b/gstreamer/src/buffer.rs index 3c1387e0c..f06094c88 100644 --- a/gstreamer/src/buffer.rs +++ b/gstreamer/src/buffer.rs @@ -28,7 +28,7 @@ pub struct BufferMap<'a, T> { } pub struct MappedBuffer { - buffer: Option, + buffer: Buffer, map_info: ffi::GstMapInfo, phantom: PhantomData, } @@ -118,7 +118,7 @@ impl Buffer { )); if res { Ok(MappedBuffer { - buffer: Some(self), + buffer: self, map_info: map_info.assume_init(), phantom: PhantomData, }) @@ -139,7 +139,7 @@ impl Buffer { )); if res { Ok(MappedBuffer { - buffer: Some(self), + buffer: self, map_info: map_info.assume_init(), phantom: PhantomData, }) @@ -1179,13 +1179,14 @@ impl MappedBuffer { #[doc(alias = "get_buffer")] pub fn buffer(&self) -> &BufferRef { - self.buffer.as_ref().unwrap().as_ref() + self.buffer.as_ref() } - pub fn into_buffer(mut self) -> Buffer { - let buffer = self.buffer.take().unwrap(); + pub fn into_buffer(self) -> Buffer { + let mut s = mem::ManuallyDrop::new(self); + let buffer = unsafe { ptr::read(&s.buffer) }; unsafe { - ffi::gst_buffer_unmap(buffer.as_mut_ptr(), &mut self.map_info); + ffi::gst_buffer_unmap(buffer.as_mut_ptr(), &mut s.map_info); } buffer @@ -1229,10 +1230,8 @@ impl ops::DerefMut for MappedBuffer { impl Drop for MappedBuffer { fn drop(&mut self) { - if let Some(ref buffer) = self.buffer { - unsafe { - ffi::gst_buffer_unmap(buffer.as_mut_ptr(), &mut self.map_info); - } + unsafe { + ffi::gst_buffer_unmap(self.buffer.as_mut_ptr(), &mut self.map_info); } } } diff --git a/gstreamer/src/memory.rs b/gstreamer/src/memory.rs index 79dd8f1f4..7ccae7497 100644 --- a/gstreamer/src/memory.rs +++ b/gstreamer/src/memory.rs @@ -23,7 +23,7 @@ pub struct MemoryMap<'a, T> { } pub struct MappedMemory { - memory: Option, + memory: Memory, map_info: ffi::GstMapInfo, phantom: PhantomData, } @@ -133,7 +133,7 @@ impl Memory { )); if res { Ok(MappedMemory { - memory: Some(self), + memory: self, map_info: map_info.assume_init(), phantom: PhantomData, }) @@ -153,7 +153,7 @@ impl Memory { )); if res { Ok(MappedMemory { - memory: Some(self), + memory: self, map_info: map_info.assume_init(), phantom: PhantomData, }) @@ -418,13 +418,14 @@ impl MappedMemory { #[doc(alias = "get_memory")] pub fn memory(&self) -> &MemoryRef { - self.memory.as_ref().unwrap().as_ref() + self.memory.as_ref() } - pub fn into_memory(mut self) -> Memory { - let memory = self.memory.take().unwrap(); + pub fn into_memory(self) -> Memory { + let mut s = mem::ManuallyDrop::new(self); + let memory = unsafe { ptr::read(&s.memory) }; unsafe { - ffi::gst_memory_unmap(memory.as_mut_ptr(), &mut self.map_info); + ffi::gst_memory_unmap(memory.as_mut_ptr(), &mut s.map_info); } memory @@ -468,10 +469,8 @@ impl DerefMut for MappedMemory { impl Drop for MappedMemory { fn drop(&mut self) { - if let Some(ref memory) = self.memory { - unsafe { - ffi::gst_memory_unmap(memory.as_mut_ptr(), &mut self.map_info); - } + unsafe { + ffi::gst_memory_unmap(self.memory.as_mut_ptr(), &mut self.map_info); } } } @@ -879,6 +878,23 @@ macro_rules! memory_object_wrapper { #[cfg(test)] mod tests { + #[test] + fn test_map() { + crate::init().unwrap(); + + let mem = crate::Memory::from_slice(vec![1, 2, 3, 4]); + let map = mem.map_readable().unwrap(); + assert_eq!(map.as_slice(), &[1, 2, 3, 4]); + drop(map); + + let mem = mem.into_mapped_memory_readable().unwrap(); + assert_eq!(mem.as_slice(), &[1, 2, 3, 4]); + + let mem = mem.into_memory(); + let map = mem.map_readable().unwrap(); + assert_eq!(map.as_slice(), &[1, 2, 3, 4]); + } + #[test] fn test_dump() { crate::init().unwrap();