android-binder 0.2.0

Safe bindings to Android Binder, restricted to the NDK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use crate::{
    binder::AsNative,
    error::{StatusCode, status_result},
    impl_deserialize_for_unstructured_parcelable, impl_serialize_for_unstructured_parcelable,
    parcel::{BorrowedParcel, UnstructuredParcelable},
};
use binder_ndk_sys::{
    APERSISTABLEBUNDLE_ALLOCATOR_FAILED, APERSISTABLEBUNDLE_KEY_NOT_FOUND, APersistableBundle,
    APersistableBundle_delete, APersistableBundle_dup, APersistableBundle_erase,
    APersistableBundle_getBoolean, APersistableBundle_getBooleanKeys,
    APersistableBundle_getBooleanVector, APersistableBundle_getBooleanVectorKeys,
    APersistableBundle_getDouble, APersistableBundle_getDoubleKeys,
    APersistableBundle_getDoubleVector, APersistableBundle_getDoubleVectorKeys,
    APersistableBundle_getInt, APersistableBundle_getIntKeys, APersistableBundle_getIntVector,
    APersistableBundle_getIntVectorKeys, APersistableBundle_getLong,
    APersistableBundle_getLongKeys, APersistableBundle_getLongVector,
    APersistableBundle_getLongVectorKeys, APersistableBundle_getPersistableBundle,
    APersistableBundle_getPersistableBundleKeys, APersistableBundle_getString,
    APersistableBundle_getStringKeys, APersistableBundle_getStringVector,
    APersistableBundle_getStringVectorKeys, APersistableBundle_isEqual, APersistableBundle_new,
    APersistableBundle_putBoolean, APersistableBundle_putBooleanVector,
    APersistableBundle_putDouble, APersistableBundle_putDoubleVector, APersistableBundle_putInt,
    APersistableBundle_putIntVector, APersistableBundle_putLong, APersistableBundle_putLongVector,
    APersistableBundle_putPersistableBundle, APersistableBundle_putString,
    APersistableBundle_putStringVector, APersistableBundle_readFromParcel, APersistableBundle_size,
    APersistableBundle_writeToParcel,
};

use alloc::boxed::Box;
use alloc::ffi::{CString, NulError};
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::ffi::{CStr, c_char, c_void};
use core::mem::size_of;
use core::ptr::{NonNull, null_mut, slice_from_raw_parts_mut};
use zerocopy::FromZeros;

/// A mapping from string keys to values of various types.
#[derive(Debug)]
pub struct PersistableBundle(NonNull<APersistableBundle>);

impl PersistableBundle {
    /// Creates a new `PersistableBundle`.
    pub fn new() -> Self {
        // SAFETY: APersistableBundle_new doesn't actually have any safety requirements.
        let bundle = unsafe { APersistableBundle_new() };
        Self(NonNull::new(bundle).expect("Allocated APersistableBundle was null"))
    }

