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> {
// Has to be boxed because it contains self-references
audio_buffer: Box<ffi::GstAudioBuffer>,
buffer: Option<gst::Buffer>,
buffer: gst::Buffer,
info: crate::AudioInfo,
phantom: PhantomData<T>,
}
@ -34,8 +34,8 @@ impl<T> AudioBuffer<T> {
&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<T> AudioBuffer<T> {
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<Readable> {
);
Ok(Self {
audio_buffer,
buffer: Some(buffer),
buffer,
info,
phantom: PhantomData,
})
@ -194,7 +194,7 @@ impl AudioBuffer<Writable> {
);
Ok(Self {
audio_buffer,
buffer: Some(buffer),
buffer,
info,
phantom: PhantomData,
})
@ -225,9 +225,9 @@ impl AudioBuffer<Writable> {
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<T> {
// Has to be boxed because it contains self-references
audio_buffer: AudioBufferPtr,
buffer: Option<T>,
info: crate::AudioInfo,
unmap: bool,
phantom: PhantomData<T>,
}
impl<T> AudioBufferRef<T> {
@ -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> {

View file

@ -9,7 +9,7 @@ pub enum Writable {}
pub struct VideoFrame<T> {
frame: ffi::GstVideoFrame,
buffer: Option<gst::Buffer>,
buffer: gst::Buffer,
info: crate::VideoInfo,
phantom: PhantomData<T>,
}
@ -41,8 +41,9 @@ impl<T> VideoFrame<T> {
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<T> VideoFrame<T> {
let buffer = gst::Buffer::from_glib_none(frame.buffer);
Self {
frame,
buffer: Some(buffer),
buffer,
info,
phantom: PhantomData,
}
@ -234,9 +235,9 @@ impl<T> VideoFrame<T> {
let info = self.info.clone();
VideoFrameRef {
frame,
buffer: Some(self.buffer()),
info,
unmap: false,
phantom: PhantomData,
}
}
@ -245,9 +246,7 @@ impl<T> VideoFrame<T> {
}
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<Readable> {
let info = crate::VideoInfo(ptr::read(&frame.info));
Ok(Self {
frame,
buffer: Some(buffer),
buffer,
info,
phantom: PhantomData,
})
@ -318,7 +317,7 @@ impl VideoFrame<Readable> {
let info = crate::VideoInfo(ptr::read(&frame.info));
Ok(Self {
frame,
buffer: Some(buffer),
buffer,
info,
phantom: PhantomData,
})
@ -358,7 +357,7 @@ impl VideoFrame<Writable> {
let info = crate::VideoInfo(ptr::read(&frame.info));
Ok(Self {
frame,
buffer: Some(buffer),
buffer,
info,
phantom: PhantomData,
})
@ -394,7 +393,7 @@ impl VideoFrame<Writable> {
let info = crate::VideoInfo(ptr::read(&frame.info));
Ok(Self {
frame,
buffer: Some(buffer),
buffer,
info,
phantom: PhantomData,
})
@ -452,9 +451,9 @@ impl VideoFrame<Writable> {
let info = self.info.clone();
VideoFrameRef {
frame,
buffer: Some(self.buffer_mut()),
info,
unmap: false,
phantom: PhantomData,
}
}
@ -466,9 +465,9 @@ impl VideoFrame<Writable> {
#[derive(Debug)]
pub struct VideoFrameRef<T> {
frame: ffi::GstVideoFrame,
buffer: Option<T>,
info: crate::VideoInfo,
unmap: bool,
phantom: PhantomData<T>,
}
impl<T> VideoFrameRef<T> {
@ -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> {

View file

@ -28,7 +28,7 @@ pub struct BufferMap<'a, T> {
}
pub struct MappedBuffer<T> {
buffer: Option<Buffer>,
buffer: Buffer,
map_info: ffi::GstMapInfo,
phantom: PhantomData<T>,
}
@ -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<T> MappedBuffer<T> {
#[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<Writable> {
impl<T> Drop for MappedBuffer<T> {
fn drop(&mut self) {
if let Some(ref buffer) = self.buffer {
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> {
memory: Option<Memory>,
memory: Memory,
map_info: ffi::GstMapInfo,
phantom: PhantomData<T>,
}
@ -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<T> MappedMemory<T> {
#[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<Writable> {
impl<T> Drop for MappedMemory<T> {
fn drop(&mut self) {
if let Some(ref memory) = self.memory {
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)]
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();