Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5da35fa8 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Add conversions between NativeHandle and AIDL NativeHandle." into main

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