    /// Returns the number of mappings in the bundle.
    pub fn size(&self) -> usize {
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`.
        unsafe { APersistableBundle_size(self.0.as_ptr()) }
            .try_into()
            .expect("APersistableBundle_size returned a negative size")
    }

    /// Removes any entry with the given key.
    ///
    /// Returns an error if the given key contains a NUL character, otherwise returns whether there
    /// was any entry to remove.
    pub fn remove(&mut self, key: &str) -> Result<bool, NulError> {
        let key = CString::new(key)?;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call.
        Ok(unsafe { APersistableBundle_erase(self.0.as_ptr(), key.as_ptr()) != 0 })
    }

    /// Inserts a key-value pair into the bundle.
    ///
    /// If the key is already present then its value will be overwritten by the given value.
    ///
    /// Returns an error if the key contains a NUL character.
    pub fn insert_bool(&mut self, key: &str, value: bool) -> Result<(), NulError> {
        let key = CString::new(key)?;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call.
        unsafe {
            APersistableBundle_putBoolean(self.0.as_ptr(), key.as_ptr(), value);
        }
        Ok(())
    }

    /// Inserts a key-value pair into the bundle.
    ///
    /// If the key is already present then its value will be overwritten by the given value.
    ///
    /// Returns an error if the key contains a NUL character.
    pub fn insert_int(&mut self, key: &str, value: i32) -> Result<(), NulError> {
        let key = CString::new(key)?;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call.
        unsafe {
            APersistableBundle_putInt(self.0.as_ptr(), key.as_ptr(), value);
        }
        Ok(())
    }

    /// Inserts a key-value pair into the bundle.
    ///
    /// If the key is already present then its value will be overwritten by the given value.
    ///
    /// Returns an error if the key contains a NUL character.
    pub fn insert_long(&mut self, key: &str, value: i64) -> Result<(), NulError> {
        let key = CString::new(key)?;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call.
        unsafe {
            APersistableBundle_putLong(self.0.as_ptr(), key.as_ptr(), value);
        }
        Ok(())
    }

    /// Inserts a key-value pair into the bundle.
    ///
    /// If the key is already present then its value will be overwritten by the given value.
    ///
    /// Returns an error if the key contains a NUL character.
    pub fn insert_double(&mut self, key: &str, value: f64) -> Result<(), NulError> {
        let key = CString::new(key)?;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call.
        unsafe {
            APersistableBundle_putDouble(self.0.as_ptr(), key.as_ptr(), value);
        }
        Ok(())
    }

    /// Inserts a key-value pair into the bundle.
    ///
    /// If the key is already present then its value will be overwritten by the given value.
    ///
    /// Returns an error if the key or value contains a NUL character.
    pub fn insert_string(&mut self, key: &str, value: &str) -> Result<(), NulError> {
        let key = CString::new(key)?;
        let value = CString::new(value)?;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `CStr::as_ptr` is guaranteed
        // to be valid for the duration of this call.
        unsafe {
            APersistableBundle_putString(self.0.as_ptr(), key.as_ptr(), value.as_ptr());
        }
        Ok(())
    }

    /// Inserts a key-value pair into the bundle.
    ///
    /// If the key is already present then its value will be overwritten by the given value.
    ///
    /// Returns an error if the key contains a NUL character.
    pub fn insert_bool_vec(&mut self, key: &str, value: &[bool]) -> Result<(), NulError> {
        let key = CString::new(key)?;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call, and likewise the pointer returned by
        // `value.as_ptr()` is guaranteed to be valid for at least `value.len()` values for the
        // duration of the call.
        unsafe {
            APersistableBundle_putBooleanVector(
                self.0.as_ptr(),
                key.as_ptr(),
                value.as_ptr(),
                value.len().try_into().unwrap(),
            );
        }
        Ok(())
    }

    /// Inserts a key-value pair into the bundle.
    ///
    /// If the key is already present then its value will be overwritten by the given value.
    ///
    /// Returns an error if the key contains a NUL character.
    pub fn insert_int_vec(&mut self, key: &str, value: &[i32]) -> Result<(), NulError> {
        let key = CString::new(key)?;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call, and likewise the pointer returned by
        // `value.as_ptr()` is guaranteed to be valid for at least `value.len()` values for the
        // duration of the call.
        unsafe {
            APersistableBundle_putIntVector(
                self.0.as_ptr(),
                key.as_ptr(),
                value.as_ptr(),
                value.len().try_into().unwrap(),
            );
        }
        Ok(())
    }

    /// Inserts a key-value pair into the bundle.
    ///
    /// If the key is already present then its value will be overwritten by the given value.
    ///
    /// Returns an error if the key contains a NUL character.
    pub fn insert_long_vec(&mut self, key: &str, value: &[i64]) -> Result<(), NulError> {
        let key = CString::new(key)?;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call, and likewise the pointer returned by
        // `value.as_ptr()` is guaranteed to be valid for at least `value.len()` values for the
        // duration of the call.
        unsafe {
            APersistableBundle_putLongVector(
                self.0.as_ptr(),
                key.as_ptr(),
                value.as_ptr(),
                value.len().try_into().unwrap(),
            );
        }
        Ok(())
    }

    /// Inserts a key-value pair into the bundle.
    ///
    /// If the key is already present then its value will be overwritten by the given value.
    ///
    /// Returns an error if the key contains a NUL character.
    pub fn insert_double_vec(&mut self, key: &str, value: &[f64]) -> Result<(), NulError> {
        let key = CString::new(key)?;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call, and likewise the pointer returned by
        // `value.as_ptr()` is guaranteed to be valid for at least `value.len()` values for the
        // duration of the call.
        unsafe {
            APersistableBundle_putDoubleVector(
                self.0.as_ptr(),
                key.as_ptr(),
                value.as_ptr(),
                value.len().try_into().unwrap(),
            );
        }
        Ok(())
    }

    /// Inserts a key-value pair into the bundle.
    ///
    /// If the key is already present then its value will be overwritten by the given value.
    ///
    /// Returns an error if the key contains a NUL character.
    pub fn insert_string_vec<'a, T: ToString + 'a>(
        &mut self,
        key: &str,
        value: impl IntoIterator<Item = &'a T>,
    ) -> Result<(), NulError> {
        let key = CString::new(key)?;
        // We need to collect the new `CString`s into something first so that they live long enough
        // for their pointers to be valid for the `APersistableBundle_putStringVector` call below.
        let c_strings = value
            .into_iter()
            .map(|s| CString::new(s.to_string()))
            .collect::<Result<Vec<_>, NulError>>()?;
        let char_pointers = c_strings.iter().map(|s| s.as_ptr()).collect::<Vec<_>>();
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call, and likewise the pointer returned by
        // `value.as_ptr()` is guaranteed to be valid for at least `value.len()` values for the
        // duration of the call.
        unsafe {
            APersistableBundle_putStringVector(
                self.0.as_ptr(),
                key.as_ptr(),
                char_pointers.as_ptr(),
                char_pointers.len().try_into().unwrap(),
            );
        }
        Ok(())
    }

    /// Inserts a key-value pair into the bundle.
    ///
    /// If the key is already present then its value will be overwritten by the given value.
    ///
    /// Returns an error if the key contains a NUL character.
    pub fn insert_persistable_bundle(
        &mut self,
        key: &str,
        value: &PersistableBundle,
    ) -> Result<(), NulError> {
        let key = CString::new(key)?;
        // SAFETY: The wrapped `APersistableBundle` pointers are guaranteed to be valid for the
        // lifetime of the `PersistableBundle`s. The pointer returned by `CStr::as_ptr` is
        // guaranteed to be valid for the duration of this call, and
        // `APersistableBundle_putPersistableBundle` does a deep copy so that is all that is
        // required.
        unsafe {
            APersistableBundle_putPersistableBundle(
                self.0.as_ptr(),
                key.as_ptr(),
                value.0.as_ptr(),
            );
        }
        Ok(())
    }

    /// Gets the boolean value associated with the given key.
    ///
    /// Returns an error if the key contains a NUL character, or `Ok(None)` if the key doesn't exist
    /// in the bundle.
    pub fn get_bool(&self, key: &str) -> Result<Option<bool>, NulError> {
        let key = CString::new(key)?;
        let mut value = false;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call. The value pointer must be valid because it
        // comes from a reference.
        if unsafe { APersistableBundle_getBoolean(self.0.as_ptr(), key.as_ptr(), &mut value) } {
            Ok(Some(value))
        } else {
            Ok(None)
        }
    }

    /// Gets the i32 value associated with the given key.
    ///
    /// Returns an error if the key contains a NUL character, or `Ok(None)` if the key doesn't exist
    /// in the bundle.
    pub fn get_int(&self, key: &str) -> Result<Option<i32>, NulError> {
        let key = CString::new(key)?;
        let mut value = 0;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call. The value pointer must be valid because it
        // comes from a reference.
        if unsafe { APersistableBundle_getInt(self.0.as_ptr(), key.as_ptr(), &mut value) } {
            Ok(Some(value))
        } else {
            Ok(None)
        }
    }

    /// Gets the i64 value associated with the given key.
    ///
    /// Returns an error if the key contains a NUL character, or `Ok(None)` if the key doesn't exist
    /// in the bundle.
    pub fn get_long(&self, key: &str) -> Result<Option<i64>, NulError> {
        let key = CString::new(key)?;
        let mut value = 0;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call. The value pointer must be valid because it
        // comes from a reference.
        if unsafe { APersistableBundle_getLong(self.0.as_ptr(), key.as_ptr(), &mut value) } {
            Ok(Some(value))
        } else {
            Ok(None)
        }
    }

    /// Gets the f64 value associated with the given key.
    ///
    /// Returns an error if the key contains a NUL character, or `Ok(None)` if the key doesn't exist
    /// in the bundle.
    pub fn get_double(&self, key: &str) -> Result<Option<f64>, NulError> {
        let key = CString::new(key)?;
        let mut value = 0.0;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the duration of this call. The value pointer must be valid because it
        // comes from a reference.
        if unsafe { APersistableBundle_getDouble(self.0.as_ptr(), key.as_ptr(), &mut value) } {
            Ok(Some(value))
        } else {
            Ok(None)
        }
    }

    /// Gets the string value associated with the given key.
    ///
    /// Returns an error if the key contains a NUL character, or `Ok(None)` if the key doesn't exist
    /// in the bundle.
    pub fn get_string(&self, key: &str) -> Result<Option<String>, NulError> {
        let key = CString::new(key)?;
        let mut value = null_mut();
        let mut allocated_size: usize = 0;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the lifetime of `key`. The value pointer must be valid because it comes
        // from a reference.
        let value_size_bytes = unsafe {
            APersistableBundle_getString(
                self.0.as_ptr(),
                key.as_ptr(),
                &mut value,
                Some(string_allocator),
                (&raw mut allocated_size).cast(),
            )
        };
        match value_size_bytes {
            APERSISTABLEBUNDLE_KEY_NOT_FOUND => Ok(None),
            APERSISTABLEBUNDLE_ALLOCATOR_FAILED => {
                panic!("APersistableBundle_getString failed to allocate string");
            }
            _ => {
                let raw_slice = slice_from_raw_parts_mut(value.cast(), allocated_size);
                // SAFETY: The pointer was returned from string_allocator, which used
                // `Box::into_raw`, and we've got the appropriate size back from allocated_size.
                let boxed_slice: Box<[u8]> = unsafe { Box::from_raw(raw_slice) };
                assert_eq!(
                    allocated_size,
                    usize::try_from(value_size_bytes)
                        .expect("APersistableBundle_getString returned negative value size")
                        + 1
                );
                let c_string = CString::from_vec_with_nul(boxed_slice.into())
                    .expect("APersistableBundle_getString returned string missing NUL byte");
                let string = c_string
                    .into_string()
                    .expect("APersistableBundle_getString returned invalid UTF-8");
                Ok(Some(string))
            }
        }
    }

    /// Gets the vector of `T` associated with the given key.
    ///
    /// Returns an error if the key contains a NUL character, or `Ok(None)` if the key doesn't exist
    /// in the bundle.
    ///
    /// `get_func` should be one of the `APersistableBundle_get*Vector` functions from
    /// `binder_ndk_sys`.
    ///
    /// # Safety
    ///
    /// `get_func` must only require that the pointers it takes are valid for the duration of the
    /// call. It must allow a null pointer for the buffer, and must return the size in bytes of
    /// buffer it requires. If it is given a non-null buffer pointer it must write that number of
    /// bytes to the buffer, which must be a whole number of valid `T` values.
    unsafe fn get_vec<T: Clone>(
        &self,
        key: &str,
        default: T,
        get_func: unsafe extern "C" fn(
            *const APersistableBundle,
            *const c_char,
            *mut T,
            i32,
        ) -> i32,
    ) -> Result<Option<Vec<T>>, NulError> {
        let key = CString::new(key)?;
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the lifetime of `key`. A null pointer is allowed for the buffer.
        match unsafe { get_func(self.0.as_ptr(), key.as_ptr(), null_mut(), 0) } {
            APERSISTABLEBUNDLE_KEY_NOT_FOUND => Ok(None),
            APERSISTABLEBUNDLE_ALLOCATOR_FAILED => {
                panic!("APersistableBundle_getStringVector failed to allocate string");
            }
            required_buffer_size => {
                let mut value = vec![
                    default;
                    usize::try_from(required_buffer_size).expect(
                        "APersistableBundle_get*Vector returned invalid size"
                    ) / size_of::<T>()
                ];
                // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for
                // the lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()`
                // is guaranteed to be valid for the lifetime of `key`. The value buffer pointer is
                // valid as it comes from the Vec we just allocated.
                match unsafe {
                    get_func(
                        self.0.as_ptr(),
                        key.as_ptr(),
                        value.as_mut_ptr(),
                        (value.len() * size_of::<T>()).try_into().unwrap(),
                    )
                } {
                    APERSISTABLEBUNDLE_KEY_NOT_FOUND => {
                        panic!(
                            "APersistableBundle_get*Vector failed to find key after first finding it"
                        );
                    }
                    APERSISTABLEBUNDLE_ALLOCATOR_FAILED => {
                        panic!("APersistableBundle_getStringVector failed to allocate string");
                    }
                    _ => Ok(Some(value)),
                }
            }
        }
    }

    /// Gets the boolean vector value associated with the given key.
    ///
    /// Returns an error if the key contains a NUL character, or `Ok(None)` if the key doesn't exist
    /// in the bundle.
    pub fn get_bool_vec(&self, key: &str) -> Result<Option<Vec<bool>>, NulError> {
        // SAFETY: APersistableBundle_getBooleanVector fulfils all the safety requirements of
        // `get_vec`.
        unsafe { self.get_vec(key, Default::default(), APersistableBundle_getBooleanVector) }
    }

    /// Gets the i32 vector value associated with the given key.
    ///
    /// Returns an error if the key contains a NUL character, or `Ok(None)` if the key doesn't exist
    /// in the bundle.
    pub fn get_int_vec(&self, key: &str) -> Result<Option<Vec<i32>>, NulError> {
        // SAFETY: APersistableBundle_getIntVector fulfils all the safety requirements of
        // `get_vec`.
        unsafe { self.get_vec(key, Default::default(), APersistableBundle_getIntVector) }
    }

    /// Gets the i64 vector value associated with the given key.
    ///
    /// Returns an error if the key contains a NUL character, or `Ok(None)` if the key doesn't exist
    /// in the bundle.
    pub fn get_long_vec(&self, key: &str) -> Result<Option<Vec<i64>>, NulError> {
        // SAFETY: APersistableBundle_getLongVector fulfils all the safety requirements of
        // `get_vec`.
        unsafe { self.get_vec(key, Default::default(), APersistableBundle_getLongVector) }
    }

    /// Gets the f64 vector value associated with the given key.
    ///
    /// Returns an error if the key contains a NUL character, or `Ok(None)` if the key doesn't exist
    /// in the bundle.
    pub fn get_double_vec(&self, key: &str) -> Result<Option<Vec<f64>>, NulError> {
        // SAFETY: APersistableBundle_getDoubleVector fulfils all the safety requirements of
        // `get_vec`.
        unsafe { self.get_vec(key, Default::default(), APersistableBundle_getDoubleVector) }
    }

    /// Gets the string vector value associated with the given key.
    ///
    /// Returns an error if the key contains a NUL character, or `Ok(None)` if the key doesn't exist
    /// in the bundle.
    pub fn get_string_vec(&self, key: &str) -> Result<Option<Vec<String>>, NulError> {
        if let Some(value) =
            // SAFETY: `get_string_vector_with_allocator` fulfils all the safety requirements of
            // `get_vec`.
            unsafe { self.get_vec(key, null_mut(), get_string_vector_with_allocator) }?
        {
            Ok(Some(
                value
                    .into_iter()
                    .map(|s| {
                        // SAFETY: The pointer was returned from `string_allocator`, which used
                        // `Box::into_raw`, and `APersistableBundle_getStringVector` should have
                        // written valid bytes to it including a NUL terminator in the last
                        // position.
                        let string_length = unsafe { CStr::from_ptr(s) }.count_bytes();
                        let raw_slice = slice_from_raw_parts_mut(s.cast(), string_length + 1);
                        // SAFETY: The pointer was returned from `string_allocator`, which used
                        // `Box::into_raw`, and we've got the appropriate size back by checking the
                        // length of the string.
                        let boxed_slice: Box<[u8]> = unsafe { Box::from_raw(raw_slice) };
                        let c_string = CString::from_vec_with_nul(boxed_slice.into()).expect(
                            "APersistableBundle_getStringVector returned string missing NUL byte",
                        );
                        c_string
                            .into_string()
                            .expect("APersistableBundle_getStringVector returned invalid UTF-8")
                    })
                    .collect(),
            ))
        } else {
            Ok(None)
        }
    }

    /// Gets the `PersistableBundle` value associated with the given key.
    ///
    /// Returns an error if the key contains a NUL character, or `Ok(None)` if the key doesn't exist
    /// in the bundle.
    pub fn get_persistable_bundle(&self, key: &str) -> Result<Option<Self>, NulError> {
        let key = CString::new(key)?;
        let mut value = null_mut();
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. The pointer returned by `key.as_ptr()` is guaranteed
        // to be valid for the lifetime of `key`. The value pointer must be valid because it comes
        // from a reference.
        if unsafe {
            APersistableBundle_getPersistableBundle(self.0.as_ptr(), key.as_ptr(), &mut value)
        } {
            Ok(Some(Self(NonNull::new(value).expect(
                "APersistableBundle_getPersistableBundle returned true but didn't set outBundle",
            ))))
        } else {
            Ok(None)
        }
    }

    /// Calls the appropriate `APersistableBundle_get*Keys` function for the given `value_type`,
    /// with our `string_allocator` and a null context pointer.
    ///
    /// # Safety
    ///
    /// `out_keys` must either be null or point to a buffer of at least `buffer_size_bytes` bytes,
    /// properly aligned for `T`, and not otherwise accessed for the duration of the call.
    unsafe fn get_keys_raw(
        &self,
        value_type: ValueType,
        out_keys: *mut *mut c_char,
        buffer_size_bytes: i32,
    ) -> i32 {
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. Our caller guarantees an appropriate value for
        // `out_keys` and `buffer_size_bytes`.
        unsafe {
            match value_type {
                ValueType::Boolean => APersistableBundle_getBooleanKeys(
                    self.0.as_ptr(),
                    out_keys,
                    buffer_size_bytes,
                    Some(string_allocator),
                    null_mut(),
                ),
                ValueType::Integer => APersistableBundle_getIntKeys(
                    self.0.as_ptr(),
                    out_keys,
                    buffer_size_bytes,
                    Some(string_allocator),
                    null_mut(),
                ),
                ValueType::Long => APersistableBundle_getLongKeys(
                    self.0.as_ptr(),
                    out_keys,
                    buffer_size_bytes,
                    Some(string_allocator),
                    null_mut(),
                ),
                ValueType::Double => APersistableBundle_getDoubleKeys(
                    self.0.as_ptr(),
                    out_keys,
                    buffer_size_bytes,
                    Some(string_allocator),
                    null_mut(),
                ),
                ValueType::String => APersistableBundle_getStringKeys(
                    self.0.as_ptr(),
                    out_keys,
                    buffer_size_bytes,
                    Some(string_allocator),
                    null_mut(),
                ),
                ValueType::BooleanVector => APersistableBundle_getBooleanVectorKeys(
                    self.0.as_ptr(),
                    out_keys,
                    buffer_size_bytes,
                    Some(string_allocator),
                    null_mut(),
                ),
                ValueType::IntegerVector => APersistableBundle_getIntVectorKeys(
                    self.0.as_ptr(),
                    out_keys,
                    buffer_size_bytes,
                    Some(string_allocator),
                    null_mut(),
                ),
                ValueType::LongVector => APersistableBundle_getLongVectorKeys(
                    self.0.as_ptr(),
                    out_keys,
                    buffer_size_bytes,
                    Some(string_allocator),
                    null_mut(),
                ),
                ValueType::DoubleVector => APersistableBundle_getDoubleVectorKeys(
                    self.0.as_ptr(),
                    out_keys,
                    buffer_size_bytes,
                    Some(string_allocator),
                    null_mut(),
                ),
                ValueType::StringVector => APersistableBundle_getStringVectorKeys(
                    self.0.as_ptr(),
                    out_keys,
                    buffer_size_bytes,
                    Some(string_allocator),
                    null_mut(),
                ),
                ValueType::PersistableBundle => APersistableBundle_getPersistableBundleKeys(
                    self.0.as_ptr(),
                    out_keys,
                    buffer_size_bytes,
                    Some(string_allocator),
                    null_mut(),
                ),
            }
        }
    }

    /// Gets all the keys associated with values of the given type.
    pub fn keys_for_type(&self, value_type: ValueType) -> Vec<String> {
        // SAFETY: A null pointer is allowed for the buffer.
        match unsafe { self.get_keys_raw(value_type, null_mut(), 0) } {
            APERSISTABLEBUNDLE_ALLOCATOR_FAILED => {
                panic!("APersistableBundle_get*Keys failed to allocate string");
            }
            required_buffer_size => {
                let required_buffer_size_usize = usize::try_from(required_buffer_size)
                    .expect("APersistableBundle_get*Keys returned invalid size");
                assert_eq!(required_buffer_size_usize % size_of::<*mut c_char>(), 0);
                let mut keys =
                    vec![null_mut(); required_buffer_size_usize / size_of::<*mut c_char>()];
                // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for
                // the lifetime of the `PersistableBundle`. The keys buffer pointer is valid as it
                // comes from the Vec we just allocated.
                if unsafe { self.get_keys_raw(value_type, keys.as_mut_ptr(), required_buffer_size) }
                    == APERSISTABLEBUNDLE_ALLOCATOR_FAILED
                {
                    panic!("APersistableBundle_get*Keys failed to allocate string");
                }
                keys.into_iter()
                    .map(|key| {
                        // SAFETY: The pointer was returned from `string_allocator`, which used
                        // `Box::into_raw`, and `APersistableBundle_getStringVector` should have
                        // written valid bytes to it including a NUL terminator in the last
                        // position.
                        let string_length = unsafe { CStr::from_ptr(key) }.count_bytes();
                        let raw_slice = slice_from_raw_parts_mut(key.cast(), string_length + 1);
                        // SAFETY: The pointer was returned from `string_allocator`, which used
                        // `Box::into_raw`, and we've got the appropriate size back by checking the
                        // length of the string.
                        let boxed_slice: Box<[u8]> = unsafe { Box::from_raw(raw_slice) };
                        let c_string = CString::from_vec_with_nul(boxed_slice.into())
                            .expect("APersistableBundle_get*Keys returned string missing NUL byte");
                        c_string
                            .into_string()
                            .expect("APersistableBundle_get*Keys returned invalid UTF-8")
                    })
                    .collect()
            }
        }
    }

    /// Returns an iterator over all keys in the bundle, along with the type of their associated
    /// value.
    pub fn keys(&self) -> impl Iterator<Item = (String, ValueType)> + use<'_> {
        [
            ValueType::Boolean,
            ValueType::Integer,
            ValueType::Long,
            ValueType::Double,
            ValueType::String,
            ValueType::BooleanVector,
            ValueType::IntegerVector,
            ValueType::LongVector,
            ValueType::DoubleVector,
            ValueType::StringVector,
            ValueType::PersistableBundle,
        ]
        .iter()
        .flat_map(|value_type| {
            self.keys_for_type(*value_type).into_iter().map(|key| (key, *value_type))
        })
    }
}

