Loading libs/nativewindow/rust/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -110,6 +110,7 @@ rust_defaults { name: "libnativewindow_defaults", srcs: ["src/lib.rs"], rustlibs: [ "android.hardware.common-V2-rust", "libbinder_rs", "libbitflags", "libnativewindow_bindgen", Loading libs/nativewindow/rust/src/handle.rs +35 −0 Original line number Diff line number Diff line Loading @@ -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, Loading Loading @@ -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 {} Loading Loading @@ -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); } } Loading
libs/nativewindow/rust/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -110,6 +110,7 @@ rust_defaults { name: "libnativewindow_defaults", srcs: ["src/lib.rs"], rustlibs: [ "android.hardware.common-V2-rust", "libbinder_rs", "libbitflags", "libnativewindow_bindgen", Loading
libs/nativewindow/rust/src/handle.rs +35 −0 Original line number Diff line number Diff line Loading @@ -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, Loading Loading @@ -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 {} Loading Loading @@ -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); } }