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

Commit 994a0f0d authored by Stephen Crane's avatar Stephen Crane Committed by Matthew Maurer
Browse files

Add trait implementations for Rust binder crate

Implements Error for StatusCode in the bindgen generated bindings, which
makes usage simpler. Implements Eq for SpIBinder. Exposes a method to
promote weak binder references to strong ones.

Bug: 161559357
Test: m libbinder_rs
Change-Id: Ia2f891c2f0f84ed0454039169ed42ab20401c1a4
parent aae76385
Loading
Loading
Loading
Loading
+17 −5
Original line number Diff line number Diff line
@@ -3,12 +3,24 @@ rust_library {
    crate_name: "binder",
    srcs: ["src/lib.rs"],
    shared_libs: [
        "libbinder_ndk",
        "libutils",
    ],
    rustlibs: [
        "liblibc",
        "libbinder_ndk_bindgen",
        "libbinder_ndk_sys",
    ],
    host_supported: true,
}

rust_library {
    name: "libbinder_ndk_sys",
    crate_name: "binder_ndk_sys",
    srcs: [
        "sys/lib.rs",
        ":libbinder_ndk_bindgen",
    ],
    shared_libs: [
        "libbinder_ndk",
    ],
    host_supported: true,
}
@@ -16,8 +28,8 @@ rust_library {
rust_bindgen {
    name: "libbinder_ndk_bindgen",
    crate_name: "binder_ndk_bindgen",
    wrapper_src: "BinderBindings.h",
    source_stem: "ndk_bindings",
    wrapper_src: "sys/BinderBindings.h",
    source_stem: "bindings",
    cflags: [
        "-x c++",
    ],
@@ -69,6 +81,6 @@ rust_test {
    ],
    rustlibs: [
        "liblibc",
        "libbinder_ndk_bindgen",
        "libbinder_ndk_sys",
    ],
}
+0 −8
Original line number Diff line number Diff line
@@ -43,14 +43,6 @@ pub fn status_result(status: status_t) -> Result<()> {
    }
}

// impl Display for StatusCode {
//     fn fmt(&self, f: &mut Formatter) -> FmtResult {
//         write!(f, "StatusCode::{:?}", self)
//     }
// }

// impl error::Error for StatusCode {}

fn parse_status_code(code: i32) -> StatusCode {
    match code {
        e if e == StatusCode::OK as i32 => StatusCode::OK,
+1 −1
Original line number Diff line number Diff line
@@ -102,7 +102,7 @@ mod error;
mod native;
mod state;

use binder_ndk_bindgen as sys;
use binder_ndk_sys as sys;

pub mod parcel;

+20 −0
Original line number Diff line number Diff line
@@ -122,6 +122,14 @@ impl AssociateClass for SpIBinder {
    }
}

impl PartialEq for SpIBinder {
    fn eq(&self, other: &Self) -> bool {
        ptr::eq(self.0, other.0)
    }
}

impl Eq for SpIBinder {}

impl Clone for SpIBinder {
    fn clone(&self) -> Self {
        unsafe {
@@ -363,6 +371,18 @@ impl WpIBinder {
        assert!(!ptr.is_null());
        Self(ptr)
    }

    /// Promote this weak reference to a strong reference to the binder object.
    pub fn promote(&self) -> Option<SpIBinder> {
        unsafe {
            // Safety: `WpIBinder` always contains a valid weak reference, so we
            // can pass this pointer to `AIBinder_Weak_promote`. Returns either
            // null or an AIBinder owned by the caller, both of which are valid
            // to pass to `SpIBinder::from_raw`.
            let ptr = sys::AIBinder_Weak_promote(self.0);
            SpIBinder::from_raw(ptr)
        }
    }
}

/// Rust wrapper around DeathRecipient objects.
Loading