binder::binder_impl

Struct Binder

Source
#[repr(C)]
pub struct Binder<T: Remotable> { /* private fields */ }
Expand description

Rust wrapper around Binder remotable objects.

Implements the C++ BBinder class, and therefore implements the C++ IBinder interface.

Implementations§

Source§

impl<T: Remotable> Binder<T>

Source

pub fn new(rust_object: T) -> Binder<T>

Create a new Binder remotable object with default stability

This moves the rust_object into an owned Box and Binder will manage its lifetime.

Source

pub fn new_with_stability(rust_object: T, stability: Stability) -> Binder<T>

Create a new Binder remotable object with the given stability

This moves the rust_object into an owned Box and Binder will manage its lifetime.

Source

pub fn set_extension( &mut self, extension: &mut SpIBinder, ) -> Result<(), StatusCode>

Set the extension of a binder interface. This allows a downstream developer to add an extension to an interface without modifying its interface file. This should be called immediately when the object is created before it is passed to another thread.

§Examples

For instance, imagine if we have this Binder AIDL interface definition: interface IFoo { void doFoo(); }

If an unrelated owner (perhaps in a downstream codebase) wants to make a change to the interface, they have two options:

  1. Historical option that has proven to be BAD! Only the original author of an interface should change an interface. If someone downstream wants additional functionality, they should not ever change the interface or use this method.

    BAD TO DO:  interface IFoo {                       BAD TO DO
    BAD TO DO:      void doFoo();                      BAD TO DO
    BAD TO DO: +    void doBar(); // adding a method   BAD TO DO
    BAD TO DO:  }                                      BAD TO DO
  2. Option that this method enables! Leave the original interface unchanged (do not change IFoo!). Instead, create a new AIDL interface in a downstream package:

    package com.<name>; // new functionality in a new package
    interface IBar { void doBar(); }

    When registering the interface, add:

    let mut foo: Binder<MyFoo> = Binder::new(my_foo); // class in AOSP codebase
    let bar: Binder<MyBar> = Binder::new(my_bar);     // custom extension class
    foo.set_extension(&mut bar.as_binder());          // use method in Binder

    Then, clients of IFoo can get this extension:

    if let Some(barBinder) = binder.get_extension()? {
        let bar = BpBar::new(barBinder)
            .expect("Extension was not of type IBar");
    } else {
        // There was no extension
    }
Source

pub fn get_descriptor() -> &'static str

Retrieve the interface descriptor string for this object’s Binder interface.

Trait Implementations§

Source§

impl<T: Remotable> Deref for Binder<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: Remotable> Drop for Binder<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Remotable> Interface for Binder<T>

Source§

fn as_binder(&self) -> SpIBinder

Converts the local remotable object into a generic SpIBinder reference.

The resulting SpIBinder will hold its own strong reference to this remotable object, which will prevent the object from being dropped while the SpIBinder is alive.

Source§

fn dump( &self, _writer: &mut dyn Write, _args: &[&CStr], ) -> Result<(), StatusCode>

Dump transaction handler for this Binder object. Read more
Source§

impl<B: Remotable> Serialize for Binder<B>

Source§

fn serialize(&self, parcel: &mut BorrowedParcel<'_>) -> Result<(), StatusCode>

Serialize this instance into the given crate::parcel::Parcel.
Source§

impl<B: Remotable> TryFrom<SpIBinder> for Binder<B>

Source§

type Error = android_c_interface_StatusCode

The type returned in the event of a conversion error.
Source§

fn try_from(ibinder: SpIBinder) -> Result<Self, StatusCode>

Performs the conversion.
Source§

impl<T: Remotable> Send for Binder<T>

Safety:

A Binder<T> is a pair of unique owning pointers to two values:

  • a C++ ABBinder which the C++ API guarantees can be passed between threads
  • a Rust object which implements Remotable; this trait requires Send + Sync

Both pointers are unique (never escape the Binder<T> object and are not copied) so we can essentially treat Binder<T> as a box-like containing the two objects; the box-like object inherits Send from the two inner values, similarly to how Box<T> is Send if T is Send.

Source§

impl<T: Remotable> Sync for Binder<T>

Safety:

A Binder<T> is a pair of unique owning pointers to two values:

  • a C++ ABBinder which is thread-safe, i.e. Send + Sync
  • a Rust object which implements Remotable; this trait requires Send + Sync

ABBinder contains an immutable mUserData pointer, which is actually a pointer to a boxed T: Remotable, which is Sync. ABBinder also contains a mutable pointer to its class, but mutation of this field is controlled by a mutex and it is only allowed to be set once, therefore we can concurrently access this field safely. ABBinder inherits from BBinder, which is also thread-safe. Thus ABBinder is thread-safe.

Both pointers are unique (never escape the Binder<T> object and are not copied) so we can essentially treat Binder<T> as a box-like containing the two objects; the box-like object inherits Sync from the two inner values, similarly to how Box<T> is Sync if T is Sync.

Auto Trait Implementations§

§

impl<T> Freeze for Binder<T>

§

impl<T> RefUnwindSafe for Binder<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for Binder<T>

§

impl<T> UnwindSafe for Binder<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> IBinder for T
where T: AsNative<AIBinder>,

Register the recipient for a notification if this binder goes away. If this binder object unexpectedly goes away (typically because its hosting process has been killed), then the DeathRecipient’s callback will be called. Read more
Remove a previously registered death notification. The recipient will no longer be called if this object dies.
Source§

fn ping_binder(&mut self) -> Result<(), android_c_interface_StatusCode>

Send a ping transaction to this object
Source§

impl<T> IBinderInternal for T
where T: AsNative<AIBinder>,

Source§

fn prepare_transact(&self) -> Result<Parcel, android_c_interface_StatusCode>

Create a Parcel that can be used with submit_transact.
Source§

fn submit_transact( &self, code: u32, data: Parcel, flags: u32, ) -> Result<Parcel, android_c_interface_StatusCode>

Perform a generic operation with the object. Read more
Source§

fn is_binder_alive(&self) -> bool

Is this object still alive?
Source§

fn set_requesting_sid(&mut self, enable: bool)

Indicate that the service intends to receive caller security contexts.
Source§

fn dump<F>( &mut self, fp: &F, args: &[&str], ) -> Result<(), android_c_interface_StatusCode>
where F: AsRawFd,

Dump this object to the given file handle
Source§

fn get_extension( &mut self, ) -> Result<Option<SpIBinder>, android_c_interface_StatusCode>

Get a new interface that exposes additional extension functionality, if available.
Source§

fn transact<F: FnOnce(BorrowedParcel<'_>) -> Result<(), StatusCode>>( &self, code: TransactionCode, flags: TransactionFlags, input_callback: F, ) -> Result<Parcel, StatusCode>

Perform a generic operation with the object. This is a convenience method that internally calls prepare_transact followed by `submit_transact. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.