Skip to content

Commit 4f2257f

Browse files
committed
[Rust] Keep TypeArchiveSnapshotId as a c_str when passing to core
1 parent 397b5fa commit 4f2257f

File tree

1 file changed

+39
-69
lines changed

1 file changed

+39
-69
lines changed

rust/src/type_archive.rs

Lines changed: 39 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,8 @@ impl TypeArchive {
133133

134134
/// Revert the type archive's current snapshot to the given snapshot
135135
pub fn set_current_snapshot_id(&self, id: &TypeArchiveSnapshotId) {
136-
unsafe {
137-
BNSetTypeArchiveCurrentSnapshot(self.handle.as_ptr(), id.0.as_ptr() as *const c_char)
138-
}
136+
let snapshot = id.clone().to_cstr();
137+
unsafe { BNSetTypeArchiveCurrentSnapshot(self.handle.as_ptr(), snapshot.as_ptr()) }
139138
}
140139

141140
/// Get a list of every snapshot's id
@@ -152,12 +151,9 @@ impl TypeArchive {
152151
snapshot: &TypeArchiveSnapshotId,
153152
) -> Option<Array<BnString>> {
154153
let mut count = 0;
154+
let snapshot = snapshot.clone().to_cstr();
155155
let result = unsafe {
156-
BNGetTypeArchiveSnapshotParentIds(
157-
self.handle.as_ptr(),
158-
snapshot.0.as_ptr() as *const c_char,
159-
&mut count,
160-
)
156+
BNGetTypeArchiveSnapshotParentIds(self.handle.as_ptr(), snapshot.as_ptr(), &mut count)
161157
};
162158
(!result.is_null()).then(|| unsafe { Array::new(result, count, ()) })
163159
}
@@ -168,12 +164,9 @@ impl TypeArchive {
168164
snapshot: &TypeArchiveSnapshotId,
169165
) -> Option<Array<BnString>> {
170166
let mut count = 0;
167+
let snapshot = snapshot.clone().to_cstr();
171168
let result = unsafe {
172-
BNGetTypeArchiveSnapshotChildIds(
173-
self.handle.as_ptr(),
174-
snapshot.0.as_ptr() as *const c_char,
175-
&mut count,
176-
)
169+
BNGetTypeArchiveSnapshotChildIds(self.handle.as_ptr(), snapshot.as_ptr(), &mut count)
177170
};
178171
(!result.is_null()).then(|| unsafe { Array::new(result, count, ()) })
179172
}
@@ -267,12 +260,9 @@ impl TypeArchive {
267260
snapshot: &TypeArchiveSnapshotId,
268261
) -> Option<Ref<Type>> {
269262
let raw_name = QualifiedName::into_raw(name);
263+
let snapshot = snapshot.clone().to_cstr();
270264
let result = unsafe {
271-
BNGetTypeArchiveTypeByName(
272-
self.handle.as_ptr(),
273-
&raw_name,
274-
snapshot.0.as_ptr() as *const c_char,
275-
)
265+
BNGetTypeArchiveTypeByName(self.handle.as_ptr(), &raw_name, snapshot.as_ptr())
276266
};
277267
QualifiedName::free_raw(raw_name);
278268
(!result.is_null()).then(|| unsafe { Type::ref_from_raw(result) })
@@ -295,12 +285,9 @@ impl TypeArchive {
295285
snapshot: &TypeArchiveSnapshotId,
296286
) -> Option<Ref<Type>> {
297287
let id = id.to_cstr();
288+
let snapshot = snapshot.clone().to_cstr();
298289
let result = unsafe {
299-
BNGetTypeArchiveTypeById(
300-
self.handle.as_ptr(),
301-
id.as_ptr(),
302-
snapshot.0.as_ptr() as *const c_char,
303-
)
290+
BNGetTypeArchiveTypeById(self.handle.as_ptr(), id.as_ptr(), snapshot.as_ptr())
304291
};
305292
(!result.is_null()).then(|| unsafe { Type::ref_from_raw(result) })
306293
}
@@ -322,12 +309,9 @@ impl TypeArchive {
322309
snapshot: &TypeArchiveSnapshotId,
323310
) -> QualifiedName {
324311
let id = id.to_cstr();
312+
let snapshot = snapshot.clone().to_cstr();
325313
let result = unsafe {
326-
BNGetTypeArchiveTypeName(
327-
self.handle.as_ptr(),
328-
id.as_ptr(),
329-
snapshot.0.as_ptr() as *const c_char,
330-
)
314+
BNGetTypeArchiveTypeName(self.handle.as_ptr(), id.as_ptr(), snapshot.as_ptr())
331315
};
332316
QualifiedName::from_owned_raw(result)
333317
}
@@ -349,13 +333,9 @@ impl TypeArchive {
349333
snapshot: &TypeArchiveSnapshotId,
350334
) -> Option<String> {
351335
let raw_name = QualifiedName::into_raw(name);
352-
let result = unsafe {
353-
BNGetTypeArchiveTypeId(
354-
self.handle.as_ptr(),
355-
&raw_name,
356-
snapshot.0.as_ptr() as *const c_char,
357-
)
358-
};
336+
let snapshot = snapshot.clone().to_cstr();
337+
let result =
338+
unsafe { BNGetTypeArchiveTypeId(self.handle.as_ptr(), &raw_name, snapshot.as_ptr()) };
359339
QualifiedName::free_raw(raw_name);
360340
(!result.is_null()).then(|| unsafe { BnString::into_string(result) })
361341
}
@@ -373,13 +353,9 @@ impl TypeArchive {
373353
snapshot: &TypeArchiveSnapshotId,
374354
) -> Array<QualifiedNameTypeAndId> {
375355
let mut count = 0;
376-
let result = unsafe {
377-
BNGetTypeArchiveTypes(
378-
self.handle.as_ptr(),
379-
snapshot.0.as_ptr() as *const c_char,
380-
&mut count,
381-
)
382-
};
356+
let snapshot = snapshot.clone().to_cstr();
357+
let result =
358+
unsafe { BNGetTypeArchiveTypes(self.handle.as_ptr(), snapshot.as_ptr(), &mut count) };
383359
assert!(!result.is_null());
384360
unsafe { Array::new(result, count, ()) }
385361
}
@@ -394,13 +370,9 @@ impl TypeArchive {
394370
/// * `snapshot` - Snapshot id to search for types
395371
pub fn get_type_ids_from_snapshot(&self, snapshot: &TypeArchiveSnapshotId) -> Array<BnString> {
396372
let mut count = 0;
397-
let result = unsafe {
398-
BNGetTypeArchiveTypeIds(
399-
self.handle.as_ptr(),
400-
snapshot.0.as_ptr() as *const c_char,
401-
&mut count,
402-
)
403-
};
373+
let snapshot = snapshot.clone().to_cstr();
374+
let result =
375+
unsafe { BNGetTypeArchiveTypeIds(self.handle.as_ptr(), snapshot.as_ptr(), &mut count) };
404376
assert!(!result.is_null());
405377
unsafe { Array::new(result, count, ()) }
406378
}
@@ -418,12 +390,9 @@ impl TypeArchive {
418390
snapshot: &TypeArchiveSnapshotId,
419391
) -> Array<QualifiedName> {
420392
let mut count = 0;
393+
let snapshot = snapshot.clone().to_cstr();
421394
let result = unsafe {
422-
BNGetTypeArchiveTypeNames(
423-
self.handle.as_ptr(),
424-
snapshot.0.as_ptr() as *const c_char,
425-
&mut count,
426-
)
395+
BNGetTypeArchiveTypeNames(self.handle.as_ptr(), snapshot.as_ptr(), &mut count)
427396
};
428397
assert!(!result.is_null());
429398
unsafe { Array::new(result, count, ()) }
@@ -442,12 +411,13 @@ impl TypeArchive {
442411
snapshot: &TypeArchiveSnapshotId,
443412
) -> (Array<QualifiedName>, Array<BnString>) {
444413
let mut count = 0;
414+
let snapshot = snapshot.clone().to_cstr();
445415
let mut names = std::ptr::null_mut();
446416
let mut ids = std::ptr::null_mut();
447417
let result = unsafe {
448418
BNGetTypeArchiveTypeNamesAndIds(
449419
self.handle.as_ptr(),
450-
snapshot.0.as_ptr() as *const c_char,
420+
snapshot.as_ptr(),
451421
&mut names,
452422
&mut ids,
453423
&mut count,
@@ -476,12 +446,13 @@ impl TypeArchive {
476446
snapshot: &TypeArchiveSnapshotId,
477447
) -> Array<BnString> {
478448
let id = id.to_cstr();
449+
let snapshot = snapshot.clone().to_cstr();
479450
let mut count = 0;
480451
let result = unsafe {
481452
BNGetTypeArchiveOutgoingDirectTypeReferences(
482453
self.handle.as_ptr(),
483454
id.as_ptr(),
484-
snapshot.0.as_ptr() as *const c_char,
455+
snapshot.as_ptr(),
485456
&mut count,
486457
)
487458
};
@@ -506,12 +477,13 @@ impl TypeArchive {
506477
snapshot: &TypeArchiveSnapshotId,
507478
) -> Array<BnString> {
508479
let id = id.to_cstr();
480+
let snapshot = snapshot.clone().to_cstr();
509481
let mut count = 0;
510482
let result = unsafe {
511483
BNGetTypeArchiveOutgoingRecursiveTypeReferences(
512484
self.handle.as_ptr(),
513485
id.as_ptr(),
514-
snapshot.0.as_ptr() as *const c_char,
486+
snapshot.as_ptr(),
515487
&mut count,
516488
)
517489
};
@@ -536,12 +508,13 @@ impl TypeArchive {
536508
snapshot: &TypeArchiveSnapshotId,
537509
) -> Array<BnString> {
538510
let id = id.to_cstr();
511+
let snapshot = snapshot.clone().to_cstr();
539512
let mut count = 0;
540513
let result = unsafe {
541514
BNGetTypeArchiveIncomingDirectTypeReferences(
542515
self.handle.as_ptr(),
543516
id.as_ptr(),
544-
snapshot.0.as_ptr() as *const c_char,
517+
snapshot.as_ptr(),
545518
&mut count,
546519
)
547520
};
@@ -566,12 +539,13 @@ impl TypeArchive {
566539
snapshot: &TypeArchiveSnapshotId,
567540
) -> Array<BnString> {
568541
let id = id.to_cstr();
542+
let snapshot = snapshot.clone().to_cstr();
569543
let mut count = 0;
570544
let result = unsafe {
571545
BNGetTypeArchiveIncomingRecursiveTypeReferences(
572546
self.handle.as_ptr(),
573547
id.as_ptr(),
574-
snapshot.0.as_ptr() as *const c_char,
548+
snapshot.as_ptr(),
575549
&mut count,
576550
)
577551
};
@@ -605,12 +579,9 @@ impl TypeArchive {
605579

606580
/// Turn a given `snapshot` id into a data stream
607581
pub fn serialize_snapshot(&self, snapshot: &TypeArchiveSnapshotId) -> DataBuffer {
608-
let result = unsafe {
609-
BNTypeArchiveSerializeSnapshot(
610-
self.handle.as_ptr(),
611-
snapshot.0.as_ptr() as *const c_char,
612-
)
613-
};
582+
let snapshot = snapshot.clone().to_cstr();
583+
let result =
584+
unsafe { BNTypeArchiveSerializeSnapshot(self.handle.as_ptr(), snapshot.as_ptr()) };
614585
assert!(!result.is_null());
615586
DataBuffer::from_raw(result)
616587
}
@@ -712,15 +683,14 @@ impl TypeArchive {
712683
fun(&TypeArchiveSnapshotId(id_str))
713684
}
714685

715-
// SAFETY TypeArchiveSnapshotId and `*const c_char` are transparent
716-
let parents_raw = parents.as_ptr() as *const *const c_char;
717-
686+
let parents_cstr: Vec<_> = parents.iter().map(|p| p.clone().to_cstr()).collect();
687+
let parents_raw: Vec<_> = parents_cstr.iter().map(|p| p.as_ptr()).collect();
718688
let result = unsafe {
719689
BNTypeArchiveNewSnapshotTransaction(
720690
self.handle.as_ptr(),
721691
Some(cb_callback::<F>),
722692
&mut function as *mut F as *mut c_void,
723-
parents_raw,
693+
parents_raw.as_ptr(),
724694
parents.len(),
725695
)
726696
};

0 commit comments

Comments
 (0)