/// Wrapper around `APersistableBundle_getStringVector` to pass `string_allocator` and a null
/// context pointer.
///
/// # Safety
///
/// * `bundle` must point to a valid `APersistableBundle` which is not modified for the duration of
///   the call.
/// * `key` must point to a valid NUL-terminated C string.
/// * `buffer` must either be null or point to a buffer of at least `buffer_size_bytes` bytes,
///   properly aligned for `T`, and not otherwise accessed for the duration of the call.
unsafe extern "C" fn get_string_vector_with_allocator(
    bundle: *const APersistableBundle,
    key: *const c_char,
    buffer: *mut *mut c_char,
    buffer_size_bytes: i32,
) -> i32 {
    // SAFETY: The safety requirements are all guaranteed by our caller according to the safety
    // documentation above.
    unsafe {
        APersistableBundle_getStringVector(
            bundle,
            key,
            buffer,
            buffer_size_bytes,
            Some(string_allocator),
            null_mut(),
        )
    }
}

// SAFETY: The underlying *APersistableBundle can be moved between threads.
unsafe impl Send for PersistableBundle {}

// SAFETY: The underlying *APersistableBundle can be read from multiple threads, and we require
// `&mut PersistableBundle` for any operations which mutate it.
unsafe impl Sync for PersistableBundle {}

