From 083e353657bc9b2cd0f074f8eebc080a65e09fd7 Mon Sep 17 00:00:00 2001 From: Andrei Homescu Date: Wed, 8 Sep 2021 00:36:18 +0000 Subject: 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 --- libs/binder/rust/src/parcel.rs | 2 +- libs/binder/rust/src/parcel/parcelable.rs | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) (limited to 'libs') diff --git a/libs/binder/rust/src/parcel.rs b/libs/binder/rust/src/parcel.rs index 4e655b3dfc..9ab28ea0dd 100644 --- a/libs/binder/rust/src/parcel.rs +++ b/libs/binder/rust/src/parcel.rs @@ -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 diff --git a/libs/binder/rust/src/parcel/parcelable.rs b/libs/binder/rust/src/parcel/parcelable.rs index 172ae3fcb0..499ef09e6a 100644 --- a/libs/binder/rust/src/parcel/parcelable.rs +++ b/libs/binder/rust/src/parcel/parcelable.rs @@ -185,6 +185,18 @@ unsafe extern "C" fn deserialize_element( 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` // 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> { 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 { -- cgit v1.2.3-59-g8ed1b