Skip to content

Commit de68aaa

Browse files
committed
Add method to get bundle value from bundle.
Bug: 389074518 Test: atest libbinder_rs-internal_test Change-Id: I4dc1263b1dc903f7f8b631ffbe144ced8076908d
1 parent d62d565 commit de68aaa

1 file changed

Lines changed: 33 additions & 6 deletions

File tree

libs/binder/rust/src/persistable_bundle.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ use crate::{
2323
use binder_ndk_sys::{
2424
APersistableBundle, APersistableBundle_delete, APersistableBundle_dup,
2525
APersistableBundle_erase, APersistableBundle_getBoolean, APersistableBundle_getDouble,
26-
APersistableBundle_getInt, APersistableBundle_getLong, APersistableBundle_isEqual,
27-
APersistableBundle_new, APersistableBundle_putBoolean, APersistableBundle_putBooleanVector,
28-
APersistableBundle_putDouble, APersistableBundle_putDoubleVector, APersistableBundle_putInt,
29-
APersistableBundle_putIntVector, APersistableBundle_putLong, APersistableBundle_putLongVector,
26+
APersistableBundle_getInt, APersistableBundle_getLong, APersistableBundle_getPersistableBundle,
27+
APersistableBundle_isEqual, APersistableBundle_new, APersistableBundle_putBoolean,
28+
APersistableBundle_putBooleanVector, APersistableBundle_putDouble,
29+
APersistableBundle_putDoubleVector, APersistableBundle_putInt, APersistableBundle_putIntVector,
30+
APersistableBundle_putLong, APersistableBundle_putLongVector,
3031
APersistableBundle_putPersistableBundle, APersistableBundle_putString,
3132
APersistableBundle_putStringVector, APersistableBundle_readFromParcel, APersistableBundle_size,
3233
APersistableBundle_writeToParcel,
@@ -371,6 +372,28 @@ impl PersistableBundle {
371372
Ok(None)
372373
}
373374
}
375+
376+
/// Gets the `PersistableBundle` value associated with the given key.
377+
///
378+
/// Returns an error if the key contains a NUL character, or `Ok(None)` if the key doesn't exist
379+
/// in the bundle.
380+
pub fn get_persistable_bundle(&self, key: &str) -> Result<Option<Self>, NulError> {
381+
let key = CString::new(key)?;
382+
let mut value = null_mut();
383+
// SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
384+
// lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
385+
// to be valid for the lifetime of `key`. The value pointer must be valid because it comes
386+
// from a reference.
387+
if unsafe {
388+
APersistableBundle_getPersistableBundle(self.0.as_ptr(), key.as_ptr(), &mut value)
389+
} {
390+
Ok(Some(Self(NonNull::new(value).expect(
391+
"APersistableBundle_getPersistableBundle returned true but didn't set outBundle",
392+
))))
393+
} else {
394+
Ok(None)
395+
}
396+
}
374397
}
375398

376399
// SAFETY: The underlying *APersistableBundle can be moved between threads.
@@ -547,10 +570,14 @@ mod test {
547570
}
548571

549572
#[test]
550-
fn insert_bundle() {
573+
fn insert_get_bundle() {
551574
let mut bundle = PersistableBundle::new();
552575

553-
let sub_bundle = PersistableBundle::new();
576+
let mut sub_bundle = PersistableBundle::new();
577+
assert_eq!(sub_bundle.insert_int("int", 42), Ok(()));
578+
assert_eq!(sub_bundle.size(), 1);
554579
assert_eq!(bundle.insert_persistable_bundle("bundle", &sub_bundle), Ok(()));
580+
581+
assert_eq!(bundle.get_persistable_bundle("bundle"), Ok(Some(sub_bundle)));
555582
}
556583
}

0 commit comments

Comments
 (0)