From 8fe3ecc99b260169d35fab0db625be761a33377d Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Tue, 15 Dec 2020 11:29:58 +0000 Subject: Add WpIBinder to public_api, and implement Debug and Send. Bug: 175584883 Test: mm Change-Id: I412aba0ba4fa3002f4877cdafdb4bb61066d1dc2 --- libs/binder/rust/src/lib.rs | 2 +- libs/binder/rust/src/proxy.rs | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/libs/binder/rust/src/lib.rs b/libs/binder/rust/src/lib.rs index 8ee6a62180..edfb56a26c 100644 --- a/libs/binder/rust/src/lib.rs +++ b/libs/binder/rust/src/lib.rs @@ -122,7 +122,7 @@ pub mod public_api { pub use super::parcel::ParcelFileDescriptor; pub use super::{add_service, get_interface}; pub use super::{ - ExceptionCode, Interface, ProcessState, SpIBinder, Status, StatusCode, + ExceptionCode, Interface, ProcessState, SpIBinder, Status, StatusCode, WpIBinder, }; /// Binder result containing a [`Status`] on error. diff --git a/libs/binder/rust/src/proxy.rs b/libs/binder/rust/src/proxy.rs index 485bb422fa..9d612a43f4 100644 --- a/libs/binder/rust/src/proxy.rs +++ b/libs/binder/rust/src/proxy.rs @@ -102,6 +102,11 @@ impl SpIBinder { class.as_ref().map(|p| InterfaceClass::from_ptr(p)) } } + + /// Creates a new weak reference to this binder object. + pub fn downgrade(&mut self) -> WpIBinder { + WpIBinder::new(self) + } } /// An object that can be associate with an [`InterfaceClass`]. @@ -370,15 +375,25 @@ impl DeserializeArray for Option {} /// A weak reference to a Binder remote object. /// -/// This struct encapsulates the C++ `wp` class. However, this wrapper -/// is untyped, so properly typed versions implementing a particular binder -/// interface should be crated with [`declare_binder_interface!`]. +/// This struct encapsulates the generic C++ `wp` class. This wrapper +/// is untyped; typed interface access is implemented by the AIDL compiler. pub struct WpIBinder(*mut sys::AIBinder_Weak); +impl fmt::Debug for WpIBinder { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.pad("WpIBinder") + } +} + +/// # Safety +/// +/// A `WpIBinder` is a handle to a C++ IBinder, which is thread-safe. +unsafe impl Send for WpIBinder {} + impl WpIBinder { /// Create a new weak reference from an object that can be converted into a /// raw `AIBinder` pointer. - pub fn new>(binder: &mut B) -> WpIBinder { + fn new>(binder: &mut B) -> WpIBinder { let ptr = unsafe { // Safety: `SpIBinder` guarantees that `binder` always contains a // valid pointer to an `AIBinder`. -- cgit v1.2.3-59-g8ed1b