Get rid of unnecessary option wrapping

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1187>
This commit is contained in:
Sebastian Dröge 2023-01-04 20:08:39 +02:00 committed by GStreamer Marge Bot
parent da2eb50dd3
commit 1b022a6b7c
4 changed files with 74 additions and 66 deletions

View file

@ -10,7 +10,7 @@ pub enum Writable {}
pub struct AudioBuffer<T> { pub struct AudioBuffer<T> {
// Has to be boxed because it contains self-references // Has to be boxed because it contains self-references
audio_buffer: Box<ffi::GstAudioBuffer>, audio_buffer: Box<ffi::GstAudioBuffer>,
buffer: Option<gst::Buffer>, buffer: gst::Buffer,
info: crate::AudioInfo, info: crate::AudioInfo,
phantom: PhantomData<T>, phantom: PhantomData<T>,
} }
@ -34,8 +34,8 @@ impl<T> AudioBuffer<T> {
&self.info &self.info
} }
pub fn into_buffer(mut self) -> gst::Buffer { pub fn into_buffer(self) -> gst::Buffer {
self.buffer.take().unwrap() unsafe { ptr::read(&mem::ManuallyDrop::new(self).buffer) }
} }
pub fn format(&self) -> crate::AudioFormat { pub fn format(&self) -> crate::AudioFormat {
@ -114,9 +114,9 @@ impl<T> AudioBuffer<T> {
let info = self.info.clone(); let info = self.info.clone();
AudioBufferRef { AudioBufferRef {
audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::from(&*self.audio_buffer)), audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::from(&*self.audio_buffer)),
buffer: Some(self.buffer()),
info, info,
unmap: false, unmap: false,
phantom: PhantomData,
} }
} }
@ -159,7 +159,7 @@ impl AudioBuffer<Readable> {
); );
Ok(Self { Ok(Self {
audio_buffer, audio_buffer,
buffer: Some(buffer), buffer,
info, info,
phantom: PhantomData, phantom: PhantomData,
}) })
@ -194,7 +194,7 @@ impl AudioBuffer<Writable> {
); );
Ok(Self { Ok(Self {
audio_buffer, audio_buffer,
buffer: Some(buffer), buffer,
info, info,
phantom: PhantomData, phantom: PhantomData,
}) })
@ -225,9 +225,9 @@ impl AudioBuffer<Writable> {
let info = self.info.clone(); let info = self.info.clone();
AudioBufferRef { AudioBufferRef {
audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::from(&mut *self.audio_buffer)), audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::from(&mut *self.audio_buffer)),
buffer: Some(self.buffer_mut()),
info, info,
unmap: false, unmap: false,
phantom: PhantomData,
} }
} }
@ -266,9 +266,9 @@ impl ops::DerefMut for AudioBufferPtr {
pub struct AudioBufferRef<T> { pub struct AudioBufferRef<T> {
// Has to be boxed because it contains self-references // Has to be boxed because it contains self-references
audio_buffer: AudioBufferPtr, audio_buffer: AudioBufferPtr,
buffer: Option<T>,
info: crate::AudioInfo, info: crate::AudioInfo,
unmap: bool, unmap: bool,
phantom: PhantomData<T>,
} }
impl<T> AudioBufferRef<T> { impl<T> AudioBufferRef<T> {
@ -360,14 +360,13 @@ impl<'a> AudioBufferRef<&'a gst::BufferRef> {
let info = crate::AudioInfo::from_glib_none( let info = crate::AudioInfo::from_glib_none(
&(*audio_buffer).info as *const _ as *mut ffi::GstAudioInfo, &(*audio_buffer).info as *const _ as *mut ffi::GstAudioInfo,
); );
let buffer = gst::BufferRef::from_ptr((*audio_buffer).buffer);
Borrowed::new(Self { Borrowed::new(Self {
audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::new_unchecked( audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::new_unchecked(
audio_buffer as *mut _, audio_buffer as *mut _,
)), )),
buffer: Some(buffer),
info, info,
unmap: false, unmap: false,
phantom: PhantomData,
}) })
} }
@ -396,16 +395,16 @@ impl<'a> AudioBufferRef<&'a gst::BufferRef> {
); );
Ok(Self { Ok(Self {
audio_buffer: AudioBufferPtr::Owned(audio_buffer), audio_buffer: AudioBufferPtr::Owned(audio_buffer),
buffer: Some(buffer),
info, info,
unmap: true, unmap: true,
phantom: PhantomData,
}) })
} }
} }
} }
pub fn buffer(&self) -> &gst::BufferRef { 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( let info = crate::AudioInfo::from_glib_none(
&(*audio_buffer).info as *const _ as *mut ffi::GstAudioInfo, &(*audio_buffer).info as *const _ as *mut ffi::GstAudioInfo,
); );
let buffer = gst::BufferRef::from_mut_ptr((*audio_buffer).buffer);
Borrowed::new(Self { Borrowed::new(Self {
audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::new_unchecked(audio_buffer)), audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::new_unchecked(audio_buffer)),
buffer: Some(buffer),
info, info,
unmap: false, unmap: false,
phantom: PhantomData,
}) })
} }
@ -450,16 +448,16 @@ impl<'a> AudioBufferRef<&'a mut gst::BufferRef> {
); );
Ok(Self { Ok(Self {
audio_buffer: AudioBufferPtr::Owned(audio_buffer), audio_buffer: AudioBufferPtr::Owned(audio_buffer),
buffer: Some(buffer),
info, info,
unmap: true, unmap: true,
phantom: PhantomData,
}) })
} }
} }
} }
pub fn buffer_mut(&mut self) -> &mut gst::BufferRef { 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> { pub fn plane_data_mut(&mut self, plane: u32) -> Result<&mut [u8], glib::BoolError> {

View file

@ -9,7 +9,7 @@ pub enum Writable {}
pub struct VideoFrame<T> { pub struct VideoFrame<T> {
frame: ffi::GstVideoFrame, frame: ffi::GstVideoFrame,
buffer: Option<gst::Buffer>, buffer: gst::Buffer,
info: crate::VideoInfo, info: crate::VideoInfo,
phantom: PhantomData<T>, phantom: PhantomData<T>,
} }
@ -41,8 +41,9 @@ impl<T> VideoFrame<T> {
self.frame.id self.frame.id
} }
pub fn into_buffer(mut self) -> gst::Buffer { pub fn into_buffer(self) -> gst::Buffer {
self.buffer.take().unwrap() let s = mem::ManuallyDrop::new(self);
unsafe { ptr::read(&s.buffer) }
} }
#[doc(alias = "gst_video_frame_copy")] #[doc(alias = "gst_video_frame_copy")]
@ -223,7 +224,7 @@ impl<T> VideoFrame<T> {
let buffer = gst::Buffer::from_glib_none(frame.buffer); let buffer = gst::Buffer::from_glib_none(frame.buffer);
Self { Self {
frame, frame,
buffer: Some(buffer), buffer,
info, info,
phantom: PhantomData, phantom: PhantomData,
} }
@ -234,9 +235,9 @@ impl<T> VideoFrame<T> {
let info = self.info.clone(); let info = self.info.clone();
VideoFrameRef { VideoFrameRef {
frame, frame,
buffer: Some(self.buffer()),
info, info,
unmap: false, unmap: false,
phantom: PhantomData,
} }
} }
@ -245,9 +246,7 @@ impl<T> VideoFrame<T> {
} }
pub fn into_raw(self) -> ffi::GstVideoFrame { pub fn into_raw(self) -> ffi::GstVideoFrame {
let mut s = mem::ManuallyDrop::new(self); mem::ManuallyDrop::new(self).frame
s.buffer = None;
s.frame
} }
} }
@ -284,7 +283,7 @@ impl VideoFrame<Readable> {
let info = crate::VideoInfo(ptr::read(&frame.info)); let info = crate::VideoInfo(ptr::read(&frame.info));
Ok(Self { Ok(Self {
frame, frame,
buffer: Some(buffer), buffer,
info, info,
phantom: PhantomData, phantom: PhantomData,
}) })
@ -318,7 +317,7 @@ impl VideoFrame<Readable> {
let info = crate::VideoInfo(ptr::read(&frame.info)); let info = crate::VideoInfo(ptr::read(&frame.info));
Ok(Self { Ok(Self {
frame, frame,
buffer: Some(buffer), buffer,
info, info,
phantom: PhantomData, phantom: PhantomData,
}) })
@ -358,7 +357,7 @@ impl VideoFrame<Writable> {
let info = crate::VideoInfo(ptr::read(&frame.info)); let info = crate::VideoInfo(ptr::read(&frame.info));
Ok(Self { Ok(Self {
frame, frame,
buffer: Some(buffer), buffer,
info, info,
phantom: PhantomData, phantom: PhantomData,
}) })
@ -394,7 +393,7 @@ impl VideoFrame<Writable> {
let info = crate::VideoInfo(ptr::read(&frame.info)); let info = crate::VideoInfo(ptr::read(&frame.info));
Ok(Self { Ok(Self {
frame, frame,
buffer: Some(buffer), buffer,
info, info,
phantom: PhantomData, phantom: PhantomData,
}) })
@ -452,9 +451,9 @@ impl VideoFrame<Writable> {
let info = self.info.clone(); let info = self.info.clone();
VideoFrameRef { VideoFrameRef {
frame, frame,
buffer: Some(self.buffer_mut()),
info, info,
unmap: false, unmap: false,
phantom: PhantomData,
} }
} }
@ -466,9 +465,9 @@ impl VideoFrame<Writable> {
#[derive(Debug)] #[derive(Debug)]
pub struct VideoFrameRef<T> { pub struct VideoFrameRef<T> {
frame: ffi::GstVideoFrame, frame: ffi::GstVideoFrame,
buffer: Option<T>,
info: crate::VideoInfo, info: crate::VideoInfo,
unmap: bool, unmap: bool,
phantom: PhantomData<T>,
} }
impl<T> VideoFrameRef<T> { impl<T> VideoFrameRef<T> {
@ -667,23 +666,21 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> {
let frame = ptr::read(frame); let frame = ptr::read(frame);
let info = crate::VideoInfo(ptr::read(&frame.info)); let info = crate::VideoInfo(ptr::read(&frame.info));
let buffer = gst::BufferRef::from_ptr(frame.buffer);
Borrowed::new(Self { Borrowed::new(Self {
frame, frame,
buffer: Some(buffer),
info, info,
unmap: false, unmap: false,
phantom: PhantomData,
}) })
} }
pub unsafe fn from_glib_full(frame: ffi::GstVideoFrame) -> Self { pub unsafe fn from_glib_full(frame: ffi::GstVideoFrame) -> Self {
let info = crate::VideoInfo(ptr::read(&frame.info)); let info = crate::VideoInfo(ptr::read(&frame.info));
let buffer = gst::BufferRef::from_ptr(frame.buffer);
Self { Self {
frame, frame,
buffer: Some(buffer),
info, info,
unmap: true, unmap: true,
phantom: PhantomData,
} }
} }
@ -711,9 +708,9 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> {
let info = crate::VideoInfo(ptr::read(&frame.info)); let info = crate::VideoInfo(ptr::read(&frame.info));
Ok(Self { Ok(Self {
frame, frame,
buffer: Some(buffer),
info, info,
unmap: true, unmap: true,
phantom: PhantomData,
}) })
} }
} }
@ -745,16 +742,16 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> {
let info = crate::VideoInfo(ptr::read(&frame.info)); let info = crate::VideoInfo(ptr::read(&frame.info));
Ok(Self { Ok(Self {
frame, frame,
buffer: Some(buffer),
info, info,
unmap: true, unmap: true,
phantom: PhantomData,
}) })
} }
} }
} }
pub fn buffer(&self) -> &gst::BufferRef { 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 frame = ptr::read(frame);
let info = crate::VideoInfo(ptr::read(&frame.info)); let info = crate::VideoInfo(ptr::read(&frame.info));
let buffer = gst::BufferRef::from_mut_ptr(frame.buffer);
Self { Self {
frame, frame,
buffer: Some(buffer),
info, info,
unmap: false, unmap: false,
phantom: PhantomData,
} }
} }
pub unsafe fn from_glib_full_mut(frame: ffi::GstVideoFrame) -> Self { pub unsafe fn from_glib_full_mut(frame: ffi::GstVideoFrame) -> Self {
let info = crate::VideoInfo(ptr::read(&frame.info)); let info = crate::VideoInfo(ptr::read(&frame.info));
let buffer = gst::BufferRef::from_mut_ptr(frame.buffer);
Self { Self {
frame, frame,
buffer: Some(buffer),
info, info,
unmap: true, unmap: true,
phantom: PhantomData,
} }
} }
@ -810,9 +805,9 @@ impl<'a> VideoFrameRef<&'a mut gst::BufferRef> {
let info = crate::VideoInfo(ptr::read(&frame.info)); let info = crate::VideoInfo(ptr::read(&frame.info));
Ok(Self { Ok(Self {
frame, frame,
buffer: Some(buffer),
info, info,
unmap: true, unmap: true,
phantom: PhantomData,
}) })
} }
} }
@ -846,16 +841,16 @@ impl<'a> VideoFrameRef<&'a mut gst::BufferRef> {
let info = crate::VideoInfo(ptr::read(&frame.info)); let info = crate::VideoInfo(ptr::read(&frame.info));
Ok(Self { Ok(Self {
frame, frame,
buffer: Some(buffer),
info, info,
unmap: true, unmap: true,
phantom: PhantomData,
}) })
} }
} }
} }
pub fn buffer_mut(&mut self) -> &mut gst::BufferRef { 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> { pub fn comp_data_mut(&mut self, component: u32) -> Result<&mut [u8], glib::BoolError> {

View file

@ -28,7 +28,7 @@ pub struct BufferMap<'a, T> {
} }
pub struct MappedBuffer<T> { pub struct MappedBuffer<T> {
buffer: Option<Buffer>, buffer: Buffer,
map_info: ffi::GstMapInfo, map_info: ffi::GstMapInfo,
phantom: PhantomData<T>, phantom: PhantomData<T>,
} }
@ -118,7 +118,7 @@ impl Buffer {
)); ));
if res { if res {
Ok(MappedBuffer { Ok(MappedBuffer {
buffer: Some(self), buffer: self,
map_info: map_info.assume_init(), map_info: map_info.assume_init(),
phantom: PhantomData, phantom: PhantomData,
}) })
@ -139,7 +139,7 @@ impl Buffer {
)); ));
if res { if res {
Ok(MappedBuffer { Ok(MappedBuffer {
buffer: Some(self), buffer: self,
map_info: map_info.assume_init(), map_info: map_info.assume_init(),
phantom: PhantomData, phantom: PhantomData,
}) })
@ -1179,13 +1179,14 @@ impl<T> MappedBuffer<T> {
#[doc(alias = "get_buffer")] #[doc(alias = "get_buffer")]
pub fn buffer(&self) -> &BufferRef { pub fn buffer(&self) -> &BufferRef {
self.buffer.as_ref().unwrap().as_ref() self.buffer.as_ref()
} }
pub fn into_buffer(mut self) -> Buffer { pub fn into_buffer(self) -> Buffer {
let buffer = self.buffer.take().unwrap(); let mut s = mem::ManuallyDrop::new(self);
let buffer = unsafe { ptr::read(&s.buffer) };
unsafe { 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 buffer
@ -1229,10 +1230,8 @@ impl ops::DerefMut for MappedBuffer<Writable> {
impl<T> Drop for MappedBuffer<T> { impl<T> Drop for MappedBuffer<T> {
fn drop(&mut self) { fn drop(&mut self) {
if let Some(ref buffer) = self.buffer {
unsafe { unsafe {
ffi::gst_buffer_unmap(buffer.as_mut_ptr(), &mut self.map_info); ffi::gst_buffer_unmap(self.buffer.as_mut_ptr(), &mut self.map_info);
}
} }
} }
} }

View file

@ -23,7 +23,7 @@ pub struct MemoryMap<'a, T> {
} }
pub struct MappedMemory<T> { pub struct MappedMemory<T> {
memory: Option<Memory>, memory: Memory,
map_info: ffi::GstMapInfo, map_info: ffi::GstMapInfo,
phantom: PhantomData<T>, phantom: PhantomData<T>,
} }
@ -133,7 +133,7 @@ impl Memory {
)); ));
if res { if res {
Ok(MappedMemory { Ok(MappedMemory {
memory: Some(self), memory: self,
map_info: map_info.assume_init(), map_info: map_info.assume_init(),
phantom: PhantomData, phantom: PhantomData,
}) })
@ -153,7 +153,7 @@ impl Memory {
)); ));
if res { if res {
Ok(MappedMemory { Ok(MappedMemory {
memory: Some(self), memory: self,
map_info: map_info.assume_init(), map_info: map_info.assume_init(),
phantom: PhantomData, phantom: PhantomData,
}) })
@ -418,13 +418,14 @@ impl<T> MappedMemory<T> {
#[doc(alias = "get_memory")] #[doc(alias = "get_memory")]
pub fn memory(&self) -> &MemoryRef { pub fn memory(&self) -> &MemoryRef {
self.memory.as_ref().unwrap().as_ref() self.memory.as_ref()
} }
pub fn into_memory(mut self) -> Memory { pub fn into_memory(self) -> Memory {
let memory = self.memory.take().unwrap(); let mut s = mem::ManuallyDrop::new(self);
let memory = unsafe { ptr::read(&s.memory) };
unsafe { 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 memory
@ -468,10 +469,8 @@ impl DerefMut for MappedMemory<Writable> {
impl<T> Drop for MappedMemory<T> { impl<T> Drop for MappedMemory<T> {
fn drop(&mut self) { fn drop(&mut self) {
if let Some(ref memory) = self.memory {
unsafe { unsafe {
ffi::gst_memory_unmap(memory.as_mut_ptr(), &mut self.map_info); ffi::gst_memory_unmap(self.memory.as_mut_ptr(), &mut self.map_info);
}
} }
} }
} }
@ -879,6 +878,23 @@ macro_rules! memory_object_wrapper {
#[cfg(test)] #[cfg(test)]
mod tests { 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] #[test]
fn test_dump() { fn test_dump() {
crate::init().unwrap(); crate::init().unwrap();