gstreamer-base/adapter: Replace various error cases with Rust assertions

These are assertions on the C side too and are programmer errors, not
something that can be handled gracefully.
This commit is contained in:
Sebastian Dröge 2020-12-07 10:38:25 +02:00
parent 1199e89dca
commit f6700f1ff1

View file

@ -14,13 +14,12 @@ use std::ops;
impl Adapter { impl Adapter {
pub fn copy(&self, offset: usize, dest: &mut [u8]) -> Result<(), glib::BoolError> { pub fn copy(&self, offset: usize, dest: &mut [u8]) -> Result<(), glib::BoolError> {
if offset assert!(
.checked_add(dest.len()) offset
.map(|end| end <= self.available()) .checked_add(dest.len())
!= Some(true) .map(|end| end <= self.available())
{ == Some(true)
return Err(glib::glib_bool_error!("Not enough data available")); );
}
if dest.is_empty() { if dest.is_empty() {
return Ok(()); return Ok(());
@ -40,9 +39,7 @@ impl Adapter {
} }
pub fn copy_bytes(&self, offset: usize, size: usize) -> Result<glib::Bytes, glib::BoolError> { pub fn copy_bytes(&self, offset: usize, size: usize) -> Result<glib::Bytes, glib::BoolError> {
if offset.checked_add(size).map(|end| end <= self.available()) != Some(true) { assert!(offset.checked_add(size).map(|end| end <= self.available()) == Some(true));
return Err(glib::glib_bool_error!("Not enough data available"));
}
if size == 0 { if size == 0 {
return Ok(glib::Bytes::from_static(&[])); return Ok(glib::Bytes::from_static(&[]));
@ -57,26 +54,21 @@ impl Adapter {
} }
} }
pub fn flush(&self, flush: usize) -> Result<(), glib::BoolError> { pub fn flush(&self, flush: usize) {
if flush > self.available() { assert!(flush <= self.available());
return Err(glib::glib_bool_error!("Not enough data available"));
if flush == 0 {
return;
} }
unsafe { unsafe {
ffi::gst_adapter_flush(self.to_glib_none().0, flush); ffi::gst_adapter_flush(self.to_glib_none().0, flush);
} }
Ok(())
} }
pub fn get_buffer(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> { pub fn get_buffer(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> {
if nbytes > self.available() { assert!(nbytes <= self.available());
return Err(glib::glib_bool_error!("Not enough data available")); assert!(nbytes != 0);
}
if nbytes == 0 {
return Ok(gst::Buffer::new());
}
unsafe { unsafe {
Option::<_>::from_glib_full(ffi::gst_adapter_get_buffer(self.to_glib_none().0, nbytes)) Option::<_>::from_glib_full(ffi::gst_adapter_get_buffer(self.to_glib_none().0, nbytes))
@ -85,13 +77,8 @@ impl Adapter {
} }
pub fn get_buffer_fast(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> { pub fn get_buffer_fast(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> {
if nbytes > self.available() { assert!(nbytes <= self.available());
return Err(glib::glib_bool_error!("Not enough data available")); assert!(nbytes != 0);
}
if nbytes == 0 {
return Ok(gst::Buffer::new());
}
unsafe { unsafe {
Option::<_>::from_glib_full(ffi::gst_adapter_get_buffer_fast( Option::<_>::from_glib_full(ffi::gst_adapter_get_buffer_fast(
@ -103,13 +90,8 @@ impl Adapter {
} }
pub fn get_buffer_list(&self, nbytes: usize) -> Result<gst::BufferList, glib::BoolError> { pub fn get_buffer_list(&self, nbytes: usize) -> Result<gst::BufferList, glib::BoolError> {
if nbytes > self.available() { assert!(nbytes <= self.available());
return Err(glib::glib_bool_error!("Not enough data available")); assert!(nbytes != 0);
}
if nbytes == 0 {
return Ok(gst::BufferList::new());
}
unsafe { unsafe {
Option::<_>::from_glib_full(ffi::gst_adapter_get_buffer_list( Option::<_>::from_glib_full(ffi::gst_adapter_get_buffer_list(
@ -121,13 +103,8 @@ impl Adapter {
} }
pub fn get_list(&self, nbytes: usize) -> Result<Vec<gst::Buffer>, glib::BoolError> { pub fn get_list(&self, nbytes: usize) -> Result<Vec<gst::Buffer>, glib::BoolError> {
if nbytes > self.available() { assert!(nbytes <= self.available());
return Err(glib::glib_bool_error!("Not enough data available")); assert!(nbytes != 0);
}
if nbytes == 0 {
return Ok(Vec::new());
}
unsafe { unsafe {
Ok(FromGlibPtrContainer::from_glib_full( Ok(FromGlibPtrContainer::from_glib_full(
@ -143,13 +120,9 @@ impl Adapter {
offset: usize, offset: usize,
size: usize, size: usize,
) -> Result<Option<usize>, glib::BoolError> { ) -> Result<Option<usize>, glib::BoolError> {
if offset.checked_add(size).map(|end| end <= self.available()) != Some(true) { assert!(offset.checked_add(size).map(|end| end <= self.available()) == Some(true));
return Err(glib::glib_bool_error!("Not enough data available")); assert!(size != 0);
} assert!(((!mask) & pattern) == 0);
if size == 0 || ((!mask) & pattern) != 0 {
return Ok(None);
}
unsafe { unsafe {
let ret = ffi::gst_adapter_masked_scan_uint32( let ret = ffi::gst_adapter_masked_scan_uint32(
@ -175,13 +148,9 @@ impl Adapter {
offset: usize, offset: usize,
size: usize, size: usize,
) -> Result<Option<(usize, u32)>, glib::BoolError> { ) -> Result<Option<(usize, u32)>, glib::BoolError> {
if offset.checked_add(size).map(|end| end <= self.available()) != Some(true) { assert!(offset.checked_add(size).map(|end| end <= self.available()) == Some(true));
return Err(glib::glib_bool_error!("Not enough data available")); assert!(size != 0);
} assert!(((!mask) & pattern) == 0);
if size == 0 || ((!mask) & pattern) != 0 {
return Ok(None);
}
unsafe { unsafe {
let mut value = mem::MaybeUninit::uninit(); let mut value = mem::MaybeUninit::uninit();
@ -205,13 +174,8 @@ impl Adapter {
} }
pub fn take_buffer(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> { pub fn take_buffer(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> {
if nbytes > self.available() { assert!(nbytes <= self.available());
return Err(glib::glib_bool_error!("Not enough data available")); assert!(nbytes != 0);
}
if nbytes == 0 {
return Ok(gst::Buffer::new());
}
unsafe { unsafe {
Option::<_>::from_glib_full(ffi::gst_adapter_take_buffer(self.to_glib_none().0, nbytes)) Option::<_>::from_glib_full(ffi::gst_adapter_take_buffer(self.to_glib_none().0, nbytes))
@ -220,13 +184,8 @@ impl Adapter {
} }
pub fn take_buffer_fast(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> { pub fn take_buffer_fast(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> {
if nbytes > self.available() { assert!(nbytes <= self.available());
return Err(glib::glib_bool_error!("Not enough data available")); assert!(nbytes != 0);
}
if nbytes == 0 {
return Ok(gst::Buffer::new());
}
unsafe { unsafe {
Option::<_>::from_glib_full(ffi::gst_adapter_take_buffer_fast( Option::<_>::from_glib_full(ffi::gst_adapter_take_buffer_fast(
@ -238,13 +197,8 @@ impl Adapter {
} }
pub fn take_buffer_list(&self, nbytes: usize) -> Result<gst::BufferList, glib::BoolError> { pub fn take_buffer_list(&self, nbytes: usize) -> Result<gst::BufferList, glib::BoolError> {
if nbytes > self.available() { assert!(nbytes <= self.available());
return Err(glib::glib_bool_error!("Not enough data available")); assert!(nbytes != 0);
}
if nbytes == 0 {
return Ok(gst::BufferList::new());
}
unsafe { unsafe {
Option::<_>::from_glib_full(ffi::gst_adapter_take_buffer_list( Option::<_>::from_glib_full(ffi::gst_adapter_take_buffer_list(
@ -256,13 +210,8 @@ impl Adapter {
} }
pub fn take_list(&self, nbytes: usize) -> Result<Vec<gst::Buffer>, glib::BoolError> { pub fn take_list(&self, nbytes: usize) -> Result<Vec<gst::Buffer>, glib::BoolError> {
if nbytes > self.available() { assert!(nbytes <= self.available());
return Err(glib::glib_bool_error!("Not enough data available")); assert!(nbytes != 0);
}
if nbytes == 0 {
return Ok(Vec::new());
}
unsafe { unsafe {
Ok(FromGlibPtrContainer::from_glib_full( Ok(FromGlibPtrContainer::from_glib_full(
@ -300,8 +249,8 @@ impl io::Read for Adapter {
self.copy(0, &mut buf[0..len]) self.copy(0, &mut buf[0..len])
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
// Can't happen because we checked the size first self.flush(len);
self.flush(len).expect("Failed to flush");
Ok(len) Ok(len)
} }
} }
@ -345,8 +294,8 @@ impl UniqueAdapter {
self.0.dts_at_discont() self.0.dts_at_discont()
} }
pub fn flush(&mut self, flush: usize) -> Result<(), glib::BoolError> { pub fn flush(&mut self, flush: usize) {
self.0.flush(flush) self.0.flush(flush);
} }
pub fn get_buffer(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> { pub fn get_buffer(&self, nbytes: usize) -> Result<gst::Buffer, glib::BoolError> {
@ -444,23 +393,18 @@ impl UniqueAdapter {
} }
pub fn map(&mut self, nbytes: usize) -> Result<UniqueAdapterMap, glib::error::BoolError> { pub fn map(&mut self, nbytes: usize) -> Result<UniqueAdapterMap, glib::error::BoolError> {
assert!(nbytes <= self.available());
assert!(nbytes != 0);
use std::slice; use std::slice;
if nbytes > self.available() {
return Err(glib::glib_bool_error!("Not enough data available"));
}
if nbytes == 0 {
return Ok(UniqueAdapterMap(None, &[]));
}
unsafe { unsafe {
let ptr = ffi::gst_adapter_map(self.0.to_glib_none().0, nbytes); let ptr = ffi::gst_adapter_map(self.0.to_glib_none().0, nbytes);
if ptr.is_null() { if ptr.is_null() {
Err(glib::glib_bool_error!("size bytes are not available")) Err(glib::glib_bool_error!("size bytes are not available"))
} else { } else {
Ok(UniqueAdapterMap( Ok(UniqueAdapterMap(
Some(self), self,
slice::from_raw_parts(ptr as *const u8, nbytes), slice::from_raw_parts(ptr as *const u8, nbytes),
)) ))
} }
@ -469,14 +413,12 @@ impl UniqueAdapter {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct UniqueAdapterMap<'a>(Option<&'a UniqueAdapter>, &'a [u8]); pub struct UniqueAdapterMap<'a>(&'a UniqueAdapter, &'a [u8]);
impl<'a> Drop for UniqueAdapterMap<'a> { impl<'a> Drop for UniqueAdapterMap<'a> {
fn drop(&mut self) { fn drop(&mut self) {
if let Some(adapter) = self.0 { unsafe {
unsafe { ffi::gst_adapter_unmap((self.0).0.to_glib_none().0);
ffi::gst_adapter_unmap(adapter.0.to_glib_none().0);
}
} }
} }
} }