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/sync.rs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'rust/src/collaboration/sync.rs') diff --git a/rust/src/collaboration/sync.rs b/rust/src/collaboration/sync.rs index 6fc85d31..4c112336 100644 --- a/rust/src/collaboration/sync.rs +++ b/rust/src/collaboration/sync.rs @@ -11,7 +11,7 @@ use crate::file_metadata::FileMetadata; use crate::progress::{NoProgressCallback, ProgressCallback}; use crate::project::file::ProjectFile; use crate::rc::Ref; -use crate::string::{raw_to_string, BnStrCompatible, BnString}; +use crate::string::{raw_to_string, AsCStr, BnString}; use crate::type_archive::{TypeArchive, TypeArchiveMergeConflict}; // TODO: PathBuf @@ -43,10 +43,7 @@ pub fn default_file_path(file: &RemoteFile) -> Result { /// /// * `file` - Remote File to download and open /// * `db_path` - File path for saved database -pub fn download_file( - file: &RemoteFile, - db_path: S, -) -> Result, ()> { +pub fn download_file(file: &RemoteFile, db_path: S) -> Result, ()> { download_file_with_progress(file, db_path, NoProgressCallback) } @@ -57,12 +54,12 @@ pub fn download_file( /// * `file` - Remote File to download and open /// * `db_path` - File path for saved database /// * `progress` - Function to call for progress updates -pub fn download_file_with_progress( +pub fn download_file_with_progress( file: &RemoteFile, db_path: S, mut progress: F, ) -> Result, ()> { - let db_path = db_path.into_bytes_with_nul(); + let db_path = db_path.to_cstr(); let result = unsafe { BNCollaborationDownloadFile( file.handle.as_ptr(), @@ -223,7 +220,7 @@ pub fn get_local_snapshot_for_remote( pub fn download_database(file: &RemoteFile, location: S, force: bool) -> Result<(), ()> where - S: BnStrCompatible, + S: AsCStr, { download_database_with_progress(file, location, force, NoProgressCallback) } @@ -235,10 +232,10 @@ pub fn download_database_with_progress( mut progress: F, ) -> Result<(), ()> where - S: BnStrCompatible, + S: AsCStr, F: ProgressCallback, { - let db_path = location.into_bytes_with_nul(); + let db_path = location.to_cstr(); let success = unsafe { BNCollaborationDownloadDatabaseForFile( file.handle.as_ptr(), @@ -478,12 +475,12 @@ pub fn get_snapshot_author( /// * `database` - Parent database /// * `snapshot` - Snapshot to edit /// * `author` - Target author -pub fn set_snapshot_author( +pub fn set_snapshot_author( database: &Database, snapshot: &Snapshot, author: S, ) -> Result<(), ()> { - let author = author.into_bytes_with_nul(); + let author = author.to_cstr(); let success = unsafe { BNCollaborationSetSnapshotAuthor( database.handle.as_ptr(), @@ -653,11 +650,11 @@ pub fn get_remote_file_for_local_type_archive(database: &TypeArchive) -> Option< } /// Get the remote snapshot associated with a local snapshot (if it exists) in a Type Archive -pub fn get_remote_snapshot_from_local_type_archive( +pub fn get_remote_snapshot_from_local_type_archive( type_archive: &TypeArchive, snapshot_id: S, ) -> Option> { - let snapshot_id = snapshot_id.into_bytes_with_nul(); + let snapshot_id = snapshot_id.to_cstr(); let value = unsafe { BNCollaborationGetRemoteSnapshotFromLocalTypeArchive( type_archive.handle.as_ptr(), @@ -682,11 +679,11 @@ pub fn get_local_snapshot_from_remote_type_archive( } /// Test if a snapshot is ignored from the archive -pub fn is_type_archive_snapshot_ignored( +pub fn is_type_archive_snapshot_ignored( type_archive: &TypeArchive, snapshot_id: S, ) -> bool { - let snapshot_id = snapshot_id.into_bytes_with_nul(); + let snapshot_id = snapshot_id.to_cstr(); unsafe { BNCollaborationIsTypeArchiveSnapshotIgnored( type_archive.handle.as_ptr(), @@ -697,7 +694,7 @@ pub fn is_type_archive_snapshot_ignored( /// Download a type archive from its remote, saving all snapshots to an archive in the /// specified `location`. Returns a [`TypeArchive`] for using later. -pub fn download_type_archive( +pub fn download_type_archive( file: &RemoteFile, location: S, ) -> Result>, ()> { @@ -706,13 +703,13 @@ pub fn download_type_archive( /// Download a type archive from its remote, saving all snapshots to an archive in the /// specified `location`. Returns a [`TypeArchive`] for using later. -pub fn download_type_archive_with_progress( +pub fn download_type_archive_with_progress( file: &RemoteFile, location: S, mut progress: F, ) -> Result>, ()> { let mut value = std::ptr::null_mut(); - let db_path = location.into_bytes_with_nul(); + let db_path = location.to_cstr(); let success = unsafe { BNCollaborationDownloadTypeArchive( file.handle.as_ptr(), -- cgit v1.3.1