aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Walbran <qwandor@google.com>2024-01-11 16:30:09 +0000
committerTreehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com>2024-01-17 00:00:52 +0000
commita0aec5d84bd6364b647d3a624f9ae74c2a994081 (patch)
tree2e59edb4ed047490bc41c976d4f0df442687594e
parent65634abf082aafaaef8133003f2161a64fc78b0f (diff)
downloadaidl-a0aec5d84bd6364b647d3a624f9ae74c2a994081.tar.gz
Implement UnstructuredParcelable and use macros.
This saves a bunch of code duplication. Bug: 303064346 Test: atest aidl_integration_test Change-Id: Iedf51331a7b3887481fdcdaf7cdd926ee7b130ef
-rw-r--r--tests/rust/simple_parcelable.rs70
1 files changed, 12 insertions, 58 deletions
diff --git a/tests/rust/simple_parcelable.rs b/tests/rust/simple_parcelable.rs
index ff9eda2c..1b4a5ed7 100644
--- a/tests/rust/simple_parcelable.rs
+++ b/tests/rust/simple_parcelable.rs
@@ -17,10 +17,8 @@
//! Rust implementation of `SimpleParcelable`.
use binder::{
- binder_impl::{
- BorrowedParcel, Deserialize, DeserializeArray, DeserializeOption, Serialize,
- SerializeArray, SerializeOption, NON_NULL_PARCELABLE_FLAG, NULL_PARCELABLE_FLAG,
- },
+ binder_impl::{BorrowedParcel, UnstructuredParcelable},
+ impl_deserialize_for_unstructured_parcelable, impl_serialize_for_unstructured_parcelable,
StatusCode,
};
@@ -33,63 +31,19 @@ pub struct SimpleParcelable {
pub number: i32,
}
-impl Serialize for SimpleParcelable {
- fn serialize(&self, parcel: &mut BorrowedParcel) -> Result<(), StatusCode> {
- // Parcelables are always treated as optional, because of Java.
- SerializeOption::serialize_option(Some(self), parcel)
- }
-}
-
-impl SerializeOption for SimpleParcelable {
- fn serialize_option(
- this: Option<&Self>,
- parcel: &mut BorrowedParcel,
- ) -> Result<(), StatusCode> {
- if let Some(this) = this {
- parcel.write(&NON_NULL_PARCELABLE_FLAG)?;
- parcel.write(&this.name)?;
- parcel.write(&this.number)?;
- } else {
- parcel.write(&NULL_PARCELABLE_FLAG)?;
- }
+impl UnstructuredParcelable for SimpleParcelable {
+ fn write_to_parcel(&self, parcel: &mut BorrowedParcel) -> Result<(), StatusCode> {
+ parcel.write(&self.name)?;
+ parcel.write(&self.number)?;
Ok(())
}
-}
-
-impl Deserialize for SimpleParcelable {
- type UninitType = Option<Self>;
-
- fn uninit() -> Option<Self> {
- None
- }
- fn from_init(value: Self) -> Option<Self> {
- Some(value)
- }
-
- fn deserialize(parcel: &BorrowedParcel) -> Result<Self, StatusCode> {
- // Parcelables are always treated as optional, because of Java.
- DeserializeOption::deserialize_option(parcel)
- .transpose()
- .unwrap_or(Err(StatusCode::UNEXPECTED_NULL))
- }
-}
-
-impl DeserializeOption for SimpleParcelable {
- fn deserialize_option(parcel: &BorrowedParcel) -> Result<Option<Self>, StatusCode> {
- let present: i32 = parcel.read()?;
- match present {
- NULL_PARCELABLE_FLAG => Ok(None),
- NON_NULL_PARCELABLE_FLAG => {
- let name = parcel.read()?;
- let number = parcel.read()?;
- Ok(Some(Self { name, number }))
- }
- _ => Err(StatusCode::BAD_VALUE),
- }
+ fn from_parcel(parcel: &BorrowedParcel) -> Result<Self, StatusCode> {
+ let name = parcel.read()?;
+ let number = parcel.read()?;
+ Ok(Self { name, number })
}
}
-impl SerializeArray for SimpleParcelable {}
-
-impl DeserializeArray for SimpleParcelable {}
+impl_deserialize_for_unstructured_parcelable!(SimpleParcelable);
+impl_serialize_for_unstructured_parcelable!(SimpleParcelable);