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/download_provider.rs | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'rust/src/download_provider.rs') diff --git a/rust/src/download_provider.rs b/rust/src/download_provider.rs index 4b7b3c0c..f9e21e64 100644 --- a/rust/src/download_provider.rs +++ b/rust/src/download_provider.rs @@ -1,6 +1,6 @@ use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable}; use crate::settings::Settings; -use crate::string::{BnStrCompatible, BnString}; +use crate::string::{AsCStr, BnString}; use binaryninjacore_sys::*; use std::collections::HashMap; use std::ffi::{c_void, CStr}; @@ -13,11 +13,9 @@ pub struct DownloadProvider { } impl DownloadProvider { - pub fn get(name: S) -> Option { + pub fn get(name: S) -> Option { let result = unsafe { - BNGetDownloadProviderByName( - name.into_bytes_with_nul().as_ref().as_ptr() as *const c_char - ) + BNGetDownloadProviderByName(name.to_cstr().as_ref().as_ptr() as *const c_char) }; if result.is_null() { return None; @@ -134,7 +132,7 @@ impl DownloadInstance { } } - pub fn perform_request( + pub fn perform_request( &mut self, url: S, callbacks: DownloadInstanceOutputCallbacks, @@ -150,7 +148,7 @@ impl DownloadInstance { let result = unsafe { BNPerformDownloadRequest( self.handle, - url.into_bytes_with_nul().as_ref().as_ptr() as *const c_char, + url.to_cstr().as_ref().as_ptr() as *const c_char, &mut cbs as *mut BNDownloadInstanceOutputCallbacks, ) }; @@ -204,10 +202,10 @@ impl DownloadInstance { } pub fn perform_custom_request< - M: BnStrCompatible, - U: BnStrCompatible, - HK: BnStrCompatible, - HV: BnStrCompatible, + M: AsCStr, + U: AsCStr, + HK: AsCStr, + HV: AsCStr, I: IntoIterator, >( &mut self, @@ -219,8 +217,8 @@ impl DownloadInstance { let mut header_keys = vec![]; let mut header_values = vec![]; for (key, value) in headers { - header_keys.push(key.into_bytes_with_nul()); - header_values.push(value.into_bytes_with_nul()); + header_keys.push(key.to_cstr()); + header_values.push(value.to_cstr()); } let mut header_key_ptrs = vec![]; @@ -246,8 +244,8 @@ impl DownloadInstance { let result = unsafe { BNPerformCustomRequest( self.handle, - method.into_bytes_with_nul().as_ref().as_ptr() as *const c_char, - url.into_bytes_with_nul().as_ref().as_ptr() as *const c_char, + method.to_cstr().as_ref().as_ptr() as *const c_char, + url.to_cstr().as_ref().as_ptr() as *const c_char, header_key_ptrs.len() as u64, header_key_ptrs.as_ptr(), header_value_ptrs.as_ptr(), -- cgit v1.3.1