binaryninja/
type_archive.rs

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
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
use crate::progress::{NoProgressCallback, ProgressCallback};
use binaryninjacore_sys::*;
use std::ffi::{c_char, c_void, CStr};
use std::fmt::{Debug, Display, Formatter};
use std::hash::Hash;
use std::path::{Path, PathBuf};
use std::ptr::NonNull;

use crate::data_buffer::DataBuffer;
use crate::metadata::Metadata;
use crate::platform::Platform;
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable};
use crate::string::{raw_to_string, BnStrCompatible, BnString};
use crate::type_container::TypeContainer;
use crate::types::{QualifiedName, QualifiedNameAndType, QualifiedNameTypeAndId, Type};

#[repr(transparent)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TypeArchiveSnapshotId(pub String);

impl TypeArchiveSnapshotId {
    pub fn unset() -> Self {
        Self("".to_string())
    }
}

impl Display for TypeArchiveSnapshotId {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{}", self.0))
    }
}

impl CoreArrayProvider for TypeArchiveSnapshotId {
    type Raw = *mut c_char;
    type Context = ();
    type Wrapped<'a> = TypeArchiveSnapshotId;
}

unsafe impl CoreArrayProviderInner for TypeArchiveSnapshotId {
    unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
        BNFreeStringList(raw, count)
    }

    unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
        let str = CStr::from_ptr(*raw).to_str().unwrap().to_string();
        TypeArchiveSnapshotId(str)
    }
}

/// Type Archives are a collection of types which can be shared between different analysis
/// sessions and are backed by a database file on disk. Their types can be modified, and
/// a history of previous versions of types is stored in snapshots in the archive.
pub struct TypeArchive {
    pub(crate) handle: NonNull<BNTypeArchive>,
}

impl TypeArchive {
    pub(crate) unsafe fn from_raw(handle: NonNull<BNTypeArchive>) -> Self {
        Self { handle }
    }

    pub(crate) unsafe fn ref_from_raw(handle: NonNull<BNTypeArchive>) -> Ref<Self> {
        Ref::new(Self { handle })
    }

    /// Open the Type Archive at the given path, if it exists.
    pub fn open(path: impl AsRef<Path>) -> Option<Ref<TypeArchive>> {
        let raw_path = path.as_ref().into_bytes_with_nul();
        let handle = unsafe { BNOpenTypeArchive(raw_path.as_ptr() as *const c_char) };
        NonNull::new(handle).map(|handle| unsafe { TypeArchive::ref_from_raw(handle) })
    }

    /// Create a Type Archive at the given path, returning `None` if it could not be created.
    ///
    /// If the file has already been created and is not a valid type archive this will return `None`.
    pub fn create(path: impl AsRef<Path>, platform: &Platform) -> Option<Ref<TypeArchive>> {
        let raw_path = path.as_ref().into_bytes_with_nul();
        let handle =
            unsafe { BNCreateTypeArchive(raw_path.as_ptr() as *const c_char, platform.handle) };
        NonNull::new(handle).map(|handle| unsafe { TypeArchive::ref_from_raw(handle) })
    }

    /// Create a Type Archive at the given path and id, returning None if it could not be created.
    ///
    /// If the file has already been created and is not a valid type archive this will return `None`.
    pub fn create_with_id<I: BnStrCompatible>(
        path: impl AsRef<Path>,
        id: I,
        platform: &Platform,
    ) -> Option<Ref<TypeArchive>> {
        let raw_path = path.as_ref().into_bytes_with_nul();
        let id = id.into_bytes_with_nul();
        let handle = unsafe {
            BNCreateTypeArchiveWithId(
                raw_path.as_ptr() as *const c_char,
                platform.handle,
                id.as_ref().as_ptr() as *const c_char,
            )
        };
        NonNull::new(handle).map(|handle| unsafe { TypeArchive::ref_from_raw(handle) })
    }

    /// Get a reference to the Type Archive with the known id, if one exists.
    pub fn lookup_by_id<S: BnStrCompatible>(id: S) -> Option<Ref<TypeArchive>> {
        let id = id.into_bytes_with_nul();
        let handle = unsafe { BNLookupTypeArchiveById(id.as_ref().as_ptr() as *const c_char) };
        NonNull::new(handle).map(|handle| unsafe { TypeArchive::ref_from_raw(handle) })
    }

    /// Get the path to the Type Archive's file
    pub fn path(&self) -> Option<PathBuf> {
        let result = unsafe { BNGetTypeArchivePath(self.handle.as_ptr()) };
        match result.is_null() {
            false => {
                let bn_res = unsafe { BnString::from_raw(result) };
                Some(PathBuf::from(bn_res.to_string()))
            }
            true => None,
        }
    }

    /// Get the guid for a Type Archive
    pub fn id(&self) -> Option<BnString> {
        let result = unsafe { BNGetTypeArchiveId(self.handle.as_ptr()) };
        (!result.is_null()).then(|| unsafe { BnString::from_raw(result) })
    }

