diff options
| -rw-r--r-- | libs/binder/rust/src/proxy.rs | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/libs/binder/rust/src/proxy.rs b/libs/binder/rust/src/proxy.rs index cdd7c081d0..b03ed49e51 100644 --- a/libs/binder/rust/src/proxy.rs +++ b/libs/binder/rust/src/proxy.rs @@ -38,7 +38,7 @@ use std::ptr; /// /// This struct encapsulates the generic C++ `sp<IBinder>` class. This wrapper /// is untyped; typed interface access is implemented by the AIDL compiler. -pub struct SpIBinder(*mut sys::AIBinder); +pub struct SpIBinder(ptr::NonNull<sys::AIBinder>); impl fmt::Debug for SpIBinder { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -74,7 +74,7 @@ impl SpIBinder { /// to an `AIBinder`, which will remain valid for the entire lifetime of the /// `SpIBinder` (we keep a strong reference, and only decrement on drop). pub(crate) unsafe fn from_raw(ptr: *mut sys::AIBinder) -> Option<Self> { - ptr.as_mut().map(|p| Self(p)) + ptr::NonNull::new(ptr).map(Self) } /// Extract a raw `AIBinder` pointer from this wrapper. @@ -88,7 +88,7 @@ impl SpIBinder { /// The SpIBinder object retains ownership of the AIBinder and the caller /// should not attempt to free the returned pointer. pub unsafe fn as_raw(&self) -> *mut sys::AIBinder { - self.0 + self.0.as_ptr() } /// Return true if this binder object is hosted in a different process than @@ -176,13 +176,13 @@ impl Ord for SpIBinder { // Safety: SpIBinder always holds a valid `AIBinder` pointer, so // this pointer is always safe to pass to `AIBinder_lt` (null is // also safe to pass to this function, but we should never do that). - sys::AIBinder_lt(self.0, other.0) + sys::AIBinder_lt(self.0.as_ptr(), other.0.as_ptr()) }; let greater_than = unsafe { // Safety: SpIBinder always holds a valid `AIBinder` pointer, so // this pointer is always safe to pass to `AIBinder_lt` (null is // also safe to pass to this function, but we should never do that). - sys::AIBinder_lt(other.0, self.0) + sys::AIBinder_lt(other.0.as_ptr(), self.0.as_ptr()) }; if !less_than && !greater_than { Ordering::Equal @@ -202,7 +202,7 @@ impl PartialOrd for SpIBinder { impl PartialEq for SpIBinder { fn eq(&self, other: &Self) -> bool { - ptr::eq(self.0, other.0) + ptr::eq(self.0.as_ptr(), other.0.as_ptr()) } } @@ -214,7 +214,7 @@ impl Clone for SpIBinder { // Safety: Cloning a strong reference must increment the reference // count. We are guaranteed by the `SpIBinder` constructor // invariants that `self.0` is always a valid `AIBinder` pointer. - sys::AIBinder_incStrong(self.0); + sys::AIBinder_incStrong(self.0.as_ptr()); } Self(self.0) } @@ -443,7 +443,7 @@ impl DeserializeArray for Option<SpIBinder> {} /// /// This struct encapsulates the generic C++ `wp<IBinder>` class. This wrapper /// is untyped; typed interface access is implemented by the AIDL compiler. -pub struct WpIBinder(*mut sys::AIBinder_Weak); +pub struct WpIBinder(ptr::NonNull<sys::AIBinder_Weak>); impl fmt::Debug for WpIBinder { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -470,8 +470,7 @@ impl WpIBinder { // valid pointer to an `AIBinder`. sys::AIBinder_Weak_new(binder.as_native_mut()) }; - assert!(!ptr.is_null()); - Self(ptr) + Self(ptr::NonNull::new(ptr).expect("Unexpected null pointer from AIBinder_Weak_new")) } /// Promote this weak reference to a strong reference to the binder object. @@ -481,7 +480,7 @@ impl WpIBinder { // 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); + let ptr = sys::AIBinder_Weak_promote(self.0.as_ptr()); SpIBinder::from_raw(ptr) } } @@ -496,13 +495,9 @@ impl Clone for WpIBinder { // // We get ownership of the returned pointer, so can construct a new // WpIBinder object from it. - sys::AIBinder_Weak_clone(self.0) + sys::AIBinder_Weak_clone(self.0.as_ptr()) }; - assert!( - !ptr.is_null(), - "Unexpected null pointer from AIBinder_Weak_clone" - ); - Self(ptr) + Self(ptr::NonNull::new(ptr).expect("Unexpected null pointer from AIBinder_Weak_clone")) } } @@ -513,14 +508,14 @@ impl Ord for WpIBinder { // so this pointer is always safe to pass to `AIBinder_Weak_lt` // (null is also safe to pass to this function, but we should never // do that). - sys::AIBinder_Weak_lt(self.0, other.0) + sys::AIBinder_Weak_lt(self.0.as_ptr(), other.0.as_ptr()) }; let greater_than = unsafe { // Safety: WpIBinder always holds a valid `AIBinder_Weak` pointer, // so this pointer is always safe to pass to `AIBinder_Weak_lt` // (null is also safe to pass to this function, but we should never // do that). - sys::AIBinder_Weak_lt(other.0, self.0) + sys::AIBinder_Weak_lt(other.0.as_ptr(), self.0.as_ptr()) }; if !less_than && !greater_than { Ordering::Equal @@ -551,7 +546,7 @@ impl Drop for WpIBinder { unsafe { // Safety: WpIBinder always holds a valid `AIBinder_Weak` pointer, so we // know this pointer is safe to pass to `AIBinder_Weak_delete` here. - sys::AIBinder_Weak_delete(self.0); + sys::AIBinder_Weak_delete(self.0.as_ptr()); } } } @@ -716,10 +711,10 @@ pub fn wait_for_interface<T: FromIBinder + ?Sized>(name: &str) -> Result<Strong< /// `AIBinder`, so we can trivially extract this pointer here. unsafe impl AsNative<sys::AIBinder> for SpIBinder { fn as_native(&self) -> *const sys::AIBinder { - self.0 + self.0.as_ptr() } fn as_native_mut(&mut self) -> *mut sys::AIBinder { - self.0 + self.0.as_ptr() } } |