Skip to content

Commit 263d042

Browse files
committed
More Rust API cleanup
1 parent 05293c3 commit 263d042

File tree

14 files changed

+75
-63
lines changed

14 files changed

+75
-63
lines changed

rust/src/architecture.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,7 @@ impl Architecture for CoreArchitecture {
18101810
Ok(result) => result,
18111811
Err(_) => return Err("Result buffer allocation failed".to_string()),
18121812
};
1813+
// TODO: This is actually a list of errors.
18131814
let mut error_raw: *mut c_char = std::ptr::null_mut();
18141815
let res = unsafe {
18151816
BNAssemble(

rust/src/debuginfo.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -807,15 +807,8 @@ impl DebugInfo {
807807
}
808808

809809
for local_variable in &new_func.local_variables {
810-
local_variables_array.push(BNVariableNameAndType {
811-
var: local_variable.variable.into(),
812-
autoDefined: local_variable.auto_defined,
813-
typeConfidence: local_variable.ty.confidence,
814-
name: BNAllocString(
815-
local_variable.name.clone().into_bytes_with_nul().as_ptr() as _
816-
),
817-
type_: local_variable.ty.contents.handle,
818-
});
810+
// NOTE: must be manually freed after call to BNAddDebugFunction is over.
811+
local_variables_array.push(NamedVariableWithType::into_raw(local_variable.clone()));
819812
}
820813

821814
let result = BNAddDebugFunction(
@@ -841,11 +834,11 @@ impl DebugInfo {
841834
);
842835

843836
for i in components_array {
844-
BNFreeString(i);
837+
BnString::free_raw(i);
845838
}
846839

847840
for i in &local_variables_array {
848-
BNFreeString(i.name);
841+
NamedVariableWithType::free_raw(*i);
849842
}
850843
result
851844
}

rust/src/function.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use crate::variable::{
4949
use crate::workflow::Workflow;
5050
use std::fmt::{Debug, Formatter};
5151
use std::ptr::NonNull;
52-
use std::time::Duration;
52+
use std::time::{Duration, UNIX_EPOCH};
5353
use std::{ffi::c_char, hash::Hash, ops::Range};
5454

5555
/// Used to describe a location within a [`Function`].
@@ -259,7 +259,7 @@ impl FunctionViewType {
259259
}
260260

261261
pub(crate) fn free_raw(value: BNFunctionViewType) {
262-
let _ = unsafe { BnString::from_raw(value.name as *mut _) };
262+
unsafe { BnString::free_raw(value.name as *mut _) };
263263
}
264264
}
265265

@@ -2511,23 +2511,38 @@ pub struct PerformanceInfo {
25112511
pub seconds: Duration,
25122512
}
25132513

2514-
impl From<BNPerformanceInfo> for PerformanceInfo {
2515-
fn from(value: BNPerformanceInfo) -> Self {
2514+
impl PerformanceInfo {
2515+
pub fn new(name: String, seconds: Duration) -> Self {
25162516
Self {
2517-
name: unsafe { BnString::from_raw(value.name) }.to_string(),
2518-
seconds: Duration::from_secs_f64(value.seconds),
2517+
name: name.to_string(),
2518+
seconds,
25192519
}
25202520
}
2521-
}
25222521

2523-
impl From<&BNPerformanceInfo> for PerformanceInfo {
2524-
fn from(value: &BNPerformanceInfo) -> Self {
2522+
pub(crate) fn from_raw(value: &BNPerformanceInfo) -> Self {
25252523
Self {
2526-
// TODO: Name will be freed by this. FIX!
2527-
name: unsafe { BnString::from_raw(value.name) }.to_string(),
2524+
name: raw_to_string(value.name as *mut _).unwrap(),
25282525
seconds: Duration::from_secs_f64(value.seconds),
25292526
}
25302527
}
2528+
2529+
pub(crate) fn from_owned_raw(value: BNPerformanceInfo) -> Self {
2530+
let owned = Self::from_raw(&value);
2531+
Self::free_raw(value);
2532+
owned
2533+
}
2534+
2535+
pub(crate) fn into_raw(value: Self) -> BNPerformanceInfo {
2536+
let bn_name = BnString::new(value.name);
2537+
BNPerformanceInfo {
2538+
name: BnString::into_raw(bn_name),
2539+
seconds: value.seconds.as_secs_f64(),
2540+
}
2541+
}
2542+
2543+
pub(crate) fn free_raw(value: BNPerformanceInfo) {
2544+
unsafe { BnString::free_raw(value.name) };
2545+
}
25312546
}
25322547

25332548
impl CoreArrayProvider for PerformanceInfo {
@@ -2542,8 +2557,7 @@ unsafe impl CoreArrayProviderInner for PerformanceInfo {
25422557
}
25432558

25442559
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
2545-
// TODO: Swap this to the ref version.
2546-
Self::from(*raw)
2560+
Self::from_raw(raw)
25472561
}
25482562
}
25492563

rust/src/interaction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ pub fn get_text_line_input(prompt: &str, title: &str) -> Option<String> {
3636
if !result {
3737
return None;
3838
}
39-
40-
Some(unsafe { BnString::from_raw(value).to_string() })
39+
40+
Some(unsafe { BnString::into_string(value) })
4141
}
4242

4343
pub fn get_integer_input(prompt: &str, title: &str) -> Option<i64> {

rust/src/lib.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,16 @@ where
279279
pub fn install_directory() -> PathBuf {
280280
let install_dir_ptr: *mut c_char = unsafe { BNGetInstallDirectory() };
281281
assert!(!install_dir_ptr.is_null());
282-
let bn_install_dir = unsafe { BnString::from_raw(install_dir_ptr) };
283-
PathBuf::from(bn_install_dir.to_string())
282+
let install_dir_str = unsafe { BnString::into_string(install_dir_ptr) };
283+
PathBuf::from(install_dir_str)
284284
}
285285

286286
pub fn bundled_plugin_directory() -> Result<PathBuf, ()> {
287287
let s: *mut c_char = unsafe { BNGetBundledPluginDirectory() };
288288
if s.is_null() {
289289
return Err(());
290290
}
291-
Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string()))
291+
Ok(PathBuf::from(unsafe { BnString::into_string(s) }))
292292
}
293293

294294
pub fn set_bundled_plugin_directory(new_dir: impl AsRef<Path>) {
@@ -299,31 +299,33 @@ pub fn set_bundled_plugin_directory(new_dir: impl AsRef<Path>) {
299299
pub fn user_directory() -> PathBuf {
300300
let user_dir_ptr: *mut c_char = unsafe { BNGetUserDirectory() };
301301
assert!(!user_dir_ptr.is_null());
302-
let bn_user_dir = unsafe { BnString::from_raw(user_dir_ptr) };
303-
PathBuf::from(bn_user_dir.to_string())
302+
let user_dir_str = unsafe { BnString::into_string(user_dir_ptr) };
303+
PathBuf::from(user_dir_str)
304304
}
305305

306306
pub fn user_plugin_directory() -> Result<PathBuf, ()> {
307307
let s: *mut c_char = unsafe { BNGetUserPluginDirectory() };
308308
if s.is_null() {
309309
return Err(());
310310
}
311-
Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string()))
311+
let user_plugin_dir_str = unsafe { BnString::into_string(s) };
312+
Ok(PathBuf::from(user_plugin_dir_str))
312313
}
313314

314315
pub fn repositories_directory() -> Result<PathBuf, ()> {
315316
let s: *mut c_char = unsafe { BNGetRepositoriesDirectory() };
316317
if s.is_null() {
317318
return Err(());
318319
}
319-
Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string()))
320+
let repo_dir_str = unsafe { BnString::into_string(s) };
321+
Ok(PathBuf::from(repo_dir_str))
320322
}
321323

322-
pub fn settings_file_name() -> PathBuf {
324+
pub fn settings_file_path() -> PathBuf {
323325
let settings_file_name_ptr: *mut c_char = unsafe { BNGetSettingsFileName() };
324326
assert!(!settings_file_name_ptr.is_null());
325-
let bn_settings_file_name = unsafe { BnString::from_raw(settings_file_name_ptr) };
326-
PathBuf::from(bn_settings_file_name.to_string())
327+
let settings_file_path_str = unsafe { BnString::into_string(settings_file_name_ptr) };
328+
PathBuf::from(settings_file_path_str)
327329
}
328330

329331
/// Write the installation directory of the currently running core instance to disk.
@@ -340,7 +342,7 @@ pub fn path_relative_to_bundled_plugin_directory(path: impl AsRef<Path>) -> Resu
340342
if s.is_null() {
341343
return Err(());
342344
}
343-
Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string()))
345+
Ok(PathBuf::from(unsafe { BnString::into_string(s) }))
344346
}
345347

