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

Commit 083e3536 authored by Andrei Homescu's avatar Andrei Homescu
Browse files

binder_rs: Add null parcelable flags

This adds NON_NULL_PARCELABLE_FLAG and NULL_PARCELABLE_FLAG
to parcel.rs as equivalents to Parcel::kNonNullParcelableFlag
and Parcel::kNullParcelableFlag from C++, respectively.

Bug: 169035750
Test: m
Change-Id: If9aab541bb8ff7fec14bcd590af074f564712c23
parent 72b799d4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ mod parcelable;
pub use self::file_descriptor::ParcelFileDescriptor;
pub use self::parcelable::{
    Deserialize, DeserializeArray, DeserializeOption, Serialize, SerializeArray, SerializeOption,
    Parcelable,
    Parcelable, NON_NULL_PARCELABLE_FLAG, NULL_PARCELABLE_FLAG,
};

/// Container for a message (data and object references) that can be sent
+19 −7
Original line number Diff line number Diff line
@@ -185,6 +185,18 @@ unsafe extern "C" fn deserialize_element<T: Deserialize>(
    StatusCode::OK as status_t
}

/// Flag that specifies that the following parcelable is present.
///
/// This is the Rust equivalent of `Parcel::kNonNullParcelableFlag`
/// from `include/binder/Parcel.h` in C++.
pub const NON_NULL_PARCELABLE_FLAG: i32 = 1;

/// Flag that specifies that the following parcelable is absent.
///
/// This is the Rust equivalent of `Parcel::kNullParcelableFlag`
/// from `include/binder/Parcel.h` in C++.
pub const NULL_PARCELABLE_FLAG: i32 = 0;

/// Helper trait for types that can be nullable when serialized.
// We really need this trait instead of implementing `Serialize for Option<T>`
// because of the Rust orphan rule which prevents us from doing
@@ -196,10 +208,10 @@ pub trait SerializeOption: Serialize {
    /// Serialize an Option of this type into the given [`Parcel`].
    fn serialize_option(this: Option<&Self>, parcel: &mut Parcel) -> Result<()> {
        if let Some(inner) = this {
            parcel.write(&1i32)?;
            parcel.write(&NON_NULL_PARCELABLE_FLAG)?;
            parcel.write(inner)
        } else {
            parcel.write(&0i32)
            parcel.write(&NULL_PARCELABLE_FLAG)
        }
    }
}
@@ -209,7 +221,7 @@ pub trait DeserializeOption: Deserialize {
    /// Deserialize an Option of this type from the given [`Parcel`].
    fn deserialize_option(parcel: &Parcel) -> Result<Option<Self>> {
        let null: i32 = parcel.read()?;
        if null == 0 {
        if null == NULL_PARCELABLE_FLAG {
            Ok(None)
        } else {
            parcel.read().map(Some)
@@ -765,10 +777,10 @@ macro_rules! impl_serialize_for_parcelable {
            ) -> $crate::Result<()> {
                if let Some(this) = this {
                    use $crate::parcel::Parcelable;
                    parcel.write(&1i32)?;
                    parcel.write(&$crate::parcel::NON_NULL_PARCELABLE_FLAG)?;
                    this.write_to_parcel(parcel)
                } else {
                    parcel.write(&0i32)
                    parcel.write(&$crate::parcel::NULL_PARCELABLE_FLAG)
                }
            }
        }
@@ -798,7 +810,7 @@ macro_rules! impl_deserialize_for_parcelable {
                parcel: &$crate::parcel::Parcel,
            ) -> $crate::Result<()> {
                let status: i32 = parcel.read()?;
                if status == 0 {
                if status == $crate::parcel::NULL_PARCELABLE_FLAG {
                    Err($crate::StatusCode::UNEXPECTED_NULL)
                } else {
                    use $crate::parcel::Parcelable;
@@ -822,7 +834,7 @@ macro_rules! impl_deserialize_for_parcelable {
                parcel: &$crate::parcel::Parcel,
            ) -> $crate::Result<()> {
                let status: i32 = parcel.read()?;
                if status == 0 {
                if status == $crate::parcel::NULL_PARCELABLE_FLAG {
                    *this = None;
                    Ok(())
                } else {