Skip to content

Commit

Permalink
ndk: Remove on_success closure argument from fn from_status() (#243)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MarijnS95 authored Mar 16, 2022
1 parent a6f3e13 commit b5822d0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
8 changes: 4 additions & 4 deletions ndk/src/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub enum BitmapError {
pub type BitmapResult<T, E = BitmapError> = std::result::Result<T, E>;

impl BitmapError {
pub(crate) fn from_status<T>(status: i32, on_success: impl FnOnce() -> T) -> BitmapResult<T> {
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,
Expand All @@ -33,7 +33,7 @@ impl BitmapError {
fn construct<T>(with_ptr: impl FnOnce(*mut T) -> i32) -> BitmapResult<T> {
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,
Expand Down Expand Up @@ -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"))]
Expand Down
7 changes: 2 additions & 5 deletions ndk/src/media/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ pub enum NdkMediaError {
}

impl NdkMediaError {
pub(crate) fn from_status<T>(
status: ffi::media_status_t,
on_success: impl FnOnce() -> T,
) -> Result<T> {
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
}
Expand Down
6 changes: 3 additions & 3 deletions ndk/src/media/image_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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<NativeWindow> {
Expand Down Expand Up @@ -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 _)
})
}
Expand Down
30 changes: 15 additions & 15 deletions ndk/src/media/media_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -235,7 +235,7 @@ impl MediaCodec {
},
)
};
NdkMediaError::from_status(status, || ())
NdkMediaError::from_status(status)
}

#[cfg(feature = "api-level-26")]
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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(
Expand All @@ -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();
}
}

Expand Down
2 changes: 1 addition & 1 deletion ndk/src/media/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub type Result<T, E = NdkMediaError> = std::result::Result<T, E>;
fn construct<T>(with_ptr: impl FnOnce(*mut T) -> ffi::camera_status_t) -> Result<T> {
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<T>(
Expand Down

0 comments on commit b5822d0

Please sign in to comment.