impl Default for PersistableBundle {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for PersistableBundle {
    fn drop(&mut self) {
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of this `PersistableBundle`.
        unsafe { APersistableBundle_delete(self.0.as_ptr()) };
    }
}

impl Clone for PersistableBundle {
    fn clone(&self) -> Self {
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`.
        let duplicate = unsafe { APersistableBundle_dup(self.0.as_ptr()) };
        Self(NonNull::new(duplicate).expect("Duplicated APersistableBundle was null"))
    }
}

impl PartialEq for PersistableBundle {
    fn eq(&self, other: &Self) -> bool {
        // SAFETY: The wrapped `APersistableBundle` pointers are guaranteed to be valid for the
        // lifetime of the `PersistableBundle`s.
        unsafe { APersistableBundle_isEqual(self.0.as_ptr(), other.0.as_ptr()) }
    }
}

impl UnstructuredParcelable for PersistableBundle {
    fn write_to_parcel(&self, parcel: &mut BorrowedParcel) -> Result<(), StatusCode> {
        let status =
        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. `parcel.as_native_mut()` always returns a valid
        // parcel pointer.
            unsafe { APersistableBundle_writeToParcel(self.0.as_ptr(), parcel.as_native_mut()) };
        status_result(status)
    }

    fn from_parcel(parcel: &BorrowedParcel) -> Result<Self, StatusCode> {
        let mut bundle = null_mut();

        // SAFETY: The wrapped `APersistableBundle` pointer is guaranteed to be valid for the
        // lifetime of the `PersistableBundle`. `parcel.as_native()` always returns a valid parcel
        // pointer.
        let status = unsafe { APersistableBundle_readFromParcel(parcel.as_native(), &mut bundle) };
        status_result(status)?;

        Ok(Self(NonNull::new(bundle).expect(
            "APersistableBundle_readFromParcel returned success but didn't allocate bundle",
        )))
    }
}

/// Allocates a boxed slice of the given size in bytes, returns a pointer to it and writes its size
/// to `*context`.
///
/// # Safety
///
/// `context` must either be null or point to a `usize` to which we can write.
unsafe extern "C" fn string_allocator(size: i32, context: *mut c_void) -> *mut c_char {
    let Ok(size) = size.try_into() else {
        return null_mut();
    };
    let Ok(boxed_slice) = <[c_char]>::new_box_zeroed_with_elems(size) else {
        return null_mut();
    };
    if !context.is_null() {
        // SAFETY: The caller promised that `context` is either null or points to a `usize` to which
        // we can write, and we just checked that it's not null.
        unsafe {
            *context.cast::<usize>() = size;
        }
    }
    Box::into_raw(boxed_slice).cast()
}

impl_deserialize_for_unstructured_parcelable!(PersistableBundle);
impl_serialize_for_unstructured_parcelable!(PersistableBundle);

/// The types which may be stored as values in a [`PersistableBundle`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ValueType {
    /// A `bool`.
    Boolean,
    /// An `i32`.
    Integer,
    /// An `i64`.
    Long,
    /// An `f64`.
    Double,
    /// A string.
    String,
    /// A vector of `bool`s.
    BooleanVector,
    /// A vector of `i32`s.
    IntegerVector,
    /// A vector of `i64`s.
    LongVector,
    /// A vector of `f64`s.
    DoubleVector,
    /// A vector of strings.
    StringVector,
    /// A nested `PersistableBundle`.
    PersistableBundle,
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn create_delete() {
        let bundle = PersistableBundle::new();
        drop(bundle);
    }

    #[test]
    fn duplicate_equal() {
        let bundle = PersistableBundle::new();
        let duplicate = bundle.clone();
        assert_eq!(bundle, duplicate);
    }

    #[test]
    fn get_empty() {
        let bundle = PersistableBundle::new();
        assert_eq!(bundle.get_bool("foo"), Ok(None));
        assert_eq!(bundle.get_int("foo"), Ok(None));
        assert_eq!(bundle.get_long("foo"), Ok(None));
        assert_eq!(bundle.get_double("foo"), Ok(None));
        assert_eq!(bundle.get_bool_vec("foo"), Ok(None));
        assert_eq!(bundle.get_int_vec("foo"), Ok(None));
        assert_eq!(bundle.get_long_vec("foo"), Ok(None));
        assert_eq!(bundle.get_double_vec("foo"), Ok(None));
        assert_eq!(bundle.get_string("foo"), Ok(None));
    }

    #[test]
    fn remove_empty() {
        let mut bundle = PersistableBundle::new();
        assert_eq!(bundle.remove("foo"), Ok(false));
    }

    #[test]
    fn insert_get_primitives() {
        let mut bundle = PersistableBundle::new();

        assert_eq!(bundle.insert_bool("bool", true), Ok(()));
        assert_eq!(bundle.insert_int("int", 42), Ok(()));
        assert_eq!(bundle.insert_long("long", 66), Ok(()));
        assert_eq!(bundle.insert_double("double", 123.4), Ok(()));

        assert_eq!(bundle.get_bool("bool"), Ok(Some(true)));
        assert_eq!(bundle.get_int("int"), Ok(Some(42)));
        assert_eq!(bundle.get_long("long"), Ok(Some(66)));
        assert_eq!(bundle.get_double("double"), Ok(Some(123.4)));
        assert_eq!(bundle.size(), 4);

        // Getting the wrong type should return nothing.
        assert_eq!(bundle.get_int("bool"), Ok(None));
        assert_eq!(bundle.get_long("bool"), Ok(None));
        assert_eq!(bundle.get_double("bool"), Ok(None));
        assert_eq!(bundle.get_bool("int"), Ok(None));
        assert_eq!(bundle.get_long("int"), Ok(None));
        assert_eq!(bundle.get_double("int"), Ok(None));
        assert_eq!(bundle.get_bool("long"), Ok(None));
        assert_eq!(bundle.get_int("long"), Ok(None));
        assert_eq!(bundle.get_double("long"), Ok(None));
        assert_eq!(bundle.get_bool("double"), Ok(None));
        assert_eq!(bundle.get_int("double"), Ok(None));
        assert_eq!(bundle.get_long("double"), Ok(None));

        // If they are removed they should no longer be present.
        assert_eq!(bundle.remove("bool"), Ok(true));
        assert_eq!(bundle.remove("int"), Ok(true));
        assert_eq!(bundle.remove("long"), Ok(true));
        assert_eq!(bundle.remove("double"), Ok(true));
        assert_eq!(bundle.get_bool("bool"), Ok(None));
        assert_eq!(bundle.get_int("int"), Ok(None));
        assert_eq!(bundle.get_long("long"), Ok(None));
        assert_eq!(bundle.get_double("double"), Ok(None));
        assert_eq!(bundle.size(), 0);
    }

    #[test]
    fn insert_get_string() {
        let mut bundle = PersistableBundle::new();

        assert_eq!(bundle.insert_string("string", "foo"), Ok(()));
        assert_eq!(bundle.insert_string("empty", ""), Ok(()));
        assert_eq!(bundle.size(), 2);

        assert_eq!(bundle.get_string("string"), Ok(Some("foo".to_string())));
        assert_eq!(bundle.get_string("empty"), Ok(Some("".to_string())));
    }

    #[test]
    fn insert_get_vec() {
        let mut bundle = PersistableBundle::new();

        assert_eq!(bundle.insert_bool_vec("bool", &[]), Ok(()));
        assert_eq!(bundle.insert_int_vec("int", &[42]), Ok(()));
        assert_eq!(bundle.insert_long_vec("long", &[66, 67, 68]), Ok(()));
        assert_eq!(bundle.insert_double_vec("double", &[123.4]), Ok(()));
        assert_eq!(bundle.insert_string_vec("string", &["foo", "bar", "baz"]), Ok(()));
        assert_eq!(
            bundle.insert_string_vec(
                "string",
                &[&"foo".to_string(), &"bar".to_string(), &"baz".to_string()]
            ),
            Ok(())
        );
        assert_eq!(
            bundle.insert_string_vec(
                "string",
                &["foo".to_string(), "bar".to_string(), "baz".to_string()]
            ),
            Ok(())
        );

        assert_eq!(bundle.size(), 5);

        assert_eq!(bundle.get_bool_vec("bool"), Ok(Some(vec![])));
        assert_eq!(bundle.get_int_vec("int"), Ok(Some(vec![42])));
        assert_eq!(bundle.get_long_vec("long"), Ok(Some(vec![66, 67, 68])));
        assert_eq!(bundle.get_double_vec("double"), Ok(Some(vec![123.4])));
        assert_eq!(
            bundle.get_string_vec("string"),
            Ok(Some(vec!["foo".to_string(), "bar".to_string(), "baz".to_string()]))
        );
    }

    #[test]
    fn insert_get_bundle() {
        let mut bundle = PersistableBundle::new();

        let mut sub_bundle = PersistableBundle::new();
        assert_eq!(sub_bundle.insert_int("int", 42), Ok(()));
        assert_eq!(sub_bundle.size(), 1);
        assert_eq!(bundle.insert_persistable_bundle("bundle", &sub_bundle), Ok(()));

        assert_eq!(bundle.get_persistable_bundle("bundle"), Ok(Some(sub_bundle)));
    }

    #[test]
    fn get_keys() {
        let mut bundle = PersistableBundle::new();

        assert_eq!(bundle.keys_for_type(ValueType::Boolean), Vec::<String>::new());
        assert_eq!(bundle.keys_for_type(ValueType::Integer), Vec::<String>::new());
        assert_eq!(bundle.keys_for_type(ValueType::StringVector), Vec::<String>::new());

        assert_eq!(bundle.insert_bool("bool1", false), Ok(()));
        assert_eq!(bundle.insert_bool("bool2", true), Ok(()));
        assert_eq!(bundle.insert_int("int", 42), Ok(()));

        assert_eq!(
            bundle.keys_for_type(ValueType::Boolean),
            vec!["bool1".to_string(), "bool2".to_string()]
        );
        assert_eq!(bundle.keys_for_type(ValueType::Integer), vec!["int".to_string()]);
        assert_eq!(bundle.keys_for_type(ValueType::StringVector), Vec::<String>::new());

        assert_eq!(
            bundle.keys().collect::<Vec<_>>(),
            vec![
                ("bool1".to_string(), ValueType::Boolean),
                ("bool2".to_string(), ValueType::Boolean),
                ("int".to_string(), ValueType::Integer),
            ]
        );
    }
}