    /// Get the associated Platform for a Type Archive
    pub fn platform(&self) -> Ref<Platform> {
        let result = unsafe { BNGetTypeArchivePlatform(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { Platform::ref_from_raw(result) }
    }

    /// Get the id of the current snapshot in the type archive
    pub fn current_snapshot_id(&self) -> TypeArchiveSnapshotId {
        let result = unsafe { BNGetTypeArchiveCurrentSnapshotId(self.handle.as_ptr()) };
        assert!(!result.is_null());
        TypeArchiveSnapshotId(unsafe { BnString::from_raw(result) }.to_string())
    }

    /// Revert the type archive's current snapshot to the given snapshot
    pub fn set_current_snapshot_id(&self, id: &TypeArchiveSnapshotId) {
        unsafe {
            BNSetTypeArchiveCurrentSnapshot(self.handle.as_ptr(), id.0.as_ptr() as *const c_char)
        }
    }

    /// Get a list of every snapshot's id
    pub fn all_snapshot_ids(&self) -> Array<TypeArchiveSnapshotId> {
        let mut count = 0;
        let result = unsafe { BNGetTypeArchiveAllSnapshotIds(self.handle.as_ptr(), &mut count) };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Get the ids of the parents to the given snapshot
    pub fn get_snapshot_parent_ids<S: BnStrCompatible>(
        &self,
        snapshot: &TypeArchiveSnapshotId,
    ) -> Option<Array<BnString>> {
        let mut count = 0;
        let result = unsafe {
            BNGetTypeArchiveSnapshotParentIds(
                self.handle.as_ptr(),
                snapshot.0.as_ptr() as *const c_char,
                &mut count,
            )
        };
        (!result.is_null()).then(|| unsafe { Array::new(result, count, ()) })
    }

    /// Get the ids of the children to the given snapshot
    pub fn get_snapshot_child_ids<S: BnStrCompatible>(
        &self,
        snapshot: &TypeArchiveSnapshotId,
    ) -> Option<Array<BnString>> {
        let mut count = 0;
        let result = unsafe {
            BNGetTypeArchiveSnapshotChildIds(
                self.handle.as_ptr(),
                snapshot.0.as_ptr() as *const c_char,
                &mut count,
            )
        };
        (!result.is_null()).then(|| unsafe { Array::new(result, count, ()) })
    }

    /// Add a named type to the type archive. Type must have all dependant named types added
    /// prior to being added, or this function will fail.
    /// If the type already exists, it will be overwritten.
    ///
    /// * `named_type` - Named type to add
    pub fn add_type(&self, named_type: QualifiedNameAndType) -> bool {
        self.add_types(vec![named_type])
    }

    /// Add named types to the type archive. Types must have all dependant named
    /// types prior to being added, or included in the list, or this function will fail.
    /// Types already existing with any added names will be overwritten.
    ///
    /// * `named_types` - Names and definitions of new types
    pub fn add_types(&self, named_types: Vec<QualifiedNameAndType>) -> bool {
        let new_types_raw: Vec<_> = named_types
            .into_iter()
            .map(QualifiedNameAndType::into_raw)
            .collect();
        let result = unsafe {
            BNAddTypeArchiveTypes(
                self.handle.as_ptr(),
                new_types_raw.as_ptr(),
                new_types_raw.len(),
            )
        };
        for new_type in new_types_raw {
            QualifiedNameAndType::free_raw(new_type);
        }
        result
    }

    /// Change the name of an existing type in the type archive. Returns false if failed.
    ///
    /// * `old_name` - Old type name in archive
    /// * `new_name` - New type name
    pub fn rename_type(&self, old_name: QualifiedName, new_name: QualifiedName) -> bool {
        if let Some(id) = self.get_type_id(old_name) {
            self.rename_type_by_id(id, new_name)
        } else {
            false
        }
    }

    /// Change the name of an existing type in the type archive. Returns false if failed.
    ///
    /// * `id` - Old id of type in archive
    /// * `new_name` - New type name
    pub fn rename_type_by_id<S: BnStrCompatible>(&self, id: S, new_name: QualifiedName) -> bool {
        let id = id.into_bytes_with_nul();
        let raw_name = QualifiedName::into_raw(new_name);
        let result = unsafe {
            BNRenameTypeArchiveType(
                self.handle.as_ptr(),
                id.as_ref().as_ptr() as *const c_char,
                &raw_name,
            )
        };
        QualifiedName::free_raw(raw_name);
        result
    }

    /// Delete an existing type in the type archive.
    pub fn delete_type(&self, name: QualifiedName) -> bool {
        if let Some(type_id) = self.get_type_id(name) {
            self.delete_type_by_id(type_id)
        } else {
            false
        }
    }

    /// Delete an existing type in the type archive.
    pub fn delete_type_by_id<S: BnStrCompatible>(&self, id: S) -> bool {
        let id = id.into_bytes_with_nul();
        let result = unsafe {
            BNDeleteTypeArchiveType(self.handle.as_ptr(), id.as_ref().as_ptr() as *const c_char)
        };
        result
    }

    /// Retrieve a stored type in the archive
    ///
    /// * `name` - Type name
    pub fn get_type_by_name<S: BnStrCompatible>(&self, name: QualifiedName) -> Option<Ref<Type>> {
        self.get_type_by_name_from_snapshot(name, &TypeArchiveSnapshotId::unset())
    }

    /// Retrieve a stored type in the archive
    ///
    /// * `name` - Type name
    /// * `snapshot` - Snapshot id to search for types
    pub fn get_type_by_name_from_snapshot(
        &self,
        name: QualifiedName,
        snapshot: &TypeArchiveSnapshotId,
    ) -> Option<Ref<Type>> {
        let raw_name = QualifiedName::into_raw(name);
        let result = unsafe {
            BNGetTypeArchiveTypeByName(
                self.handle.as_ptr(),
                &raw_name,
                snapshot.0.as_ptr() as *const c_char,
            )
        };
        QualifiedName::free_raw(raw_name);
        (!result.is_null()).then(|| unsafe { Type::ref_from_raw(result) })
    }

    /// Retrieve a stored type in the archive by id
    ///
    /// * `id` - Type id
    pub fn get_type_by_id<I: BnStrCompatible>(&self, id: I) -> Option<Ref<Type>> {
        self.get_type_by_id_from_snapshot(id, &TypeArchiveSnapshotId::unset())
    }

    /// Retrieve a stored type in the archive by id
    ///
    /// * `id` - Type id
    /// * `snapshot` - Snapshot id to search for types
    pub fn get_type_by_id_from_snapshot<I: BnStrCompatible>(
        &self,
        id: I,
        snapshot: &TypeArchiveSnapshotId,
    ) -> Option<Ref<Type>> {
        let id = id.into_bytes_with_nul();
        let result = unsafe {
            BNGetTypeArchiveTypeById(
                self.handle.as_ptr(),
                id.as_ref().as_ptr() as *const c_char,
                snapshot.0.as_ptr() as *const c_char,
            )
        };
        (!result.is_null()).then(|| unsafe { Type::ref_from_raw(result) })
    }

    /// Retrieve a type's name by its id
    ///
    /// * `id` - Type id
    pub fn get_type_name_by_id<I: BnStrCompatible>(&self, id: I) -> QualifiedName {
        self.get_type_name_by_id_from_snapshot(id, &TypeArchiveSnapshotId::unset())
    }

    /// Retrieve a type's name by its id
    ///
    /// * `id` - Type id
    /// * `snapshot` - Snapshot id to search for types
    pub fn get_type_name_by_id_from_snapshot<I: BnStrCompatible>(
        &self,
        id: I,
        snapshot: &TypeArchiveSnapshotId,
    ) -> QualifiedName {
        let id = id.into_bytes_with_nul();
        let result = unsafe {
            BNGetTypeArchiveTypeName(
                self.handle.as_ptr(),
                id.as_ref().as_ptr() as *const c_char,
                snapshot.0.as_ptr() as *const c_char,
            )
        };
        QualifiedName::from_owned_raw(result)
    }

    /// Retrieve a type's id by its name
    ///
    /// * `name` - Type name
    pub fn get_type_id(&self, name: QualifiedName) -> Option<BnString> {
        self.get_type_id_from_snapshot(name, &TypeArchiveSnapshotId::unset())
    }

    /// Retrieve a type's id by its name
    ///
    /// * `name` - Type name
    /// * `snapshot` - Snapshot id to search for types
    pub fn get_type_id_from_snapshot(
        &self,
        name: QualifiedName,
        snapshot: &TypeArchiveSnapshotId,
    ) -> Option<BnString> {
        let raw_name = QualifiedName::into_raw(name);
        let result = unsafe {
            BNGetTypeArchiveTypeId(
                self.handle.as_ptr(),
                &raw_name,
                snapshot.0.as_ptr() as *const c_char,
            )
        };
        QualifiedName::free_raw(raw_name);
        (!result.is_null()).then(|| unsafe { BnString::from_raw(result) })
    }

    /// Retrieve all stored types in the archive at a snapshot
    pub fn get_types_and_ids(&self) -> Array<QualifiedNameTypeAndId> {
        self.get_types_and_ids_from_snapshot(&TypeArchiveSnapshotId::unset())
    }

    /// Retrieve all stored types in the archive at a snapshot
    ///
    /// * `snapshot` - Snapshot id to search for types
    pub fn get_types_and_ids_from_snapshot(
        &self,
        snapshot: &TypeArchiveSnapshotId,
    ) -> Array<QualifiedNameTypeAndId> {
        let mut count = 0;
        let result = unsafe {
            BNGetTypeArchiveTypes(
                self.handle.as_ptr(),
                snapshot.0.as_ptr() as *const c_char,
                &mut count,
            )
        };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Get a list of all types' ids in the archive at a snapshot
    pub fn get_type_ids(&self) -> Array<BnString> {
        self.get_type_ids_from_snapshot(&TypeArchiveSnapshotId::unset())
    }

    /// Get a list of all types' ids in the archive at a snapshot
    ///
    /// * `snapshot` - Snapshot id to search for types
    pub fn get_type_ids_from_snapshot(&self, snapshot: &TypeArchiveSnapshotId) -> Array<BnString> {
        let mut count = 0;
        let result = unsafe {
            BNGetTypeArchiveTypeIds(
                self.handle.as_ptr(),
                snapshot.0.as_ptr() as *const c_char,
                &mut count,
            )
        };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Get a list of all types' names in the archive at a snapshot
    pub fn get_type_names(&self) -> Array<QualifiedName> {
        self.get_type_names_from_snapshot(&TypeArchiveSnapshotId::unset())
    }

    /// Get a list of all types' names in the archive at a snapshot
    ///
    /// * `snapshot` - Snapshot id to search for types
    pub fn get_type_names_from_snapshot(
        &self,
        snapshot: &TypeArchiveSnapshotId,
    ) -> Array<QualifiedName> {
        let mut count = 0;
        let result = unsafe {
            BNGetTypeArchiveTypeNames(
                self.handle.as_ptr(),
                snapshot.0.as_ptr() as *const c_char,
                &mut count,
            )
        };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Get a list of all types' names and ids in the archive at the latest snapshot
    pub fn get_type_names_and_ids(&self) -> (Array<QualifiedName>, Array<BnString>) {
        self.get_type_names_and_ids_from_snapshot(&TypeArchiveSnapshotId::unset())
    }

    /// Get a list of all types' names and ids in the archive at a specific snapshot
    ///
    /// * `snapshot` - Snapshot id to search for types
    pub fn get_type_names_and_ids_from_snapshot(
        &self,
        snapshot: &TypeArchiveSnapshotId,
    ) -> (Array<QualifiedName>, Array<BnString>) {
        let mut count = 0;
        let mut names = std::ptr::null_mut();
        let mut ids = std::ptr::null_mut();
        let result = unsafe {
            BNGetTypeArchiveTypeNamesAndIds(
                self.handle.as_ptr(),
                snapshot.0.as_ptr() as *const c_char,
                &mut names,
                &mut ids,
                &mut count,
            )
        };
        assert!(result);
        (unsafe { Array::new(names, count, ()) }, unsafe {
            Array::new(ids, count, ())
        })
    }

    /// Get all types a given type references directly
    ///
    /// * `id` - Source type id
    pub fn get_outgoing_direct_references<I: BnStrCompatible>(&self, id: I) -> Array<BnString> {
        self.get_outgoing_direct_references_from_snapshot(id, &TypeArchiveSnapshotId::unset())
    }

    /// Get all types a given type references directly
    ///
    /// * `id` - Source type id
    /// * `snapshot` - Snapshot id to search for types
    pub fn get_outgoing_direct_references_from_snapshot<I: BnStrCompatible>(
        &self,
        id: I,
        snapshot: &TypeArchiveSnapshotId,
    ) -> Array<BnString> {
        let id = id.into_bytes_with_nul();
        let mut count = 0;
        let result = unsafe {
            BNGetTypeArchiveOutgoingDirectTypeReferences(
                self.handle.as_ptr(),
                id.as_ref().as_ptr() as *const c_char,
                snapshot.0.as_ptr() as *const c_char,
                &mut count,
            )
        };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Get all types a given type references, and any types that the referenced types reference
    ///
    /// * `id` - Source type id
    pub fn get_outgoing_recursive_references<I: BnStrCompatible>(&self, id: I) -> Array<BnString> {
        self.get_outgoing_recursive_references_from_snapshot(id, &TypeArchiveSnapshotId::unset())
    }

    /// Get all types a given type references, and any types that the referenced types reference
    ///
    /// * `id` - Source type id
    /// * `snapshot` - Snapshot id to search for types
    pub fn get_outgoing_recursive_references_from_snapshot<I: BnStrCompatible>(
        &self,
        id: I,
        snapshot: &TypeArchiveSnapshotId,
    ) -> Array<BnString> {
        let id = id.into_bytes_with_nul();
        let mut count = 0;
        let result = unsafe {
            BNGetTypeArchiveOutgoingRecursiveTypeReferences(
                self.handle.as_ptr(),
                id.as_ref().as_ptr() as *const c_char,
                snapshot.0.as_ptr() as *const c_char,
                &mut count,
            )
        };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Get all types that reference a given type
    ///
    /// * `id` - Target type id
    pub fn get_incoming_direct_references<I: BnStrCompatible>(&self, id: I) -> Array<BnString> {
        self.get_incoming_direct_references_with_snapshot(id, &TypeArchiveSnapshotId::unset())
    }

    /// Get all types that reference a given type
    ///
    /// * `id` - Target type id
    /// * `snapshot` - Snapshot id to search for types
    pub fn get_incoming_direct_references_with_snapshot<I: BnStrCompatible>(
        &self,
        id: I,
        snapshot: &TypeArchiveSnapshotId,
    ) -> Array<BnString> {
        let id = id.into_bytes_with_nul();
        let mut count = 0;
        let result = unsafe {
            BNGetTypeArchiveIncomingDirectTypeReferences(
                self.handle.as_ptr(),
                id.as_ref().as_ptr() as *const c_char,
                snapshot.0.as_ptr() as *const c_char,
                &mut count,
            )
        };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Get all types that reference a given type, and all types that reference them, recursively
    ///
    /// * `id` - Target type id
    pub fn get_incoming_recursive_references<I: BnStrCompatible>(&self, id: I) -> Array<BnString> {
        self.get_incoming_recursive_references_with_snapshot(id, &TypeArchiveSnapshotId::unset())
    }

    /// Get all types that reference a given type, and all types that reference them, recursively
    ///
    /// * `id` - Target type id
    /// * `snapshot` - Snapshot id to search for types, or empty string to search the latest snapshot
    pub fn get_incoming_recursive_references_with_snapshot<I: BnStrCompatible>(
        &self,
        id: I,
        snapshot: &TypeArchiveSnapshotId,
    ) -> Array<BnString> {
        let id = id.into_bytes_with_nul();
        let mut count = 0;
        let result = unsafe {
            BNGetTypeArchiveIncomingRecursiveTypeReferences(
                self.handle.as_ptr(),
                id.as_ref().as_ptr() as *const c_char,
                snapshot.0.as_ptr() as *const c_char,
                &mut count,
            )
        };
        assert!(!result.is_null());
        unsafe { Array::new(result, count, ()) }
    }

    /// Look up a metadata entry in the archive
    pub fn query_metadata<S: BnStrCompatible>(&self, key: S) -> Option<Ref<Metadata>> {
        let key = key.into_bytes_with_nul();
        let result = unsafe {
            BNTypeArchiveQueryMetadata(self.handle.as_ptr(), key.as_ref().as_ptr() as *const c_char)
        };
        (!result.is_null()).then(|| unsafe { Metadata::ref_from_raw(result) })
    }

    /// Store a key/value pair in the archive's metadata storage
    ///
    /// * `key` - key value to associate the Metadata object with
    /// * `md` - object to store.
    pub fn store_metadata<S: BnStrCompatible>(&self, key: S, md: &Metadata) {
        let key = key.into_bytes_with_nul();
        let result = unsafe {
            BNTypeArchiveStoreMetadata(
                self.handle.as_ptr(),
                key.as_ref().as_ptr() as *const c_char,
                md.handle,
            )
        };
        assert!(result);
    }

    /// Delete a given metadata entry in the archive from the `key`
    pub fn remove_metadata<S: BnStrCompatible>(&self, key: S) -> bool {
        let key = key.into_bytes_with_nul();
        unsafe {
            BNTypeArchiveRemoveMetadata(
                self.handle.as_ptr(),
                key.as_ref().as_ptr() as *const c_char,
            )
        }
    }

    /// Turn a given `snapshot` id into a data stream
    pub fn serialize_snapshot<S: BnStrCompatible>(
        &self,
        snapshot: &TypeArchiveSnapshotId,
    ) -> DataBuffer {
        let result = unsafe {
            BNTypeArchiveSerializeSnapshot(
                self.handle.as_ptr(),
                snapshot.0.as_ptr() as *const c_char,
            )
        };
        assert!(!result.is_null());
        DataBuffer::from_raw(result)
    }

    /// Take a serialized snapshot `data` stream and create a new snapshot from it
    pub fn deserialize_snapshot(&self, data: &DataBuffer) -> TypeArchiveSnapshotId {
        let result =
            unsafe { BNTypeArchiveDeserializeSnapshot(self.handle.as_ptr(), data.as_raw()) };
        assert!(!result.is_null());
        TypeArchiveSnapshotId(unsafe { BnString::from_raw(result) }.to_string())
    }

    /// Register a notification listener
    pub fn register_notification_callback<T: TypeArchiveNotificationCallback>(
        &self,
        callback: T,
    ) -> TypeArchiveCallbackHandle<T> {
        // SAFETY free on [TypeArchiveCallbackHandle::Drop]
        let callback = Box::leak(Box::new(callback));
        let mut notification = BNTypeArchiveNotification {
            context: callback as *mut T as *mut c_void,
            typeAdded: Some(cb_type_added::<T>),
            typeUpdated: Some(cb_type_updated::<T>),
            typeRenamed: Some(cb_type_renamed::<T>),
            typeDeleted: Some(cb_type_deleted::<T>),
        };
        unsafe { BNRegisterTypeArchiveNotification(self.handle.as_ptr(), &mut notification) }
        TypeArchiveCallbackHandle {
            callback,
            type_archive: self.to_owned(),
        }
    }

    // NOTE NotificationClosure is left private, there is no need for the user
    // to know or use it.
    #[allow(private_interfaces)]
    pub fn register_notification_closure<A, U, R, D>(
        &self,
        type_added: A,
        type_updated: U,
        type_renamed: R,
        type_deleted: D,
    ) -> TypeArchiveCallbackHandle<NotificationClosure<A, U, R, D>>
    where
        A: FnMut(&TypeArchive, &str, &Type),
        U: FnMut(&TypeArchive, &str, &Type, &Type),
        R: FnMut(&TypeArchive, &str, &QualifiedName, &QualifiedName),
        D: FnMut(&TypeArchive, &str, &Type),
    {
        self.register_notification_callback(NotificationClosure {
            fun_type_added: type_added,
            fun_type_updated: type_updated,
            fun_type_renamed: type_renamed,
            fun_type_deleted: type_deleted,
        })
    }

    /// Close a type archive, disconnecting it from any active views and closing
    /// any open file handles
    pub fn close(&self) {
        unsafe { BNCloseTypeArchive(self.handle.as_ptr()) }
    }

    // TODO: Make this AsRef<Path>?
    /// Determine if `file` is a Type Archive
    pub fn is_type_archive<P: BnStrCompatible>(file: P) -> bool {
        let file = file.into_bytes_with_nul();
        unsafe { BNIsTypeArchive(file.as_ref().as_ptr() as *const c_char) }
    }

    ///// Get the TypeContainer interface for this Type Archive, presenting types
    ///// at the current snapshot in the archive.
    pub fn type_container(&self) -> TypeContainer {
        let result = unsafe { BNGetTypeArchiveTypeContainer(self.handle.as_ptr()) };
        unsafe { TypeContainer::from_raw(NonNull::new(result).unwrap()) }
    }

    /// Do some function in a transaction making a new snapshot whose id is passed to func. If func throws,
    /// the transaction will be rolled back and the snapshot will not be created.
    ///
    /// * `func` - Function to call
    /// * `parents` - Parent snapshot ids
    ///
    /// Returns Created snapshot id
    pub fn new_snapshot_transaction<P, F>(
        &self,
        mut function: F,
        parents: &[TypeArchiveSnapshotId],
    ) -> TypeArchiveSnapshotId
    where
        P: BnStrCompatible,
        F: FnMut(&TypeArchiveSnapshotId) -> bool,
    {
        unsafe extern "C" fn cb_callback<F: FnMut(&TypeArchiveSnapshotId) -> bool>(
            ctxt: *mut c_void,
            id: *const c_char,
        ) -> bool {
            let fun: &mut F = &mut *(ctxt as *mut F);
            let id_str = raw_to_string(id).unwrap();
            fun(&TypeArchiveSnapshotId(id_str))
        }

        // SAFETY TypeArchiveSnapshotId and `*const c_char` are transparent
        let parents_raw = parents.as_ptr() as *const *const c_char;

        let result = unsafe {
            BNTypeArchiveNewSnapshotTransaction(
                self.handle.as_ptr(),
                Some(cb_callback::<F>),
                &mut function as *mut F as *mut c_void,
                parents_raw,
                parents.len(),
            )
        };
        assert!(!result.is_null());
        let id_str = unsafe { BnString::from_raw(result) };
        TypeArchiveSnapshotId(id_str.to_string())
    }

    /// Merge two snapshots in the archive to produce a new snapshot
    ///
    /// * `base_snapshot` - Common ancestor of snapshots
    /// * `first_snapshot` - First snapshot to merge
    /// * `second_snapshot` - Second snapshot to merge
    /// * `merge_conflicts` - List of all conflicting types, id <-> target snapshot
    /// * `progress` - Function to call for progress updates
    ///
    /// Returns Snapshot id, if merge was successful, otherwise the List of
    /// conflicting type ids
    pub fn merge_snapshots<B, F, S, M, MI, MK>(
        &self,
        base_snapshot: B,
        first_snapshot: F,
        second_snapshot: S,
        merge_conflicts: M,
    ) -> Result<BnString, Array<BnString>>
    where
        B: BnStrCompatible,
        F: BnStrCompatible,
        S: BnStrCompatible,
        M: IntoIterator<Item = (MI, MK)>,
        MI: BnStrCompatible,
        MK: BnStrCompatible,
    {
        self.merge_snapshots_with_progress(
            base_snapshot,
            first_snapshot,
            second_snapshot,
            merge_conflicts,
            NoProgressCallback,
        )
    }

    /// Merge two snapshots in the archive to produce a new snapshot
    ///
    /// * `base_snapshot` - Common ancestor of snapshots
    /// * `first_snapshot` - First snapshot to merge
    /// * `second_snapshot` - Second snapshot to merge
    /// * `merge_conflicts` - List of all conflicting types, id <-> target snapshot
    /// * `progress` - Function to call for progress updates
    ///
    /// Returns Snapshot id, if merge was successful, otherwise the List of
    /// conflicting type ids
    pub fn merge_snapshots_with_progress<B, F, S, M, MI, MK, P>(
        &self,
        base_snapshot: B,
        first_snapshot: F,
        second_snapshot: S,
        merge_conflicts: M,
        mut progress: P,
    ) -> Result<BnString, Array<BnString>>
    where
        B: BnStrCompatible,
        F: BnStrCompatible,
        S: BnStrCompatible,
        M: IntoIterator<Item = (MI, MK)>,
        MI: BnStrCompatible,
        MK: BnStrCompatible,
        P: ProgressCallback,
    {
        let base_snapshot = base_snapshot.into_bytes_with_nul();
        let first_snapshot = first_snapshot.into_bytes_with_nul();
        let second_snapshot = second_snapshot.into_bytes_with_nul();
        let (merge_keys, merge_values): (Vec<BnString>, Vec<BnString>) = merge_conflicts
            .into_iter()
            .map(|(k, v)| (BnString::new(k), BnString::new(v)))
            .unzip();
        // SAFETY BnString and `*const c_char` are transparent
        let merge_keys_raw = merge_keys.as_ptr() as *const *const c_char;
        let merge_values_raw = merge_values.as_ptr() as *const *const c_char;

        let mut conflicts_errors = std::ptr::null_mut();
        let mut conflicts_errors_count = 0;

        let mut result = std::ptr::null_mut();

        let success = unsafe {
            BNTypeArchiveMergeSnapshots(
                self.handle.as_ptr(),
                base_snapshot.as_ref().as_ptr() as *const c_char,
                first_snapshot.as_ref().as_ptr() as *const c_char,
                second_snapshot.as_ref().as_ptr() as *const c_char,
                merge_keys_raw,
                merge_values_raw,
                merge_keys.len(),
                &mut conflicts_errors,
                &mut conflicts_errors_count,
                &mut result,
                Some(P::cb_progress_callback),
                &mut progress as *mut P as *mut c_void,
            )
        };

        if success {
            assert!(!result.is_null());
            Ok(unsafe { BnString::from_raw(result) })
        } else {
            assert!(!conflicts_errors.is_null());
            Err(unsafe { Array::new(conflicts_errors, conflicts_errors_count, ()) })
        }
    }
}

impl ToOwned for TypeArchive {
    type Owned = Ref<Self>;

    fn to_owned(&self) -> Self::Owned {
        unsafe { RefCountable::inc_ref(self) }
    }
}

unsafe impl RefCountable for TypeArchive {
    unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
        Ref::new(Self {
            handle: NonNull::new(BNNewTypeArchiveReference(handle.handle.as_ptr())).unwrap(),
        })
    }

    unsafe fn dec_ref(handle: &Self) {
        BNFreeTypeArchiveReference(handle.handle.as_ptr());
    }
}

impl PartialEq for TypeArchive {
    fn eq(&self, other: &Self) -> bool {
        self.id() == other.id()
    }
}
impl Eq for TypeArchive {}

impl Hash for TypeArchive {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        (self.handle.as_ptr() as usize).hash(state);
    }
}

impl Debug for TypeArchive {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TypeArchive")
            .field("id", &self.id())
            .field("path", &self.path())
            .field("current_snapshot_id", &self.current_snapshot_id())
            .field("platform", &self.platform())
            .finish()
    }
}

impl CoreArrayProvider for TypeArchive {
    type Raw = *mut BNTypeArchive;
    type Context = ();
    type Wrapped<'a> = Guard<'a, TypeArchive>;
}

unsafe impl CoreArrayProviderInner for TypeArchive {
    unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
        BNFreeTypeArchiveList(raw, count)
    }

    unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
        let raw_ptr = NonNull::new(*raw).unwrap();
        Guard::new(Self::from_raw(raw_ptr), context)
    }
}

pub struct TypeArchiveCallbackHandle<T: TypeArchiveNotificationCallback> {
    callback: *mut T,
    type_archive: Ref<TypeArchive>,
}

impl<T: TypeArchiveNotificationCallback> Drop for TypeArchiveCallbackHandle<T> {
    fn drop(&mut self) {
        let mut notification = BNTypeArchiveNotification {
            context: self.callback as *mut c_void,
            typeAdded: Some(cb_type_added::<T>),
            typeUpdated: Some(cb_type_updated::<T>),
            typeRenamed: Some(cb_type_renamed::<T>),
            typeDeleted: Some(cb_type_deleted::<T>),
        };
        // unregister the notification callback
        unsafe {
            BNUnregisterTypeArchiveNotification(
                self.type_archive.handle.as_ptr(),
                &mut notification,
            )
        }
        // free the context created at [TypeArchive::register_notification_callback]
        drop(unsafe { Box::from_raw(self.callback) });
    }
}

pub trait TypeArchiveNotificationCallback {
    /// Called when a type is added to the archive
    ///
    /// * `archive` - Source Type archive
    /// * `id` - Id of type added
    /// * `definition` - Definition of type
    fn type_added(&mut self, _archive: &TypeArchive, _id: &str, _definition: &Type) {}

    /// Called when a type in the archive is updated to a new definition
    ///
    /// * `archive` - Source Type archive
    /// * `id` - Id of type
    /// * `old_definition` - Previous definition
    /// * `new_definition` - Current definition
    fn type_updated(
        &mut self,
        _archive: &TypeArchive,
        _id: &str,
        _old_definition: &Type,
        _new_definition: &Type,
    ) {
    }

    /// Called when a type in the archive is renamed
    ///
    /// * `archive` - Source Type archive
    /// * `id` - Type id
    /// * `old_name` - Previous name
    /// * `new_name` - Current name
    fn type_renamed(
        &mut self,
        _archive: &TypeArchive,
        _id: &str,
        _old_name: &QualifiedName,
        _new_name: &QualifiedName,
    ) {
    }

    /// Called when a type in the archive is deleted from the archive
    ///
    /// * `archive` - Source Type archive
    /// * `id` - Id of type deleted
    /// * `definition` - Definition of type deleted
    fn type_deleted(&mut self, _archive: &TypeArchive, _id: &str, _definition: &Type) {}
}

struct NotificationClosure<A, U, R, D>
where
    A: FnMut(&TypeArchive, &str, &Type),
    U: FnMut(&TypeArchive, &str, &Type, &Type),
    R: FnMut(&TypeArchive, &str, &QualifiedName, &QualifiedName),
    D: FnMut(&TypeArchive, &str, &Type),
{
    fun_type_added: A,
    fun_type_updated: U,
    fun_type_renamed: R,
    fun_type_deleted: D,
}

impl<A, U, R, D> TypeArchiveNotificationCallback for NotificationClosure<A, U, R, D>
where
    A: FnMut(&TypeArchive, &str, &Type),
    U: FnMut(&TypeArchive, &str, &Type, &Type),
    R: FnMut(&TypeArchive, &str, &QualifiedName, &QualifiedName),
    D: FnMut(&TypeArchive, &str, &Type),
{
    fn type_added(&mut self, archive: &TypeArchive, id: &str, definition: &Type) {
        (self.fun_type_added)(archive, id, definition)
    }

    fn type_updated(
        &mut self,
        archive: &TypeArchive,
        id: &str,
        old_definition: &Type,
        new_definition: &Type,
    ) {
        (self.fun_type_updated)(archive, id, old_definition, new_definition)
    }

    fn type_renamed(
        &mut self,
        archive: &TypeArchive,
        id: &str,
        old_name: &QualifiedName,
        new_name: &QualifiedName,
    ) {
        (self.fun_type_renamed)(archive, id, old_name, new_name)
    }

    fn type_deleted(&mut self, archive: &TypeArchive, id: &str, definition: &Type) {
        (self.fun_type_deleted)(archive, id, definition)
    }
}

unsafe extern "C" fn cb_type_added<T: TypeArchiveNotificationCallback>(
    ctxt: *mut ::std::os::raw::c_void,
    archive: *mut BNTypeArchive,
    id: *const ::std::os::raw::c_char,
    definition: *mut BNType,
) {
    let ctxt: &mut T = &mut *(ctxt as *mut T);
    // `archive` is owned by the caller.
    let archive = unsafe { TypeArchive::from_raw(NonNull::new(archive).unwrap()) };
    ctxt.type_added(
        &archive,
        unsafe { CStr::from_ptr(id).to_string_lossy().as_ref() },
        &Type { handle: definition },
    )
}
unsafe extern "C" fn cb_type_updated<T: TypeArchiveNotificationCallback>(
    ctxt: *mut ::std::os::raw::c_void,
    archive: *mut BNTypeArchive,
    id: *const ::std::os::raw::c_char,
    old_definition: *mut BNType,
    new_definition: *mut BNType,
) {
    let ctxt: &mut T = &mut *(ctxt as *mut T);
    // `archive` is owned by the caller.
    let archive = unsafe { TypeArchive::from_raw(NonNull::new(archive).unwrap()) };
    ctxt.type_updated(
        &archive,
        unsafe { CStr::from_ptr(id).to_string_lossy().as_ref() },
        &Type {
            handle: old_definition,
        },
        &Type {
            handle: new_definition,
        },
    )
}
unsafe extern "C" fn cb_type_renamed<T: TypeArchiveNotificationCallback>(
    ctxt: *mut ::std::os::raw::c_void,
    archive: *mut BNTypeArchive,
    id: *const ::std::os::raw::c_char,
    old_name: *const BNQualifiedName,
    new_name: *const BNQualifiedName,
) {
    let ctxt: &mut T = &mut *(ctxt as *mut T);
    // `old_name` is freed by the caller
    let old_name = QualifiedName::from_raw(&*old_name);
    // `new_name` is freed by the caller
    let new_name = QualifiedName::from_raw(&*new_name);
    // `archive` is owned by the caller.
    let archive = unsafe { TypeArchive::from_raw(NonNull::new(archive).unwrap()) };
    ctxt.type_renamed(
        &archive,
        unsafe { CStr::from_ptr(id).to_string_lossy().as_ref() },
        &old_name,
        &new_name,
    )
}
unsafe extern "C" fn cb_type_deleted<T: TypeArchiveNotificationCallback>(
    ctxt: *mut ::std::os::raw::c_void,
    archive: *mut BNTypeArchive,
    id: *const ::std::os::raw::c_char,
    definition: *mut BNType,
) {
    let ctxt: &mut T = &mut *(ctxt as *mut T);
    // `archive` is owned by the caller.
    let archive = unsafe { TypeArchive::from_raw(NonNull::new(archive).unwrap()) };
    ctxt.type_deleted(
        &archive,
        unsafe { CStr::from_ptr(id).to_string_lossy().as_ref() },
        &Type { handle: definition },
    )
}

#[repr(transparent)]
pub struct TypeArchiveMergeConflict {
    handle: NonNull<BNTypeArchiveMergeConflict>,
}

impl TypeArchiveMergeConflict {
    pub(crate) unsafe fn from_raw(handle: NonNull<BNTypeArchiveMergeConflict>) -> Self {
        Self { handle }
    }

    #[allow(unused)]
    pub(crate) unsafe fn ref_from_raw(handle: NonNull<BNTypeArchiveMergeConflict>) -> Ref<Self> {
        Ref::new(Self { handle })
    }

    pub fn get_type_archive(&self) -> Option<Ref<TypeArchive>> {
        let value = unsafe { BNTypeArchiveMergeConflictGetTypeArchive(self.handle.as_ptr()) };
        NonNull::new(value).map(|handle| unsafe { TypeArchive::ref_from_raw(handle) })
    }

    pub fn type_id(&self) -> BnString {
        let value = unsafe { BNTypeArchiveMergeConflictGetTypeId(self.handle.as_ptr()) };
        assert!(!value.is_null());
        unsafe { BnString::from_raw(value) }
    }

    pub fn base_snapshot_id(&self) -> TypeArchiveSnapshotId {
        let value = unsafe { BNTypeArchiveMergeConflictGetBaseSnapshotId(self.handle.as_ptr()) };
        assert!(!value.is_null());
        let id = unsafe { BnString::from_raw(value) }.to_string();
        TypeArchiveSnapshotId(id)
    }

    pub fn first_snapshot_id(&self) -> TypeArchiveSnapshotId {
        let value = unsafe { BNTypeArchiveMergeConflictGetFirstSnapshotId(self.handle.as_ptr()) };
        assert!(!value.is_null());
        let id = unsafe { BnString::from_raw(value) }.to_string();
        TypeArchiveSnapshotId(id)
    }

    pub fn second_snapshot_id(&self) -> TypeArchiveSnapshotId {
        let value = unsafe { BNTypeArchiveMergeConflictGetSecondSnapshotId(self.handle.as_ptr()) };
        assert!(!value.is_null());
        let id = unsafe { BnString::from_raw(value) }.to_string();
        TypeArchiveSnapshotId(id)
    }

    // TODO: This needs documentation!
    pub fn success<S: BnStrCompatible>(&self, value: S) -> bool {
        let value = value.into_bytes_with_nul();
        unsafe {
            BNTypeArchiveMergeConflictSuccess(
                self.handle.as_ptr(),
                value.as_ref().as_ptr() as *const c_char,
            )
        }
    }
}

impl Debug for TypeArchiveMergeConflict {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TypeArchiveMergeConflict")
            .field("type_id", &self.type_id())
            .field("base_snapshot_id", &self.base_snapshot_id())
            .field("first_snapshot_id", &self.first_snapshot_id())
            .field("second_snapshot_id", &self.second_snapshot_id())
            .finish()
    }
}

impl ToOwned for TypeArchiveMergeConflict {
    type Owned = Ref<Self>;

    fn to_owned(&self) -> Self::Owned {
        unsafe { RefCountable::inc_ref(self) }
    }
}

unsafe impl RefCountable for TypeArchiveMergeConflict {
    unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
        Ref::new(Self {
            handle: NonNull::new(BNNewTypeArchiveMergeConflictReference(
                handle.handle.as_ptr(),
            ))
            .unwrap(),
        })
    }

    unsafe fn dec_ref(handle: &Self) {
        BNFreeTypeArchiveMergeConflict(handle.handle.as_ptr());
    }
}

impl CoreArrayProvider for TypeArchiveMergeConflict {
    type Raw = *mut BNTypeArchiveMergeConflict;
    type Context = ();
    type Wrapped<'a> = Guard<'a, Self>;
}

unsafe impl CoreArrayProviderInner for TypeArchiveMergeConflict {
    unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
        BNFreeTypeArchiveMergeConflictList(raw, count)
    }

    unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
        let raw_ptr = NonNull::new(*raw).unwrap();
        Guard::new(Self::from_raw(raw_ptr), context)
    }
}