346348
pub fn path_relative_to_user_plugin_directory(path: impl AsRef<Path>) -> Result<PathBuf, ()> {
@@ -350,7 +352,7 @@ pub fn path_relative_to_user_plugin_directory(path: impl AsRef<Path>) -> Result<
350352
if s.is_null() {
351353
return Err(());
352354
}
353-
Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string()))
355+
Ok(PathBuf::from(unsafe { BnString::into_string(s) }))
354356
}
355357

356358
pub fn path_relative_to_user_directory(path: impl AsRef<Path>) -> Result<PathBuf, ()> {
@@ -360,7 +362,7 @@ pub fn path_relative_to_user_directory(path: impl AsRef<Path>) -> Result<PathBuf
360362
if s.is_null() {
361363
return Err(());
362364
}
363-
Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string()))
365+
Ok(PathBuf::from(unsafe { BnString::into_string(s) }))
364366
}
365367

366368
/// Returns if the running thread is the "main thread"
@@ -476,7 +478,7 @@ impl VersionInfo {
476478
}
477479

478480
pub(crate) fn free_raw(value: BNVersionInfo) {
479-
let _ = unsafe { BnString::from_raw(value.channel) };
481+
unsafe { BnString::free_raw(value.channel) };
480482
}
481483

482484
pub fn from_string<S: BnStrCompatible>(string: S) -> Self {

rust/src/linear_view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl LinearViewObjectIdentifier {
267267
}
268268

269269
pub fn free_raw(value: BNLinearViewObjectIdentifier) {
270-
let _ = unsafe { BnString::from_raw(value.name) };
270+
unsafe { BnString::free_raw(value.name) };
271271
}
272272
}
273273

