From b5822d0e32240a900d5ae2cb18ea6c5a881469ee Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Wed, 16 Mar 2022 23:46:57 +0100 Subject: [PATCH] ndk: Remove on_success closure argument from `fn from_status()` (#243) By far the most calls of this function pass a closure that returns the void type. All these default calls can be simplified by always returning the void type in `Result<()>` from `fn from_status()`, and use the standard `Resuld::map(|()| ...)` to provide a different ok-value in the few cases an actual return value is desired. --- ndk/src/bitmap.rs | 8 ++++---- ndk/src/media/error.rs | 7 ++----- ndk/src/media/image_reader.rs | 6 +++--- ndk/src/media/media_codec.rs | 30 +++++++++++++++--------------- ndk/src/media/mod.rs | 2 +- 5 files changed, 25 insertions(+), 28 deletions(-) diff --git a/ndk/src/bitmap.rs b/ndk/src/bitmap.rs index 07949c03..d4e29dab 100644 --- a/ndk/src/bitmap.rs +++ b/ndk/src/bitmap.rs @@ -19,9 +19,9 @@ pub enum BitmapError { pub type BitmapResult = std::result::Result; impl BitmapError { - pub(crate) fn from_status(status: i32, on_success: impl FnOnce() -> T) -> BitmapResult { + pub(crate) fn from_status(status: i32) -> BitmapResult<()> { Err(match status { - ffi::ANDROID_BITMAP_RESULT_SUCCESS => return Ok(on_success()), + ffi::ANDROID_BITMAP_RESULT_SUCCESS => return Ok(()), ffi::ANDROID_BITMAP_RESULT_ALLOCATION_FAILED => BitmapError::AllocationFailed, ffi::ANDROID_BITMAP_RESULT_BAD_PARAMETER => BitmapError::BadParameter, ffi::ANDROID_BITMAP_RESULT_JNI_EXCEPTION => BitmapError::JniException, @@ -33,7 +33,7 @@ impl BitmapError { fn construct(with_ptr: impl FnOnce(*mut T) -> i32) -> BitmapResult { let mut result = MaybeUninit::uninit(); let status = with_ptr(result.as_mut_ptr()); - BitmapError::from_status(status, || unsafe { result.assume_init() }) + BitmapError::from_status(status).map(|()| unsafe { result.assume_init() }) } // IntoPrimitive, TryFromPrimitive use the deprecated `RGBA_4444` member below, @@ -86,7 +86,7 @@ impl AndroidBitmap { pub fn unlock_pixels(&self) -> BitmapResult<()> { let status = unsafe { ffi::AndroidBitmap_unlockPixels(self.env, self.inner) }; - BitmapError::from_status(status, || ()) + BitmapError::from_status(status) } #[cfg(all(feature = "hardware_buffer", feature = "api-level-30"))] diff --git a/ndk/src/media/error.rs b/ndk/src/media/error.rs index ecc3da7f..b864f4fa 100644 --- a/ndk/src/media/error.rs +++ b/ndk/src/media/error.rs @@ -42,13 +42,10 @@ pub enum NdkMediaError { } impl NdkMediaError { - pub(crate) fn from_status( - status: ffi::media_status_t, - on_success: impl FnOnce() -> T, - ) -> Result { + pub(crate) fn from_status(status: ffi::media_status_t) -> Result<()> { use MediaErrorResult::*; let result = match status { - ffi::media_status_t_AMEDIA_OK => return Ok(on_success()), + ffi::media_status_t_AMEDIA_OK => return Ok(()), ffi::media_status_t_AMEDIACODEC_ERROR_INSUFFICIENT_RESOURCE => { CodecErrorInsufficientResource } diff --git a/ndk/src/media/image_reader.rs b/ndk/src/media/image_reader.rs index 320b998c..5affdaba 100644 --- a/ndk/src/media/image_reader.rs +++ b/ndk/src/media/image_reader.rs @@ -124,7 +124,7 @@ impl ImageReader { onImageAvailable: Some(on_image_available), }; let status = unsafe { ffi::AImageReader_setImageListener(self.as_ptr(), &mut listener) }; - NdkMediaError::from_status(status, || ()) + NdkMediaError::from_status(status) } #[cfg(feature = "hardware_buffer")] @@ -152,7 +152,7 @@ impl ImageReader { }; let status = unsafe { ffi::AImageReader_setBufferRemovedListener(self.as_ptr(), &mut listener) }; - NdkMediaError::from_status(status, || ()) + NdkMediaError::from_status(status) } pub fn get_window(&self) -> Result { @@ -272,7 +272,7 @@ impl Image { ) }; - NdkMediaError::from_status(status, || unsafe { + NdkMediaError::from_status(status).map(|()| unsafe { std::slice::from_raw_parts(result_ptr.assume_init(), result_len.assume_init() as _) }) } diff --git a/ndk/src/media/media_codec.rs b/ndk/src/media/media_codec.rs index bf777486..ea971124 100644 --- a/ndk/src/media/media_codec.rs +++ b/ndk/src/media/media_codec.rs @@ -177,7 +177,7 @@ impl MediaFormat { impl Drop for MediaFormat { fn drop(&mut self) { let status = unsafe { ffi::AMediaFormat_delete(self.as_ptr()) }; - NdkMediaError::from_status(status, || ()).unwrap(); + NdkMediaError::from_status(status).unwrap(); } } @@ -235,7 +235,7 @@ impl MediaCodec { }, ) }; - NdkMediaError::from_status(status, || ()) + NdkMediaError::from_status(status) } #[cfg(feature = "api-level-26")] @@ -277,7 +277,7 @@ impl MediaCodec { index: result as ffi::size_t, })) } else { - NdkMediaError::from_status(result as ffi::media_status_t, || None) + NdkMediaError::from_status(result as ffi::media_status_t).map(|()| None) } } @@ -305,13 +305,13 @@ impl MediaCodec { info, })) } else { - NdkMediaError::from_status(result as ffi::media_status_t, || None) + NdkMediaError::from_status(result as ffi::media_status_t).map(|()| None) } } pub fn flush(&self) -> Result<()> { let status = unsafe { ffi::AMediaCodec_flush(self.as_ptr()) }; - NdkMediaError::from_status(status, || ()) + NdkMediaError::from_status(status) } #[cfg(feature = "api-level-28")] @@ -367,14 +367,14 @@ impl MediaCodec { flags, ) }; - NdkMediaError::from_status(status, || ()) + NdkMediaError::from_status(status) } pub fn release_output_buffer(&self, buffer: OutputBuffer, render: bool) -> Result<()> { let status = unsafe { ffi::AMediaCodec_releaseOutputBuffer(self.as_ptr(), buffer.index as ffi::size_t, render) }; - NdkMediaError::from_status(status, || ()) + NdkMediaError::from_status(status) } pub fn release_output_buffer_at_time( @@ -389,49 +389,49 @@ impl MediaCodec { timestamp_ns, ) }; - NdkMediaError::from_status(status, || ()) + NdkMediaError::from_status(status) } #[cfg(feature = "api-level-26")] pub fn set_input_surface(&self, surface: NativeWindow) -> Result<()> { let status = unsafe { ffi::AMediaCodec_setInputSurface(self.as_ptr(), surface.ptr().as_ptr()) }; - NdkMediaError::from_status(status, || ()) + NdkMediaError::from_status(status) } pub fn set_output_surface(&self, surface: NativeWindow) -> Result<()> { let status = unsafe { ffi::AMediaCodec_setOutputSurface(self.as_ptr(), surface.ptr().as_ptr()) }; - NdkMediaError::from_status(status, || ()) + NdkMediaError::from_status(status) } #[cfg(feature = "api-level-26")] pub fn set_parameters(&self, params: MediaFormat) -> Result<()> { let status = unsafe { ffi::AMediaCodec_setParameters(self.as_ptr(), params.as_ptr()) }; - NdkMediaError::from_status(status, || ()) + NdkMediaError::from_status(status) } #[cfg(feature = "api-level-26")] pub fn set_signal_end_of_input_stream(&self) -> Result<()> { let status = unsafe { ffi::AMediaCodec_signalEndOfInputStream(self.as_ptr()) }; - NdkMediaError::from_status(status, || ()) + NdkMediaError::from_status(status) } pub fn start(&self) -> Result<()> { let status = unsafe { ffi::AMediaCodec_start(self.as_ptr()) }; - NdkMediaError::from_status(status, || ()) + NdkMediaError::from_status(status) } pub fn stop(&self) -> Result<()> { let status = unsafe { ffi::AMediaCodec_stop(self.as_ptr()) }; - NdkMediaError::from_status(status, || ()) + NdkMediaError::from_status(status) } } impl Drop for MediaCodec { fn drop(&mut self) { let status = unsafe { ffi::AMediaCodec_delete(self.as_ptr()) }; - NdkMediaError::from_status(status, || ()).unwrap(); + NdkMediaError::from_status(status).unwrap(); } } diff --git a/ndk/src/media/mod.rs b/ndk/src/media/mod.rs index 1482c341..5e7d93f6 100644 --- a/ndk/src/media/mod.rs +++ b/ndk/src/media/mod.rs @@ -15,7 +15,7 @@ pub type Result = std::result::Result; fn construct(with_ptr: impl FnOnce(*mut T) -> ffi::camera_status_t) -> Result { let mut result = MaybeUninit::uninit(); let status = with_ptr(result.as_mut_ptr()); - NdkMediaError::from_status(status, || unsafe { result.assume_init() }) + NdkMediaError::from_status(status).map(|()| unsafe { result.assume_init() }) } fn construct_never_null(