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/websocket/client.rs | 16 ++++++++-------- rust/src/websocket/provider.rs | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'rust/src/websocket') diff --git a/rust/src/websocket/client.rs b/rust/src/websocket/client.rs index 36c7bedd..43e1e1f7 100644 --- a/rust/src/websocket/client.rs +++ b/rust/src/websocket/client.rs @@ -1,5 +1,5 @@ use crate::rc::{Ref, RefCountable}; -use crate::string::{BnStrCompatible, BnString}; +use crate::string::{AsCStr, BnString}; use binaryninjacore_sys::*; use std::ffi::{c_char, c_void, CStr}; use std::ptr::NonNull; @@ -21,8 +21,8 @@ pub trait WebsocketClient: Sync + Send { fn connect(&self, host: &str, headers: I) -> bool where I: IntoIterator, - K: BnStrCompatible, - V: BnStrCompatible; + K: AsCStr, + V: AsCStr; fn write(&self, data: &[u8]) -> bool; @@ -77,14 +77,14 @@ impl CoreWebsocketClient { ) -> bool where I: IntoIterator, - K: BnStrCompatible, - V: BnStrCompatible, + K: AsCStr, + V: AsCStr, C: WebsocketClientCallback, { - let url = host.into_bytes_with_nul(); + let url = host.to_cstr(); let (header_keys, header_values): (Vec, Vec) = headers .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 header_keys: Vec<*const c_char> = header_keys .iter() @@ -129,7 +129,7 @@ impl CoreWebsocketClient { /// Call the error callback function pub fn notify_error(&self, msg: &str) { - let error = msg.into_bytes_with_nul(); + let error = msg.to_cstr(); unsafe { BNNotifyWebsocketClientError(self.handle.as_ptr(), error.as_ptr() as *const c_char) } diff --git a/rust/src/websocket/provider.rs b/rust/src/websocket/provider.rs index 2d01fbc3..48c198d2 100644 --- a/rust/src/websocket/provider.rs +++ b/rust/src/websocket/provider.rs @@ -1,5 +1,5 @@ use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Ref}; -use crate::string::{BnStrCompatible, BnString}; +use crate::string::{AsCStr, BnString}; use crate::websocket::client; use crate::websocket::client::{CoreWebsocketClient, WebsocketClient}; use binaryninjacore_sys::*; @@ -11,7 +11,7 @@ pub fn register_websocket_provider(name: &str) -> &'static mut W where W: WebsocketProvider, { - let name = name.into_bytes_with_nul(); + let name = name.to_cstr(); let provider_uninit = MaybeUninit::uninit(); // SAFETY: Websocket provider is never freed let leaked_provider = Box::leak(Box::new(provider_uninit)); @@ -80,8 +80,8 @@ impl CoreWebsocketProvider { unsafe { Array::new(result, count, ()) } } - pub fn by_name(name: S) -> Option { - let name = name.into_bytes_with_nul(); + pub fn by_name(name: S) -> Option { + let name = name.to_cstr(); let result = unsafe { BNGetWebsocketProviderByName(name.as_ref().as_ptr() as *const c_char) }; NonNull::new(result).map(|h| unsafe { Self::from_raw(h) }) -- cgit v1.3.1