diff options
author | 2024-10-23 05:40:01 +0000 | |
---|---|---|
committer | 2024-10-23 05:40:01 +0000 | |
commit | 5da35fa888dc1c2ee0d9ceacaab2c0af2891063d (patch) | |
tree | 640e94883d55b5c58ff984169190d3433605e9c2 | |
parent | 5103a383b905bd75b6ee92e312b568f1908a6cf5 (diff) | |
parent | 9639e124c859d3c0586e948110be4c5f8bdca420 (diff) |
Merge "Add conversions between NativeHandle and AIDL NativeHandle." into main
-rw-r--r-- | libs/nativewindow/rust/Android.bp | 1 | ||||
-rw-r--r-- | libs/nativewindow/rust/src/handle.rs | 35 |
2 files changed, 36 insertions, 0 deletions
diff --git a/libs/nativewindow/rust/Android.bp b/libs/nativewindow/rust/Android.bp index c572ee7811..faab48b1ce 100644 --- a/libs/nativewindow/rust/Android.bp +++ b/libs/nativewindow/rust/Android.bp @@ -110,6 +110,7 @@ rust_defaults { name: "libnativewindow_defaults", srcs: ["src/lib.rs"], rustlibs: [ + "android.hardware.common-V2-rust", "libbinder_rs", "libbitflags", "libnativewindow_bindgen", diff --git a/libs/nativewindow/rust/src/handle.rs b/libs/nativewindow/rust/src/handle.rs index c41ab8d1b8..ee70e3f538 100644 --- a/libs/nativewindow/rust/src/handle.rs +++ b/libs/nativewindow/rust/src/handle.rs @@ -12,6 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +use android_hardware_common::{ + aidl::android::hardware::common::NativeHandle::NativeHandle as AidlNativeHandle, + binder::ParcelFileDescriptor, +}; use std::{ ffi::c_int, mem::forget, @@ -190,6 +194,21 @@ impl Drop for NativeHandle { } } +impl From<AidlNativeHandle> for NativeHandle { + fn from(aidl_native_handle: AidlNativeHandle) -> Self { + let fds = aidl_native_handle.fds.into_iter().map(OwnedFd::from).collect(); + Self::new(fds, &aidl_native_handle.ints).unwrap() + } +} + +impl From<NativeHandle> for AidlNativeHandle { + fn from(native_handle: NativeHandle) -> Self { + let ints = native_handle.ints().to_owned(); + let fds = native_handle.into_fds().into_iter().map(ParcelFileDescriptor::new).collect(); + Self { ints, fds } + } +} + // SAFETY: `NativeHandle` owns the `native_handle_t`, which just contains some integers and file // descriptors, which aren't tied to any particular thread. unsafe impl Send for NativeHandle {} @@ -240,4 +259,20 @@ mod test { drop(cloned); } + + #[test] + fn to_from_aidl() { + let file = File::open("/dev/null").unwrap(); + let original = NativeHandle::new(vec![file.into()], &[42]).unwrap(); + assert_eq!(original.ints(), &[42]); + assert_eq!(original.fds().len(), 1); + + let aidl = AidlNativeHandle::from(original); + assert_eq!(&aidl.ints, &[42]); + assert_eq!(aidl.fds.len(), 1); + + let converted_back = NativeHandle::from(aidl); + assert_eq!(converted_back.ints(), &[42]); + assert_eq!(converted_back.fds().len(), 1); + } } |