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

Commit eea65996 authored by Andrew Walbran's avatar Andrew Walbran
Browse files

Support Rust PersistableBundle in AIDL.

Bug: 389074518
Test: atest libbinder_rs-internal_test
Change-Id: I2ba487b6bd1ea18b2fc3eb331b1376cf20473408
parent cea15921
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -17,4 +17,4 @@


package android.os;
package android.os;


@JavaOnlyStableParcelable @NdkOnlyStableParcelable parcelable PersistableBundle cpp_header "binder/PersistableBundle.h" ndk_header "android/persistable_bundle_aidl.h";
@JavaOnlyStableParcelable @NdkOnlyStableParcelable @RustOnlyStableParcelable parcelable PersistableBundle cpp_header "binder/PersistableBundle.h" ndk_header "android/persistable_bundle_aidl.h" rust_type "binder::PersistableBundle";
+44 −2
Original line number Original line Diff line number Diff line
@@ -14,11 +14,18 @@
 * limitations under the License.
 * limitations under the License.
 */
 */


use crate::{
    binder::AsNative,
    error::{status_result, StatusCode},
    impl_deserialize_for_unstructured_parcelable, impl_serialize_for_unstructured_parcelable,
    parcel::{BorrowedParcel, UnstructuredParcelable},
};
use binder_ndk_sys::{
use binder_ndk_sys::{
    APersistableBundle, APersistableBundle_delete, APersistableBundle_dup,
    APersistableBundle, APersistableBundle_delete, APersistableBundle_dup,
    APersistableBundle_isEqual, APersistableBundle_new, APersistableBundle_size,
    APersistableBundle_isEqual, APersistableBundle_new, APersistableBundle_readFromParcel,
    APersistableBundle_size, APersistableBundle_writeToParcel,
};
};
use std::ptr::NonNull;
use std::ptr::{null_mut, NonNull};


/// A mapping from string keys to values of various types.
/// A mapping from string keys to values of various types.
#[derive(Debug)]
#[derive(Debug)]
@@ -42,6 +49,13 @@ impl PersistableBundle {
    }
    }
}
}


// SAFETY: The underlying *APersistableBundle can be moved between threads.
unsafe impl Send for PersistableBundle {}

// SAFETY: The underlying *APersistableBundle can be read from multiple threads, and we require
// `&mut PersistableBundle` for any operations which mutate it.
unsafe impl Sync for PersistableBundle {}

impl Default for PersistableBundle {
impl Default for PersistableBundle {
    fn default() -> Self {
    fn default() -> Self {
        Self::new()
        Self::new()
@@ -73,6 +87,34 @@ impl PartialEq for PersistableBundle {
    }
    }
}
}


impl UnstructuredParcelable for PersistableBundle {
    fn write_to_parcel(&self, parcel: &mut BorrowedParcel) -> Result<(), StatusCode> {
        let status =
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. `parcel.as_native_mut()` always returns a valid
        // parcel pointer.
            unsafe { APersistableBundle_writeToParcel(self.0.as_ptr(), parcel.as_native_mut()) };
        status_result(status)
    }

    fn from_parcel(parcel: &BorrowedParcel) -> Result<Self, StatusCode> {
        let mut bundle = null_mut();

        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. `parcel.as_native()` always returns a valid parcel
        // pointer.
        let status = unsafe { APersistableBundle_readFromParcel(parcel.as_native(), &mut bundle) };
        status_result(status)?;

        Ok(Self(NonNull::new(bundle).expect(
            "APersistableBundle_readFromParcel returned success but didn't allocate bundle",
        )))
    }
}

impl_deserialize_for_unstructured_parcelable!(PersistableBundle);
impl_serialize_for_unstructured_parcelable!(PersistableBundle);

#[cfg(test)]
#[cfg(test)]
mod test {
mod test {
    use super::*;
    use super::*;