diff options
| author | Mason Reed <mason@vector35.com> | 2025-05-07 19:22:21 -0400 |
|---|---|---|
| committer | Mason Reed <35282038+emesare@users.noreply.github.com> | 2025-05-12 17:45:24 -0400 |
| commit | 2f214f6c9935e8ce8df4732cde44a540a003258c (patch) | |
| tree | 6fe319433ef0d2ad75fcc58a50eaa632bb627ec9 /rust/src/lib.rs | |
| parent | b4cf0be8816182c9efca037e27e9439482f8bf36 (diff) | |
[Rust] Reduce usage of `IntoCStr` in function signatures
This is being done to reduce complexity in function signatures, specifically many of the strings we are passing ultimately should be new types themselves instead of "just strings", things such as type ids.
Another place which was confusing was dealing with filesystem related APIs, this commit turns most of those params into a stricter `Path` type.
This is bringing the rust api more inline with both python and C++, where the wrapper eagerly converts the string into the languages standard string type.
Special consideration must be made for symbols or other possible non utf-8 objects.
This commit will be followed up with one that adds the `IntoCStr` bound on API's we want to keep as invalid utf-8 so we can for example, get section by name on a section with invalid utf-8.
Diffstat (limited to 'rust/src/lib.rs')
| -rw-r--r-- | rust/src/lib.rs | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 8d8fae13..7056e529 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -469,11 +469,18 @@ impl VersionInfo { pub(crate) fn free_raw(value: BNVersionInfo) { unsafe { BnString::free_raw(value.channel) }; } +} + +impl TryFrom<&str> for VersionInfo { + type Error = (); - pub fn from_string<S: IntoCStr>(string: S) -> Self { - let string = string.to_cstr(); + fn try_from(value: &str) -> Result<Self, Self::Error> { + let string = value.to_cstr(); let result = unsafe { BNParseVersionString(string.as_ptr()) }; - Self::from_owned_raw(result) + if result.build == 0 && result.channel.is_null() && result.major == 0 && result.minor == 0 { + return Err(()); + } + Ok(Self::from_owned_raw(result)) } } @@ -529,13 +536,13 @@ pub fn license_count() -> i32 { /// 1. Check the BN_LICENSE environment variable /// 2. Check the Binary Ninja user directory for license.dat #[cfg(not(feature = "demo"))] -pub fn set_license<S: IntoCStr + Default>(license: Option<S>) { +pub fn set_license(license: Option<&str>) { let license = license.unwrap_or_default().to_cstr(); unsafe { BNSetLicense(license.as_ptr()) } } #[cfg(feature = "demo")] -pub fn set_license<S: IntoCStr + Default>(_license: Option<S>) {} +pub fn set_license(_license: Option<&str>) {} pub fn product() -> String { unsafe { BnString::into_string(BNGetProduct()) } @@ -554,8 +561,8 @@ pub fn is_ui_enabled() -> bool { unsafe { BNIsUIEnabled() } } -pub fn is_database<S: IntoCStr>(filename: S) -> bool { - let filename = filename.to_cstr(); +pub fn is_database(file: &Path) -> bool { + let filename = file.to_cstr(); unsafe { BNIsDatabase(filename.as_ptr()) } } @@ -583,12 +590,12 @@ pub fn plugin_ui_abi_minimum_version() -> u32 { BN_MINIMUM_UI_ABI_VERSION } -pub fn add_required_plugin_dependency<S: IntoCStr>(name: S) { +pub fn add_required_plugin_dependency(name: &str) { let raw_name = name.to_cstr(); unsafe { BNAddRequiredPluginDependency(raw_name.as_ptr()) }; } -pub fn add_optional_plugin_dependency<S: IntoCStr>(name: S) { +pub fn add_optional_plugin_dependency(name: &str) { let raw_name = name.to_cstr(); unsafe { BNAddOptionalPluginDependency(raw_name.as_ptr()) }; } |
