diff options
| author | Mason Reed <mason@vector35.com> | 2025-05-04 19:10:56 -0400 |
|---|---|---|
| committer | Mason Reed <35282038+emesare@users.noreply.github.com> | 2025-05-12 17:45:24 -0400 |
| commit | a826c589dfc10c542deba7ca3343a462e02d6bde (patch) | |
| tree | f116254bef39f787268bbecc5eac19da310db9ce /rust/src/secrets_provider.rs | |
| parent | 28b3c4044af06fdc32c9c85bf8381b5058306427 (diff) | |
[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 <michael.krasnitski@gmail.com>
Diffstat (limited to 'rust/src/secrets_provider.rs')
| -rw-r--r-- | rust/src/secrets_provider.rs | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/rust/src/secrets_provider.rs b/rust/src/secrets_provider.rs index 5f42dbea..7acf6d4b 100644 --- a/rust/src/secrets_provider.rs +++ b/rust/src/secrets_provider.rs @@ -4,7 +4,7 @@ use std::fmt::Debug; use std::ptr::NonNull; use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner}; -use crate::string::{BnStrCompatible, BnString}; +use crate::string::{AsCStr, BnString}; pub trait SecretsProvider { fn has_data(&mut self, key: &str) -> bool; @@ -27,7 +27,7 @@ impl CoreSecretsProvider { /// Register a new provider pub fn new<C: SecretsProvider>(name: &str, callback: C) -> Self { // SAFETY: once create SecretsProvider is never dropped - let name = name.into_bytes_with_nul(); + let name = name.to_cstr(); let callback = Box::leak(Box::new(callback)); let mut callbacks = BNSecretsProviderCallbacks { context: callback as *mut C as *mut c_void, @@ -50,8 +50,8 @@ impl CoreSecretsProvider { } /// Retrieve a provider by name - pub fn by_name<S: BnStrCompatible>(name: S) -> Option<CoreSecretsProvider> { - let name = name.into_bytes_with_nul(); + pub fn by_name<S: AsCStr>(name: S) -> Option<CoreSecretsProvider> { + let name = name.to_cstr(); let result = unsafe { BNGetSecretsProviderByName(name.as_ref().as_ptr() as *const c_char) }; NonNull::new(result).map(|h| unsafe { Self::from_raw(h) }) } @@ -63,16 +63,16 @@ impl CoreSecretsProvider { } /// Check if data for a specific key exists, but do not retrieve it - pub fn has_data<S: BnStrCompatible>(&self, key: S) -> bool { - let key = key.into_bytes_with_nul(); + pub fn has_data<S: AsCStr>(&self, key: S) -> bool { + let key = key.to_cstr(); unsafe { BNSecretsProviderHasData(self.handle.as_ptr(), key.as_ref().as_ptr() as *const c_char) } } /// Retrieve data for the given key, if it exists - pub fn get_data<S: BnStrCompatible>(&self, key: S) -> String { - let key = key.into_bytes_with_nul(); + pub fn get_data<S: AsCStr>(&self, key: S) -> String { + let key = key.to_cstr(); let result = unsafe { BNGetSecretsProviderData(self.handle.as_ptr(), key.as_ref().as_ptr() as *const c_char) }; @@ -80,9 +80,9 @@ impl CoreSecretsProvider { } /// Store data with the given key - pub fn store_data<K: BnStrCompatible, V: BnStrCompatible>(&self, key: K, value: V) -> bool { - let key = key.into_bytes_with_nul(); - let value = value.into_bytes_with_nul(); + pub fn store_data<K: AsCStr, V: AsCStr>(&self, key: K, value: V) -> bool { + let key = key.to_cstr(); + let value = value.to_cstr(); unsafe { BNStoreSecretsProviderData( self.handle.as_ptr(), @@ -93,8 +93,8 @@ impl CoreSecretsProvider { } /// Delete stored data with the given key - pub fn delete_data<S: BnStrCompatible>(&self, key: S) -> bool { - let key = key.into_bytes_with_nul(); + pub fn delete_data<S: AsCStr>(&self, key: S) -> bool { + let key = key.to_cstr(); unsafe { BNDeleteSecretsProviderData( self.handle.as_ptr(), |
