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/collaboration.rs | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'rust/src/collaboration.rs') diff --git a/rust/src/collaboration.rs b/rust/src/collaboration.rs index c9067762..1f76dc9a 100644 --- a/rust/src/collaboration.rs +++ b/rust/src/collaboration.rs @@ -30,7 +30,7 @@ pub use user::*; use binaryninjacore_sys::*; use crate::rc::{Array, Ref}; -use crate::string::{BnStrCompatible, BnString}; +use crate::string::{AsCStr, BnString}; // TODO: Should we pull metadata and information required to call a function? Or should we add documentation // TODO: on what functions need to have been called prior? I feel like we should make the user have to pull @@ -73,23 +73,23 @@ pub fn known_remotes() -> Array { } /// Get Remote by unique `id` -pub fn get_remote_by_id(id: S) -> Option> { - let id = id.into_bytes_with_nul(); +pub fn get_remote_by_id(id: S) -> Option> { + let id = id.to_cstr(); let value = unsafe { BNCollaborationGetRemoteById(id.as_ref().as_ptr() as *const c_char) }; NonNull::new(value).map(|h| unsafe { Remote::ref_from_raw(h) }) } /// Get Remote by `address` -pub fn get_remote_by_address(address: S) -> Option> { - let address = address.into_bytes_with_nul(); +pub fn get_remote_by_address(address: S) -> Option> { + let address = address.to_cstr(); let value = unsafe { BNCollaborationGetRemoteByAddress(address.as_ref().as_ptr() as *const c_char) }; NonNull::new(value).map(|h| unsafe { Remote::ref_from_raw(h) }) } /// Get Remote by `name` -pub fn get_remote_by_name(name: S) -> Option> { - let name = name.into_bytes_with_nul(); +pub fn get_remote_by_name(name: S) -> Option> { + let name = name.to_cstr(); let value = unsafe { BNCollaborationGetRemoteByName(name.as_ref().as_ptr() as *const c_char) }; NonNull::new(value).map(|h| unsafe { Remote::ref_from_raw(h) }) } @@ -106,15 +106,15 @@ pub fn save_remotes() { pub fn store_data_in_keychain(key: K, data: I) -> bool where - K: BnStrCompatible, + K: AsCStr, I: IntoIterator, - DK: BnStrCompatible, - DV: BnStrCompatible, + DK: AsCStr, + DV: AsCStr, { - let key = key.into_bytes_with_nul(); + let key = key.to_cstr(); let (data_keys, data_values): (Vec, Vec) = data .into_iter() - .map(|(k, v)| (k.into_bytes_with_nul(), v.into_bytes_with_nul())) + .map(|(k, v)| (k.to_cstr(), v.to_cstr())) .unzip(); let data_keys_ptr: Box<[*const c_char]> = data_keys .iter() @@ -134,15 +134,13 @@ where } } -pub fn has_data_in_keychain(key: K) -> bool { - let key = key.into_bytes_with_nul(); +pub fn has_data_in_keychain(key: K) -> bool { + let key = key.to_cstr(); unsafe { BNCollaborationHasDataInKeychain(key.as_ref().as_ptr() as *const c_char) } } -pub fn get_data_from_keychain( - key: K, -) -> Option<(Array, Array)> { - let key = key.into_bytes_with_nul(); +pub fn get_data_from_keychain(key: K) -> Option<(Array, Array)> { + let key = key.to_cstr(); let mut keys = std::ptr::null_mut(); let mut values = std::ptr::null_mut(); let count = unsafe { @@ -157,7 +155,7 @@ pub fn get_data_from_keychain( keys.zip(values) } -pub fn delete_data_from_keychain(key: K) -> bool { - let key = key.into_bytes_with_nul(); +pub fn delete_data_from_keychain(key: K) -> bool { + let key = key.to_cstr(); unsafe { BNCollaborationDeleteDataFromKeychain(key.as_ref().as_ptr() as *const c_char) } } -- cgit v1.3.1