From 788a8b7091bbdde77817030e0836d7a7a786fd99 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Sun, 4 May 2025 19:47:55 -0400 Subject: [Rust] Simplify usage surrounding c strings `cstring.as_ref().as_ptr() as *const c_char` -> `cstring.as_ptr()` `cstring.as_ref().as_ptr() as *mut _` -> `cstring.as_ptr()` `cstring.as_ptr() as *const c_char` -> `cstring.as_ptr()` With a few fixes for cstrings that might be dropped prematurely. --- rust/src/lib.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'rust/src/lib.rs') diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 63872be3..c0b3a38c 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -285,7 +285,7 @@ pub fn bundled_plugin_directory() -> Result { pub fn set_bundled_plugin_directory(new_dir: impl AsRef) { let new_dir = new_dir.as_ref().to_cstr(); - unsafe { BNSetBundledPluginDirectory(new_dir.as_ptr() as *const c_char) }; + unsafe { BNSetBundledPluginDirectory(new_dir.as_ptr()) }; } pub fn user_directory() -> PathBuf { @@ -330,7 +330,7 @@ pub fn save_last_run() { pub fn path_relative_to_bundled_plugin_directory(path: impl AsRef) -> Result { let path_raw = path.as_ref().to_cstr(); let s: *mut c_char = - unsafe { BNGetPathRelativeToBundledPluginDirectory(path_raw.as_ptr() as *const c_char) }; + unsafe { BNGetPathRelativeToBundledPluginDirectory(path_raw.as_ptr()) }; if s.is_null() { return Err(()); } @@ -340,7 +340,7 @@ pub fn path_relative_to_bundled_plugin_directory(path: impl AsRef) -> Resu pub fn path_relative_to_user_plugin_directory(path: impl AsRef) -> Result { let path_raw = path.as_ref().to_cstr(); let s: *mut c_char = - unsafe { BNGetPathRelativeToUserPluginDirectory(path_raw.as_ptr() as *const c_char) }; + unsafe { BNGetPathRelativeToUserPluginDirectory(path_raw.as_ptr()) }; if s.is_null() { return Err(()); } @@ -350,7 +350,7 @@ pub fn path_relative_to_user_plugin_directory(path: impl AsRef) -> Result< pub fn path_relative_to_user_directory(path: impl AsRef) -> Result { let path_raw = path.as_ref().to_cstr(); let s: *mut c_char = - unsafe { BNGetPathRelativeToUserDirectory(path_raw.as_ptr() as *const c_char) }; + unsafe { BNGetPathRelativeToUserDirectory(path_raw.as_ptr()) }; if s.is_null() { return Err(()); } @@ -475,7 +475,7 @@ impl VersionInfo { pub fn from_string(string: S) -> Self { let string = string.to_cstr(); - let result = unsafe { BNParseVersionString(string.as_ref().as_ptr() as *const c_char) }; + let result = unsafe { BNParseVersionString(string.as_ptr()) }; Self::from_owned_raw(result) } } @@ -534,8 +534,7 @@ pub fn license_count() -> i32 { #[cfg(not(feature = "demo"))] pub fn set_license(license: Option) { let license = license.unwrap_or_default().to_cstr(); - let license_slice = license.as_ref(); - unsafe { BNSetLicense(license_slice.as_ptr() as *const c_char) } + unsafe { BNSetLicense(license.as_ptr()) } } #[cfg(feature = "demo")] @@ -560,8 +559,7 @@ pub fn is_ui_enabled() -> bool { pub fn is_database(filename: S) -> bool { let filename = filename.to_cstr(); - let filename_slice = filename.as_ref(); - unsafe { BNIsDatabase(filename_slice.as_ptr() as *const c_char) } + unsafe { BNIsDatabase(filename.as_ptr()) } } pub fn plugin_abi_version() -> u32 { @@ -589,11 +587,13 @@ pub fn plugin_ui_abi_minimum_version() -> u32 { } pub fn add_required_plugin_dependency(name: S) { - unsafe { BNAddRequiredPluginDependency(name.to_cstr().as_ref().as_ptr() as *const c_char) }; + let raw_name = name.to_cstr(); + unsafe { BNAddRequiredPluginDependency(raw_name.as_ptr()) }; } pub fn add_optional_plugin_dependency(name: S) { - unsafe { BNAddOptionalPluginDependency(name.to_cstr().as_ref().as_ptr() as *const c_char) }; + let raw_name = name.to_cstr(); + unsafe { BNAddOptionalPluginDependency(raw_name.as_ptr()) }; } // Provide ABI version automatically so that the core can verify binary compatibility -- cgit v1.3.1