rust/src/platform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ impl Platform {
386386
assert!(!error_string.is_null());
387387
Err(TypeParserError::new(
388388
TypeParserErrorSeverity::FatalSeverity,
389-
unsafe { BnString::from_raw(error_string) }.to_string(),
389+
unsafe { BnString::into_string(error_string) },
390390
filename.to_string(),
391391
0,
392392
0,

rust/src/type_archive.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ impl TypeArchive {
112112
let result = unsafe { BNGetTypeArchivePath(self.handle.as_ptr()) };
113113
match result.is_null() {
114114
false => {
115-
let bn_res = unsafe { BnString::from_raw(result) };
116-
Some(PathBuf::from(bn_res.to_string()))
115+
let path_str = unsafe { BnString::into_string(result) };
116+
Some(PathBuf::from(path_str))
117117
}
118118
true => None,
119119
}
@@ -136,7 +136,8 @@ impl TypeArchive {
136136
pub fn current_snapshot_id(&self) -> TypeArchiveSnapshotId {
137137
let result = unsafe { BNGetTypeArchiveCurrentSnapshotId(self.handle.as_ptr()) };
138138
assert!(!result.is_null());
139-
TypeArchiveSnapshotId(unsafe { BnString::from_raw(result) }.to_string())
139+
let id = unsafe { BnString::into_string(result) };
140+
TypeArchiveSnapshotId(id)
140141
}
141142

142143
/// Revert the type archive's current snapshot to the given snapshot
@@ -651,7 +652,8 @@ impl TypeArchive {
651652
let result =
652653
unsafe { BNTypeArchiveDeserializeSnapshot(self.handle.as_ptr(), data.as_raw()) };
653654
assert!(!result.is_null());
654-
TypeArchiveSnapshotId(unsafe { BnString::from_raw(result) }.to_string())
655+
let id = unsafe { BnString::into_string(result) };
656+
TypeArchiveSnapshotId(id)
655657
}
656658

657659
/// Register a notification listener
@@ -1149,21 +1151,21 @@ impl TypeArchiveMergeConflict {
11491151
pub fn base_snapshot_id(&self) -> TypeArchiveSnapshotId {
11501152
let value = unsafe { BNTypeArchiveMergeConflictGetBaseSnapshotId(self.handle.as_ptr()) };
11511153
assert!(!value.is_null());
1152-
let id = unsafe { BnString::from_raw(value) }.to_string();
1154+
let id = unsafe { BnString::into_string(value) };
11531155
TypeArchiveSnapshotId(id)
11541156
}
11551157

11561158
pub fn first_snapshot_id(&self) -> TypeArchiveSnapshotId {
11571159
let value = unsafe { BNTypeArchiveMergeConflictGetFirstSnapshotId(self.handle.as_ptr()) };
11581160
assert!(!value.is_null());
1159-
let id = unsafe { BnString::from_raw(value) }.to_string();
1161+
let id = unsafe { BnString::into_string(value) };
11601162
TypeArchiveSnapshotId(id)
11611163
}
11621164

11631165
pub fn second_snapshot_id(&self) -> TypeArchiveSnapshotId {
11641166
let value = unsafe { BNTypeArchiveMergeConflictGetSecondSnapshotId(self.handle.as_ptr()) };
11651167
assert!(!value.is_null());
1166-
let id = unsafe { BnString::from_raw(value) }.to_string();
1168+
let id = unsafe { BnString::into_string(value) };
11671169
TypeArchiveSnapshotId(id)
11681170
}
11691171

rust/src/type_parser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ impl TypeParserError {
323323
}
324324

325325
pub(crate) fn free_raw(value: BNTypeParserError) {
326-
let _ = unsafe { BnString::from_raw(value.message) };
327-
let _ = unsafe { BnString::from_raw(value.fileName) };
326+
unsafe { BnString::free_raw(value.message) };
327+
unsafe { BnString::free_raw(value.fileName) };
328328
}
329329

330330
pub fn new(
@@ -677,7 +677,7 @@ unsafe extern "C" fn cb_parse_type_string<T: TypeParser>(
677677

678678
unsafe extern "C" fn cb_free_string(_ctxt: *mut c_void, string: *mut c_char) {
679679
// SAFETY: The returned string is just BnString
680-
let _ = BnString::from_raw(string);
680+
BnString::free_raw(string);
681681
}
682682

683683
unsafe extern "C" fn cb_free_result(_ctxt: *mut c_void, result: *mut BNTypeParserResult) {

rust/src/type_printer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ unsafe extern "C" fn cb_print_all_types<T: TypePrinter>(
951951

952952
unsafe extern "C" fn cb_free_string(_ctxt: *mut c_void, string: *mut c_char) {
953953
// SAFETY: The returned string is just BnString
954-
let _ = BnString::from_raw(string);
954+
BnString::free_raw(string);
955955
}
956956

957957
unsafe extern "C" fn cb_free_tokens(

0 commit comments

Comments
 (0)