From 2f214f6c9935e8ce8df4732cde44a540a003258c Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Wed, 7 May 2025 19:22:21 -0400 Subject: [Rust] Reduce usage of `IntoCStr` in function signatures This is being done to reduce complexity in function signatures, specifically many of the strings we are passing ultimately should be new types themselves instead of "just strings", things such as type ids. Another place which was confusing was dealing with filesystem related APIs, this commit turns most of those params into a stricter `Path` type. This is bringing the rust api more inline with both python and C++, where the wrapper eagerly converts the string into the languages standard string type. Special consideration must be made for symbols or other possible non utf-8 objects. This commit will be followed up with one that adds the `IntoCStr` bound on API's we want to keep as invalid utf-8 so we can for example, get section by name on a section with invalid utf-8. --- rust/src/websocket/client.rs | 25 +++++++++---------------- rust/src/websocket/provider.rs | 2 +- 2 files changed, 10 insertions(+), 17 deletions(-) (limited to 'rust/src/websocket') diff --git a/rust/src/websocket/client.rs b/rust/src/websocket/client.rs index aa69c381..fc56dc0b 100644 --- a/rust/src/websocket/client.rs +++ b/rust/src/websocket/client.rs @@ -18,11 +18,9 @@ pub trait WebsocketClient: Sync + Send { /// Called to construct this client object with the given core object. fn from_core(core: Ref) -> Self; - fn connect(&self, host: &str, headers: I) -> bool + fn connect(&self, host: &str, headers: I) -> bool where - I: IntoIterator, - K: IntoCStr, - V: IntoCStr; + I: IntoIterator; fn write(&self, data: &[u8]) -> bool; @@ -69,20 +67,13 @@ impl CoreWebsocketClient { /// * `host` - Full url with scheme, domain, optionally port, and path /// * `headers` - HTTP header keys and values /// * `callback` - Callbacks for various websocket events - pub fn initialize_connection( - &self, - host: &str, - headers: I, - callbacks: &mut C, - ) -> bool + pub fn initialize_connection(&self, host: &str, headers: I, callbacks: &mut C) -> bool where - I: IntoIterator, - K: IntoCStr, - V: IntoCStr, + I: IntoIterator, C: WebsocketClientCallback, { let url = host.to_cstr(); - let (header_keys, header_values): (Vec, Vec) = headers + let (header_keys, header_values): (Vec<_>, Vec<_>) = headers .into_iter() .map(|(k, v)| (k.to_cstr(), v.to_cstr())) .unzip(); @@ -187,8 +178,10 @@ pub(crate) unsafe extern "C" fn cb_connect( let header_count = usize::try_from(header_count).unwrap(); let header_keys = core::slice::from_raw_parts(header_keys as *const BnString, header_count); let header_values = core::slice::from_raw_parts(header_values as *const BnString, header_count); - let header_keys_str = header_keys.iter().map(|s| s.to_string_lossy()); - let header_values_str = header_values.iter().map(|s| s.to_string_lossy()); + let header_keys_str = header_keys.iter().map(|s| s.to_string_lossy().to_string()); + let header_values_str = header_values + .iter() + .map(|s| s.to_string_lossy().to_string()); let header = header_keys_str.zip(header_values_str); ctxt.connect(&host.to_string_lossy(), header) } diff --git a/rust/src/websocket/provider.rs b/rust/src/websocket/provider.rs index 40235c2b..914a3f16 100644 --- a/rust/src/websocket/provider.rs +++ b/rust/src/websocket/provider.rs @@ -80,7 +80,7 @@ impl CoreWebsocketProvider { unsafe { Array::new(result, count, ()) } } - pub fn by_name(name: S) -> Option { + pub fn by_name(name: &str) -> Option { let name = name.to_cstr(); let result = unsafe { BNGetWebsocketProviderByName(name.as_ptr()) }; NonNull::new(result).map(|h| unsafe { Self::from_raw(h) }) -- cgit v1.3.1