summaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
author Andrei Homescu <ahomescu@google.com> 2021-09-08 00:36:18 +0000
committer Andrei Homescu <ahomescu@google.com> 2021-10-07 19:42:17 +0000
commit083e353657bc9b2cd0f074f8eebc080a65e09fd7 (patch)
tree4f4c5041928f0a53a8d3a31fcaf42dec19f6568c /libs
parent72b799d4fee0bfbb4ca6c7034cee6ef8b55a6231 (diff)
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
Diffstat (limited to 'libs')
-rw-r--r--libs/binder/rust/src/parcel.rs2
-rw-r--r--libs/binder/rust/src/parcel/parcelable.rs26
2 files changed, 20 insertions, 8 deletions
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<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 {