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/database.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'rust/src/database.rs') diff --git a/rust/src/database.rs b/rust/src/database.rs index 7174ebe8..fd20d173 100644 --- a/rust/src/database.rs +++ b/rust/src/database.rs @@ -15,7 +15,7 @@ use crate::database::snapshot::{Snapshot, SnapshotId}; use crate::file_metadata::FileMetadata; use crate::progress::{NoProgressCallback, ProgressCallback}; use crate::rc::{Array, Ref, RefCountable}; -use crate::string::{BnStrCompatible, BnString}; +use crate::string::{AsCStr, BnString}; pub struct Database { pub(crate) handle: NonNull, @@ -62,7 +62,7 @@ impl Database { unsafe { BNSetDatabaseCurrentSnapshot(self.handle.as_ptr(), id.0) } } - pub fn write_snapshot_data( + pub fn write_snapshot_data( &self, parents: &[SnapshotId], file: &BinaryView, @@ -90,10 +90,10 @@ impl Database { mut progress: P, ) -> SnapshotId where - N: BnStrCompatible, + N: AsCStr, P: ProgressCallback, { - let name_raw = name.into_bytes_with_nul(); + let name_raw = name.to_cstr(); let name_ptr = name_raw.as_ref().as_ptr() as *const c_char; let new_id = unsafe { @@ -133,8 +133,8 @@ impl Database { Err(()) } } - pub fn has_global(&self, key: S) -> bool { - let key_raw = key.into_bytes_with_nul(); + pub fn has_global(&self, key: S) -> bool { + let key_raw = key.to_cstr(); let key_ptr = key_raw.as_ref().as_ptr() as *const c_char; unsafe { BNDatabaseHasGlobal(self.handle.as_ptr(), key_ptr) != 0 } } @@ -156,33 +156,33 @@ impl Database { } /// Get a specific global by key - pub fn read_global(&self, key: S) -> Option { - let key_raw = key.into_bytes_with_nul(); + pub fn read_global(&self, key: S) -> Option { + let key_raw = key.to_cstr(); let key_ptr = key_raw.as_ref().as_ptr() as *const c_char; let result = unsafe { BNReadDatabaseGlobal(self.handle.as_ptr(), key_ptr) }; unsafe { NonNull::new(result).map(|_| BnString::from_raw(result)) } } /// Write a global into the database - pub fn write_global(&self, key: K, value: V) -> bool { - let key_raw = key.into_bytes_with_nul(); + pub fn write_global(&self, key: K, value: V) -> bool { + let key_raw = key.to_cstr(); let key_ptr = key_raw.as_ref().as_ptr() as *const c_char; - let value_raw = value.into_bytes_with_nul(); + let value_raw = value.to_cstr(); let value_ptr = value_raw.as_ref().as_ptr() as *const c_char; unsafe { BNWriteDatabaseGlobal(self.handle.as_ptr(), key_ptr, value_ptr) } } /// Get a specific global by key, as a binary buffer - pub fn read_global_data(&self, key: S) -> Option { - let key_raw = key.into_bytes_with_nul(); + pub fn read_global_data(&self, key: S) -> Option { + let key_raw = key.to_cstr(); let key_ptr = key_raw.as_ref().as_ptr() as *const c_char; let result = unsafe { BNReadDatabaseGlobalData(self.handle.as_ptr(), key_ptr) }; NonNull::new(result).map(|_| DataBuffer::from_raw(result)) } /// Write a binary buffer into a global in the database - pub fn write_global_data(&self, key: K, value: &DataBuffer) -> bool { - let key_raw = key.into_bytes_with_nul(); + pub fn write_global_data(&self, key: K, value: &DataBuffer) -> bool { + let key_raw = key.to_cstr(); let key_ptr = key_raw.as_ref().as_ptr() as *const c_char; unsafe { BNWriteDatabaseGlobalData(self.handle.as_ptr(), key_ptr, value.as_raw()) } } -- cgit v1.3.1