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/project/file.rs | 22 +++------------------- rust/src/project/folder.rs | 18 ++++-------------- 2 files changed, 7 insertions(+), 33 deletions(-) (limited to 'rust/src/project') diff --git a/rust/src/project/file.rs b/rust/src/project/file.rs index d1724ab0..bc486ed9 100644 --- a/rust/src/project/file.rs +++ b/rust/src/project/file.rs @@ -8,7 +8,6 @@ use binaryninjacore_sys::{ BNProjectFileGetPathOnDisk, BNProjectFileGetProject, BNProjectFileSetDescription, BNProjectFileSetFolder, BNProjectFileSetName, }; -use std::ffi::c_char; use std::fmt::Debug; use std::ptr::{null_mut, NonNull}; use std::time::SystemTime; @@ -59,12 +58,7 @@ impl ProjectFile { /// Set the name of this file pub fn set_name(&self, value: S) -> bool { let value_raw = value.to_cstr(); - unsafe { - BNProjectFileSetName( - self.handle.as_ptr(), - value_raw.as_ref().as_ptr() as *const c_char, - ) - } + unsafe { BNProjectFileSetName(self.handle.as_ptr(), value_raw.as_ptr()) } } /// Get the description of this file @@ -75,12 +69,7 @@ impl ProjectFile { /// Set the description of this file pub fn set_description(&self, value: S) -> bool { let value_raw = value.to_cstr(); - unsafe { - BNProjectFileSetDescription( - self.handle.as_ptr(), - value_raw.as_ref().as_ptr() as *const c_char, - ) - } + unsafe { BNProjectFileSetDescription(self.handle.as_ptr(), value_raw.as_ptr()) } } /// Get the file creation time @@ -106,12 +95,7 @@ impl ProjectFile { /// * `dest` - Destination path for the exported contents pub fn export(&self, dest: S) -> bool { let dest_raw = dest.to_cstr(); - unsafe { - BNProjectFileExport( - self.handle.as_ptr(), - dest_raw.as_ref().as_ptr() as *const c_char, - ) - } + unsafe { BNProjectFileExport(self.handle.as_ptr(), dest_raw.as_ptr()) } } } diff --git a/rust/src/project/folder.rs b/rust/src/project/folder.rs index b8881ddc..80416197 100644 --- a/rust/src/project/folder.rs +++ b/rust/src/project/folder.rs @@ -8,7 +8,7 @@ use binaryninjacore_sys::{ BNProjectFolderGetName, BNProjectFolderGetParent, BNProjectFolderGetProject, BNProjectFolderSetDescription, BNProjectFolderSetName, BNProjectFolderSetParent, }; -use std::ffi::{c_char, c_void}; +use std::ffi::c_void; use std::fmt::Debug; use std::ptr::{null_mut, NonNull}; @@ -48,12 +48,7 @@ impl ProjectFolder { /// Set the name of this folder pub fn set_name(&self, value: S) -> bool { let value_raw = value.to_cstr(); - unsafe { - BNProjectFolderSetName( - self.handle.as_ptr(), - value_raw.as_ref().as_ptr() as *const c_char, - ) - } + unsafe { BNProjectFolderSetName(self.handle.as_ptr(), value_raw.as_ptr()) } } /// Get the description of this folder @@ -64,12 +59,7 @@ impl ProjectFolder { /// Set the description of this folder pub fn set_description(&self, value: S) -> bool { let value_raw = value.to_cstr(); - unsafe { - BNProjectFolderSetDescription( - self.handle.as_ptr(), - value_raw.as_ref().as_ptr() as *const c_char, - ) - } + unsafe { BNProjectFolderSetDescription(self.handle.as_ptr(), value_raw.as_ptr()) } } /// Get the folder that contains this folder @@ -107,7 +97,7 @@ impl ProjectFolder { let success = unsafe { BNProjectFolderExport( self.handle.as_ptr(), - dest_raw.as_ref().as_ptr() as *const c_char, + dest_raw.as_ptr(), &mut progress as *mut P as *mut c_void, Some(P::cb_progress_callback), ) -- cgit v1.3.1