From a826c589dfc10c542deba7ca3343a462e02d6bde Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Sun, 4 May 2025 19:10:56 -0400 Subject: [Rust] Simplify `BnStrCompatible` trait Followup to https://github.com/Vector35/binaryninja-api/pull/5897/ This simplifies usage of the trait in user code, should just be able to `to_cstr` to get the cstr repr and then call `as_ptr`. Co-authored-by: Michael Krasnitski --- rust/src/file_metadata.rs | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'rust/src/file_metadata.rs') diff --git a/rust/src/file_metadata.rs b/rust/src/file_metadata.rs index c14aaf29..6b6a79a8 100644 --- a/rust/src/file_metadata.rs +++ b/rust/src/file_metadata.rs @@ -52,7 +52,7 @@ impl FileMetadata { Self::ref_from_raw(unsafe { BNCreateFileMetadata() }) } - pub fn with_filename(name: S) -> Ref { + pub fn with_filename(name: S) -> Ref { let ret = FileMetadata::new(); ret.set_filename(name); ret @@ -75,8 +75,8 @@ impl FileMetadata { } } - pub fn set_filename(&self, name: S) { - let name = name.into_bytes_with_nul(); + pub fn set_filename(&self, name: S) { + let name = name.to_cstr(); unsafe { BNSetFilename(self.handle, name.as_ref().as_ptr() as *mut _); @@ -107,8 +107,8 @@ impl FileMetadata { self.is_database_backed_for_view_type("") } - pub fn is_database_backed_for_view_type(&self, view_type: S) -> bool { - let view_type = view_type.into_bytes_with_nul(); + pub fn is_database_backed_for_view_type(&self, view_type: S) -> bool { + let view_type = view_type.to_cstr(); unsafe { BNIsBackedByDatabase(self.handle, view_type.as_ref().as_ptr() as *const _) } } @@ -135,15 +135,15 @@ impl FileMetadata { unsafe { BnString::into_string(BNBeginUndoActions(self.handle, anonymous_allowed)) } } - pub fn commit_undo_actions(&self, id: S) { - let id = id.into_bytes_with_nul(); + pub fn commit_undo_actions(&self, id: S) { + let id = id.to_cstr(); unsafe { BNCommitUndoActions(self.handle, id.as_ref().as_ptr() as *const _); } } - pub fn revert_undo_actions(&self, id: S) { - let id = id.into_bytes_with_nul(); + pub fn revert_undo_actions(&self, id: S) { + let id = id.to_cstr(); unsafe { BNRevertUndoActions(self.handle, id.as_ref().as_ptr() as *const _); } @@ -169,8 +169,8 @@ impl FileMetadata { unsafe { BNGetCurrentOffset(self.handle) } } - pub fn navigate_to(&self, view: S, offset: u64) -> Result<(), ()> { - let view = view.into_bytes_with_nul(); + pub fn navigate_to(&self, view: S, offset: u64) -> Result<(), ()> { + let view = view.to_cstr(); unsafe { if BNNavigate(self.handle, view.as_ref().as_ptr() as *const _, offset) { @@ -181,8 +181,8 @@ impl FileMetadata { } } - pub fn view_of_type(&self, view: S) -> Option> { - let view = view.into_bytes_with_nul(); + pub fn view_of_type(&self, view: S) -> Option> { + let view = view.to_cstr(); unsafe { let raw_view_ptr = BNGetFileViewOfType(self.handle, view.as_ref().as_ptr() as *const _); @@ -215,7 +215,7 @@ impl FileMetadata { return false; }; - let file_path = file_path.as_ref().into_bytes_with_nul(); + let file_path = file_path.as_ref().to_cstr(); unsafe { BNCreateDatabase( raw_view.handle, @@ -226,7 +226,7 @@ impl FileMetadata { } // TODO: Pass settings? - pub fn create_database_with_progress( + pub fn create_database_with_progress( &self, file_path: impl AsRef, mut progress: P, @@ -235,7 +235,7 @@ impl FileMetadata { let Some(raw_view) = self.view_of_type("Raw") else { return false; }; - let file_path = file_path.as_ref().into_bytes_with_nul(); + let file_path = file_path.as_ref().to_cstr(); unsafe { BNCreateDatabaseWithProgress( raw_view.handle, @@ -256,11 +256,11 @@ impl FileMetadata { unsafe { BNSaveAutoSnapshot(raw_view.handle, ptr::null_mut() as *mut _) } } - pub fn open_database_for_configuration( + pub fn open_database_for_configuration( &self, filename: S, ) -> Result, ()> { - let filename = filename.into_bytes_with_nul(); + let filename = filename.to_cstr(); unsafe { let bv = BNOpenDatabaseForConfiguration(self.handle, filename.as_ref().as_ptr() as *const _); @@ -273,8 +273,8 @@ impl FileMetadata { } } - pub fn open_database(&self, filename: S) -> Result, ()> { - let filename = filename.into_bytes_with_nul(); + pub fn open_database(&self, filename: S) -> Result, ()> { + let filename = filename.to_cstr(); let filename_ptr = filename.as_ref().as_ptr() as *mut _; let view = unsafe { BNOpenExistingDatabase(self.handle, filename_ptr) }; @@ -286,12 +286,12 @@ impl FileMetadata { } } - pub fn open_database_with_progress( + pub fn open_database_with_progress( &self, filename: S, mut progress: P, ) -> Result, ()> { - let filename = filename.into_bytes_with_nul(); + let filename = filename.to_cstr(); let filename_ptr = filename.as_ref().as_ptr() as *mut _; let view = unsafe { -- cgit v1.3.1