From 94a140092ed01c1944a0d08819657de405cd288b Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Sat, 11 Jun 2022 12:02:23 +0200 Subject: [PATCH] ndk: Replace `posix` module with `std::io::Error::from_raw_os_error` Platform specific - i.e. POSIX - error codes can be wrapped in humand readable form by Rust's `std` through [`Error::from_raw_os_error()`]. Certain NDK interfaces use these codes directly from `errno.h` through [`Errors.h`]. [`Error::from_raw_os_error()`]: https://doc.rust-lang.org/std/io/struct.Error.html#method.from_raw_os_error [`Errors.h`]: https://cs.android.com/android/platform/superproject/+/master:system/core/libutils/include/utils/Errors.h --- ndk/src/lib.rs | 1 - ndk/src/posix.rs | 10 -------- ndk/src/surface_texture.rs | 50 +++++++++++++++++++++++--------------- 3 files changed, 30 insertions(+), 31 deletions(-) delete mode 100644 ndk/src/posix.rs diff --git a/ndk/src/lib.rs b/ndk/src/lib.rs index 29f267ea..8b6ec33f 100644 --- a/ndk/src/lib.rs +++ b/ndk/src/lib.rs @@ -27,6 +27,5 @@ pub mod looper; pub mod media; pub mod native_activity; pub mod native_window; -pub mod posix; pub mod surface_texture; pub mod trace; diff --git a/ndk/src/posix.rs b/ndk/src/posix.rs deleted file mode 100644 index c0f81817..00000000 --- a/ndk/src/posix.rs +++ /dev/null @@ -1,10 +0,0 @@ -use thiserror::Error; - -#[derive(Debug, Error)] -pub struct PosixError(pub i32); - -impl std::fmt::Display for PosixError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "Posix Error: {}", self.0) - } -} diff --git a/ndk/src/surface_texture.rs b/ndk/src/surface_texture.rs index 2d79bd77..60c68502 100644 --- a/ndk/src/surface_texture.rs +++ b/ndk/src/surface_texture.rs @@ -6,10 +6,14 @@ //! [`ASurfaceTexture`]: https://developer.android.com/ndk/reference/group/surface-texture #![cfg(feature = "api-level-28")] -use super::posix::PosixError; use crate::native_window::NativeWindow; use jni_sys::{jobject, JNIEnv}; -use std::{convert::TryInto, ptr::NonNull, time::Duration}; +use std::{ + convert::TryInto, + io::{Error, Result}, + ptr::NonNull, + time::Duration, +}; /// An opaque type to manage [`android.graphics.SurfaceTexture`] from native code /// @@ -83,12 +87,14 @@ impl SurfaceTexture { /// This can be used to access the [`SurfaceTexture`] image contents from multiple OpenGL ES /// contexts. Note, however, that the image contents are only accessible from one OpenGL ES /// context at a time. - pub fn attach_to_gl_context(&self, tex_name: u32) -> Result<(), PosixError> { - let r = unsafe { ffi::ASurfaceTexture_attachToGLContext(self.ptr.as_ptr(), tex_name) }; - if r == 0 { - Ok(()) - } else { - Err(PosixError(r)) + pub fn attach_to_gl_context(&self, tex_name: u32) -> Result<()> { + match unsafe { ffi::ASurfaceTexture_attachToGLContext(self.ptr.as_ptr(), tex_name) } { + 0 => Ok(()), + r if r < 0 => Err(Error::from_raw_os_error(-r)), + r => unreachable!( + "ASurfaceTexture_attachToGLContext returned positive integer {}", + r + ), } } @@ -103,12 +109,14 @@ impl SurfaceTexture { /// This can be used to access the [`SurfaceTexture`] image contents from multiple OpenGL ES /// contexts. Note, however, that the image contents are only accessible from one OpenGL ES /// context at a time. - pub fn detach_from_gl_context(&self) -> Result<(), PosixError> { - let r = unsafe { ffi::ASurfaceTexture_detachFromGLContext(self.ptr.as_ptr()) }; - if r == 0 { - Ok(()) - } else { - Err(PosixError(r)) + pub fn detach_from_gl_context(&self) -> Result<()> { + match unsafe { ffi::ASurfaceTexture_detachFromGLContext(self.ptr.as_ptr()) } { + 0 => Ok(()), + r if r < 0 => Err(Error::from_raw_os_error(-r)), + r => unreachable!( + "ASurfaceTexture_detachFromGLContext returned positive integer {}", + r + ), } } @@ -161,12 +169,14 @@ impl SurfaceTexture { /// This may only be called while the OpenGL ES context that owns the texture is current on the /// calling thread. It will implicitly bind its texture to the `GL_TEXTURE_EXTERNAL_OES` /// texture target. - pub fn update_tex_image(&self) -> Result<(), PosixError> { - let r = unsafe { ffi::ASurfaceTexture_updateTexImage(self.ptr.as_ptr()) }; - if r == 0 { - Ok(()) - } else { - Err(PosixError(r)) + pub fn update_tex_image(&self) -> Result<()> { + match unsafe { ffi::ASurfaceTexture_updateTexImage(self.ptr.as_ptr()) } { + 0 => Ok(()), + r if r < 0 => Err(Error::from_raw_os_error(-r)), + r => unreachable!( + "ASurfaceTexture_updateTexImage returned positive integer {}", + r + ), } } }