diff options
| author | Mason Reed <mason@vector35.com> | 2025-05-04 20:43:32 -0400 |
|---|---|---|
| committer | Mason Reed <35282038+emesare@users.noreply.github.com> | 2025-05-12 17:45:24 -0400 |
| commit | f32f083c81a5034530ac33ad2bc460dd234186a5 (patch) | |
| tree | 02e5799b595322b3a6967977ac69257eb023e882 /rust/src | |
| parent | e12dac56c123bcf39708cb381497753250eb1887 (diff) | |
[Rust] More cleanup regarding `BnString`
- Removed `to_string` shortcut from `BnString`.
- Misc formatting
Diffstat (limited to 'rust/src')
| -rw-r--r-- | rust/src/binary_view/memory_map.rs | 4 | ||||
| -rw-r--r-- | rust/src/collaboration/remote.rs | 7 | ||||
| -rw-r--r-- | rust/src/database.rs | 4 | ||||
| -rw-r--r-- | rust/src/debuginfo.rs | 1 | ||||
| -rw-r--r-- | rust/src/lib.rs | 9 | ||||
| -rw-r--r-- | rust/src/metadata.rs | 26 | ||||
| -rw-r--r-- | rust/src/platform.rs | 2 | ||||
| -rw-r--r-- | rust/src/secrets_provider.rs | 3 | ||||
| -rw-r--r-- | rust/src/string.rs | 16 | ||||
| -rw-r--r-- | rust/src/type_archive.rs | 3 | ||||
| -rw-r--r-- | rust/src/type_parser.rs | 15 | ||||
| -rw-r--r-- | rust/src/types.rs | 10 | ||||
| -rw-r--r-- | rust/src/update.rs | 4 | ||||
| -rw-r--r-- | rust/src/websocket/client.rs | 4 |
14 files changed, 41 insertions, 67 deletions
diff --git a/rust/src/binary_view/memory_map.rs b/rust/src/binary_view/memory_map.rs index 3afe0a20..6b6d3d20 100644 --- a/rust/src/binary_view/memory_map.rs +++ b/rust/src/binary_view/memory_map.rs @@ -21,13 +21,13 @@ impl MemoryMap { /// JSON string representation of the base [`MemoryMap`], consisting of unresolved auto and user segments. pub fn base_description(&self) -> String { let desc_raw = unsafe { BNGetBaseMemoryMapDescription(self.view.handle) }; - unsafe { BnString::from_raw(desc_raw) }.to_string() + unsafe { BnString::into_string(desc_raw) } } /// JSON string representation of the [`MemoryMap`]. pub fn description(&self) -> String { let desc_raw = unsafe { BNGetMemoryMapDescription(self.view.handle) }; - unsafe { BnString::from_raw(desc_raw) }.to_string() + unsafe { BnString::into_string(desc_raw) } } // When enabled, the memory map will present a simplified, logical view that merges and abstracts virtual memory diff --git a/rust/src/collaboration/remote.rs b/rust/src/collaboration/remote.rs index ee37e9fb..4d69873a 100644 --- a/rust/src/collaboration/remote.rs +++ b/rust/src/collaboration/remote.rs @@ -167,7 +167,7 @@ impl Remote { &self, username: U, password: P, - ) -> Option<BnString> { + ) -> Option<String> { let username = username.to_cstr(); let password = password.to_cstr(); let token = unsafe { @@ -180,7 +180,7 @@ impl Remote { if token.is_null() { None } else { - Some(unsafe { BnString::from_raw(token) }) + Some(unsafe { BnString::into_string(token) }) } } @@ -226,7 +226,8 @@ impl Remote { }; let username = options.username.to_cstr(); let token = token.to_cstr(); - let success = unsafe { BNRemoteConnect(self.handle.as_ptr(), username.as_ptr(), token.as_ptr()) }; + let success = + unsafe { BNRemoteConnect(self.handle.as_ptr(), username.as_ptr(), token.as_ptr()) }; success.then_some(()).ok_or(()) } diff --git a/rust/src/database.rs b/rust/src/database.rs index d01839d2..61d6f608 100644 --- a/rust/src/database.rs +++ b/rust/src/database.rs @@ -147,10 +147,10 @@ impl Database { } /// Get a dictionary of all globals - pub fn globals(&self) -> HashMap<String, String> { + pub fn globals(&self) -> HashMap<String, BnString> { self.global_keys() .iter() - .filter_map(|key| Some((key.to_string(), self.read_global(key)?.to_string()))) + .filter_map(|key| Some((key.to_string(), self.read_global(key)?))) .collect() } diff --git a/rust/src/debuginfo.rs b/rust/src/debuginfo.rs index 9336a28d..92c0170d 100644 --- a/rust/src/debuginfo.rs +++ b/rust/src/debuginfo.rs @@ -319,6 +319,7 @@ unsafe impl CoreArrayProviderInner for DebugInfoParser { /// /// Functions will not be created if an address is not provided, but will be able to be queried from debug info for later user analysis. pub struct DebugFunctionInfo { + // TODO: These need to be BnString if we want to support invalid UTF-8 short_name: Option<String>, full_name: Option<String>, raw_name: Option<String>, diff --git a/rust/src/lib.rs b/rust/src/lib.rs index c0b3a38c..08abc349 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -329,8 +329,7 @@ pub fn save_last_run() { pub fn path_relative_to_bundled_plugin_directory(path: impl AsRef<Path>) -> Result<PathBuf, ()> { let path_raw = path.as_ref().to_cstr(); - let s: *mut c_char = - unsafe { BNGetPathRelativeToBundledPluginDirectory(path_raw.as_ptr()) }; + let s: *mut c_char = unsafe { BNGetPathRelativeToBundledPluginDirectory(path_raw.as_ptr()) }; if s.is_null() { return Err(()); } @@ -339,8 +338,7 @@ pub fn path_relative_to_bundled_plugin_directory(path: impl AsRef<Path>) -> Resu pub fn path_relative_to_user_plugin_directory(path: impl AsRef<Path>) -> Result<PathBuf, ()> { let path_raw = path.as_ref().to_cstr(); - let s: *mut c_char = - unsafe { BNGetPathRelativeToUserPluginDirectory(path_raw.as_ptr()) }; + let s: *mut c_char = unsafe { BNGetPathRelativeToUserPluginDirectory(path_raw.as_ptr()) }; if s.is_null() { return Err(()); } @@ -349,8 +347,7 @@ pub fn path_relative_to_user_plugin_directory(path: impl AsRef<Path>) -> Result< pub fn path_relative_to_user_directory(path: impl AsRef<Path>) -> Result<PathBuf, ()> { let path_raw = path.as_ref().to_cstr(); - let s: *mut c_char = - unsafe { BNGetPathRelativeToUserDirectory(path_raw.as_ptr()) }; + let s: *mut c_char = unsafe { BNGetPathRelativeToUserDirectory(path_raw.as_ptr()) }; if s.is_null() { return Err(()); } diff --git a/rust/src/metadata.rs b/rust/src/metadata.rs index cc10885a..b6b3ce61 100644 --- a/rust/src/metadata.rs +++ b/rust/src/metadata.rs @@ -213,7 +213,7 @@ impl Metadata { } } - pub fn get_value_store(&self) -> Result<HashMap<BnString, Ref<Metadata>>, ()> { + pub fn get_value_store(&self) -> Result<HashMap<String, Ref<Metadata>>, ()> { match self.get_type() { MetadataType::KeyValueDataType => { let ptr: *mut BNMetadataValueStore = @@ -230,7 +230,7 @@ impl Metadata { let mut map = HashMap::new(); for i in 0..size { - let key = unsafe { BnString::from_raw(keys[i]) }; + let key = unsafe { BnString::into_string(keys[i]) }; let value = unsafe { Ref::<Metadata>::new(Self { @@ -589,7 +589,7 @@ impl TryFrom<&Metadata> for String { type Error = (); fn try_from(value: &Metadata) -> Result<Self, Self::Error> { - value.get_string().map(|s| s.to_string()) + value.get_string().map(|s| s.to_string_lossy().to_string()) } } @@ -637,9 +637,11 @@ impl TryFrom<&Metadata> for Vec<String> { type Error = (); fn try_from(value: &Metadata) -> Result<Self, Self::Error> { - value - .get_string_list() - .map(|v| v.into_iter().map(|s| s.to_string()).collect()) + value.get_string_list().map(|v| { + v.into_iter() + .map(|s| s.to_string_lossy().to_string()) + .collect() + }) } } @@ -659,21 +661,11 @@ impl TryFrom<&Metadata> for Array<Metadata> { } } -impl TryFrom<&Metadata> for HashMap<BnString, Ref<Metadata>> { - type Error = (); - - fn try_from(value: &Metadata) -> Result<Self, Self::Error> { - value.get_value_store() - } -} - impl TryFrom<&Metadata> for HashMap<String, Ref<Metadata>> { type Error = (); fn try_from(value: &Metadata) -> Result<Self, Self::Error> { - value - .get_value_store() - .map(|m| m.into_iter().map(|(k, v)| (k.to_string(), v)).collect()) + value.get_value_store() } } diff --git a/rust/src/platform.rs b/rust/src/platform.rs index c225392f..b51ad61d 100644 --- a/rust/src/platform.rs +++ b/rust/src/platform.rs @@ -334,7 +334,7 @@ impl Platform { assert!(!error_string.is_null()); Err(TypeParserError::new( TypeParserErrorSeverity::FatalSeverity, - unsafe { BnString::from_raw(error_string) }.to_string(), + unsafe { BnString::into_string(error_string) }, filename.to_string(), 0, 0, diff --git a/rust/src/secrets_provider.rs b/rust/src/secrets_provider.rs index 7a807300..fcb20644 100644 --- a/rust/src/secrets_provider.rs +++ b/rust/src/secrets_provider.rs @@ -36,8 +36,7 @@ impl CoreSecretsProvider { storeData: Some(cb_store_data::<C>), deleteData: Some(cb_delete_data::<C>), }; - let result = - unsafe { BNRegisterSecretsProvider(name.as_ptr(), &mut callbacks) }; + let result = unsafe { BNRegisterSecretsProvider(name.as_ptr(), &mut callbacks) }; unsafe { Self::from_raw(NonNull::new(result).unwrap()) } } diff --git a/rust/src/string.rs b/rust/src/string.rs index 640731ff..5be623c1 100644 --- a/rust/src/string.rs +++ b/rust/src/string.rs @@ -15,6 +15,7 @@ //! String wrappers for core-owned strings and strings being passed to the core use crate::rc::*; +use crate::type_archive::TypeArchiveSnapshotId; use crate::types::QualifiedName; use std::borrow::Cow; use std::ffi::{c_char, CStr, CString}; @@ -23,7 +24,6 @@ use std::hash::{Hash, Hasher}; use std::mem; use std::ops::Deref; use std::path::{Path, PathBuf}; -use crate::type_archive::TypeArchiveSnapshotId; // TODO: Remove or refactor this. pub(crate) fn raw_to_string(ptr: *const c_char) -> Option<String> { @@ -72,7 +72,7 @@ impl BnString { /// /// This expects the passed raw string to be owned, as in, freed by us. pub unsafe fn into_string(raw: *mut c_char) -> String { - Self::from_raw(raw).to_string() + Self::from_raw(raw).to_string_lossy().to_string() } /// Construct a BnString from an owned const char* allocated by BNAllocString @@ -160,15 +160,9 @@ impl PartialEq for BnString { impl Eq for BnString {} -impl fmt::Display for BnString { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.to_string_lossy()) - } -} - impl fmt::Debug for BnString { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.to_string_lossy().fmt(f) + write!(f, "{}", self.to_string_lossy()) } } @@ -294,10 +288,10 @@ unsafe impl AsCStr for &Path { unsafe impl AsCStr for TypeArchiveSnapshotId { type Result = CString; - + fn to_cstr(self) -> Self::Result { self.to_string().to_cstr() - } + } } pub trait IntoJson { diff --git a/rust/src/type_archive.rs b/rust/src/type_archive.rs index 2daca983..05929561 100644 --- a/rust/src/type_archive.rs +++ b/rust/src/type_archive.rs @@ -75,8 +75,7 @@ impl TypeArchive { /// If the file has already been created and is not a valid type archive this will return `None`. pub fn create(path: impl AsRef<Path>, platform: &Platform) -> Option<Ref<TypeArchive>> { let raw_path = path.as_ref().to_cstr(); - let handle = - unsafe { BNCreateTypeArchive(raw_path.as_ptr(), platform.handle) }; + let handle = unsafe { BNCreateTypeArchive(raw_path.as_ptr(), platform.handle) }; NonNull::new(handle).map(|handle| unsafe { TypeArchive::ref_from_raw(handle) }) } diff --git a/rust/src/type_parser.rs b/rust/src/type_parser.rs index af17a17f..53827e65 100644 --- a/rust/src/type_parser.rs +++ b/rust/src/type_parser.rs @@ -67,18 +67,13 @@ impl CoreTypeParser { impl TypeParser for CoreTypeParser { fn get_option_text(&self, option: TypeParserOption, value: &str) -> Option<String> { let mut output = std::ptr::null_mut(); - let value_cstr = BnString::new(value); + let value_ptr = std::ptr::null_mut(); let result = unsafe { - BNGetTypeParserOptionText( - self.handle.as_ptr(), - option, - value_cstr.as_ptr(), - &mut output, - ) + BNGetTypeParserOptionText(self.handle.as_ptr(), option, value_ptr, &mut output) }; result.then(|| { assert!(!output.is_null()); - value_cstr.to_string() + unsafe { BnString::into_string(value_ptr) } }) } @@ -114,8 +109,8 @@ impl TypeParser for CoreTypeParser { }; if success { assert!(!result.is_null()); - let bn_result = unsafe { BnString::from_raw(result) }; - Ok(bn_result.to_string()) + let bn_result = unsafe { BnString::into_string(result) }; + Ok(bn_result) } else { let errors: Array<TypeParserError> = unsafe { Array::new(errors, error_count, ()) }; Err(errors.to_vec()) diff --git a/rust/src/types.rs b/rust/src/types.rs index 710a8857..42c1f8d2 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -430,7 +430,7 @@ impl TypeBuilder { impl Display for TypeBuilder { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", unsafe { - BnString::from_raw(BNGetTypeBuilderString(self.handle, std::ptr::null_mut())) + BnString::into_string(BNGetTypeBuilderString(self.handle, std::ptr::null_mut())) }) } } @@ -944,7 +944,7 @@ impl Type { impl Display for Type { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", unsafe { - BnString::from_raw(BNGetTypeString( + BnString::into_string(BNGetTypeString( self.handle, std::ptr::null_mut(), BNTokenEscapingType::NoTokenEscapingType, @@ -1893,15 +1893,15 @@ impl NamedTypeReference { QualifiedName::from_owned_raw(raw_name) } - pub fn id(&self) -> BnString { - unsafe { BnString::from_raw(BNGetTypeReferenceId(self.handle)) } + pub fn id(&self) -> String { + unsafe { BnString::into_string(BNGetTypeReferenceId(self.handle)) } } pub fn class(&self) -> NamedTypeReferenceClass { unsafe { BNGetTypeReferenceClass(self.handle) } } - fn target_helper(&self, bv: &BinaryView, visited: &mut HashSet<BnString>) -> Option<Ref<Type>> { + fn target_helper(&self, bv: &BinaryView, visited: &mut HashSet<String>) -> Option<Ref<Type>> { let ty = bv.type_by_id(self.id())?; match ty.type_class() { TypeClass::NamedTypeReferenceClass => { diff --git a/rust/src/update.rs b/rust/src/update.rs index b3c45d84..be85c93e 100644 --- a/rust/src/update.rs +++ b/rust/src/update.rs @@ -97,9 +97,7 @@ impl UpdateChannel { let mut count = 0; let mut errors = std::ptr::null_mut(); let name = self.name.clone().to_cstr(); - let result = unsafe { - BNGetUpdateChannelVersions(name.as_ptr(), &mut count, &mut errors) - }; + let result = unsafe { BNGetUpdateChannelVersions(name.as_ptr(), &mut count, &mut errors) }; if !errors.is_null() { Err(unsafe { BnString::from_raw(errors) }) } else { diff --git a/rust/src/websocket/client.rs b/rust/src/websocket/client.rs index e9e753b1..8e2c6db0 100644 --- a/rust/src/websocket/client.rs +++ b/rust/src/websocket/client.rs @@ -124,9 +124,7 @@ impl CoreWebsocketClient { /// Call the error callback function pub fn notify_error(&self, msg: &str) { let error = msg.to_cstr(); - unsafe { - BNNotifyWebsocketClientError(self.handle.as_ptr(), error.as_ptr()) - } + unsafe { BNNotifyWebsocketClientError(self.handle.as_ptr(), error.as_ptr()) } } /// Call the read callback function, forward the callback returned value |
