1515 */
1616
1717use crate :: binder:: Stability ;
18+ use crate :: binder:: StabilityType ;
1819use crate :: error:: StatusCode ;
1920use crate :: parcel:: {
2021 BorrowedParcel , Deserialize , Parcel , Parcelable , Serialize , NON_NULL_PARCELABLE_FLAG ,
@@ -60,21 +61,25 @@ enum ParcelableHolderData {
6061/// `Send` nor `Sync`), mainly because it internally contains
6162/// a `Parcel` which in turn is not thread-safe.
6263#[ derive( Debug ) ]
63- pub struct ParcelableHolder {
64+ pub struct ParcelableHolder < STABILITY : StabilityType > {
6465 // This is a `Mutex` because of `get_parcelable`
6566 // which takes `&self` for consistency with C++.
6667 // We could make `get_parcelable` take a `&mut self`
6768 // and get rid of the `Mutex` here for a performance
6869 // improvement, but then callers would require a mutable
6970 // `ParcelableHolder` even for that getter method.
7071 data : Mutex < ParcelableHolderData > ,
71- stability : Stability ,
72+
73+ _stability_phantom : std:: marker:: PhantomData < STABILITY > ,
7274}
7375
74- impl ParcelableHolder {
76+ impl < STABILITY : StabilityType > ParcelableHolder < STABILITY > {
7577 /// Construct a new `ParcelableHolder` with the given stability.
76- pub fn new ( stability : Stability ) -> Self {
77- Self { data : Mutex :: new ( ParcelableHolderData :: Empty ) , stability }
78+ pub fn new ( ) -> Self {
79+ Self {
80+ data : Mutex :: new ( ParcelableHolderData :: Empty ) ,
81+ _stability_phantom : Default :: default ( ) ,
82+ }
7883 }
7984
8085 /// Reset the contents of this `ParcelableHolder`.
@@ -91,7 +96,7 @@ impl ParcelableHolder {
9196 where
9297 T : Any + Parcelable + ParcelableMetadata + std:: fmt:: Debug + Send + Sync ,
9398 {
94- if self . stability > p. get_stability ( ) {
99+ if STABILITY :: VALUE > p. get_stability ( ) {
95100 return Err ( StatusCode :: BAD_VALUE ) ;
96101 }
97102
@@ -157,30 +162,36 @@ impl ParcelableHolder {
157162
158163 /// Return the stability value of this object.
159164 pub fn get_stability ( & self ) -> Stability {
160- self . stability
165+ STABILITY :: VALUE
166+ }
167+ }
168+
169+ impl < STABILITY : StabilityType > Default for ParcelableHolder < STABILITY > {
170+ fn default ( ) -> Self {
171+ Self :: new ( )
161172 }
162173}
163174
164- impl Clone for ParcelableHolder {
165- fn clone ( & self ) -> ParcelableHolder {
175+ impl < STABILITY : StabilityType > Clone for ParcelableHolder < STABILITY > {
176+ fn clone ( & self ) -> Self {
166177 ParcelableHolder {
167178 data : Mutex :: new ( self . data . lock ( ) . unwrap ( ) . clone ( ) ) ,
168- stability : self . stability ,
179+ _stability_phantom : Default :: default ( ) ,
169180 }
170181 }
171182}
172183
173- impl Serialize for ParcelableHolder {
184+ impl < STABILITY : StabilityType > Serialize for ParcelableHolder < STABILITY > {
174185 fn serialize ( & self , parcel : & mut BorrowedParcel < ' _ > ) -> Result < ( ) , StatusCode > {
175186 parcel. write ( & NON_NULL_PARCELABLE_FLAG ) ?;
176187 self . write_to_parcel ( parcel)
177188 }
178189}
179190
180- impl Deserialize for ParcelableHolder {
191+ impl < STABILITY : StabilityType > Deserialize for ParcelableHolder < STABILITY > {
181192 type UninitType = Self ;
182193 fn uninit ( ) -> Self :: UninitType {
183- Self :: new ( Default :: default ( ) )
194+ Self :: new ( )
184195 }
185196 fn from_init ( value : Self ) -> Self :: UninitType {
186197 value
@@ -191,16 +202,16 @@ impl Deserialize for ParcelableHolder {
191202 if status == NULL_PARCELABLE_FLAG {
192203 Err ( StatusCode :: UNEXPECTED_NULL )
193204 } else {
194- let mut parcelable = ParcelableHolder :: new ( Default :: default ( ) ) ;
205+ let mut parcelable = Self :: new ( ) ;
195206 parcelable. read_from_parcel ( parcel) ?;
196207 Ok ( parcelable)
197208 }
198209 }
199210}
200211
201- impl Parcelable for ParcelableHolder {
212+ impl < STABILITY : StabilityType > Parcelable for ParcelableHolder < STABILITY > {
202213 fn write_to_parcel ( & self , parcel : & mut BorrowedParcel < ' _ > ) -> Result < ( ) , StatusCode > {
203- parcel. write ( & self . stability ) ?;
214+ parcel. write ( & STABILITY :: VALUE ) ?;
204215
205216 let mut data = self . data . lock ( ) . unwrap ( ) ;
206217 match * data {
@@ -236,7 +247,7 @@ impl Parcelable for ParcelableHolder {
236247 }
237248
238249 fn read_from_parcel ( & mut self , parcel : & BorrowedParcel < ' _ > ) -> Result < ( ) , StatusCode > {
239- if self . stability != parcel. read ( ) ? {
250+ if self . get_stability ( ) != parcel. read ( ) ? {
240251 return Err ( StatusCode :: BAD_VALUE ) ;
241252 }
242253
0 commit comments