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/metadata.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/metadata.rs')
| -rw-r--r-- | rust/src/metadata.rs | 40 |
1 files changed, 18 insertions, 22 deletions
diff --git a/rust/src/metadata.rs b/rust/src/metadata.rs index fc935cd5..c31e806b 100644 --- a/rust/src/metadata.rs +++ b/rust/src/metadata.rs @@ -1,5 +1,5 @@ use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable}; -use crate::string::{BnStrCompatible, BnString, IntoJson}; +use crate::string::{AsCStr, BnString, IntoJson}; use binaryninjacore_sys::*; use std::collections::HashMap; use std::os::raw::c_char; @@ -267,14 +267,14 @@ impl Metadata { Ok(Some(unsafe { Self::ref_from_raw(ptr) })) } - pub fn get<S: BnStrCompatible>(&self, key: S) -> Result<Option<Ref<Metadata>>, ()> { + pub fn get<S: AsCStr>(&self, key: S) -> Result<Option<Ref<Metadata>>, ()> { if self.get_type() != MetadataType::KeyValueDataType { return Err(()); } let ptr: *mut BNMetadata = unsafe { BNMetadataGetForKey( self.handle, - key.into_bytes_with_nul().as_ref().as_ptr() as *const c_char, + key.to_cstr().as_ref().as_ptr() as *const c_char, ) }; if ptr.is_null() { @@ -291,7 +291,7 @@ impl Metadata { Ok(()) } - pub fn insert<S: BnStrCompatible>(&self, key: S, value: &Metadata) -> Result<(), ()> { + pub fn insert<S: AsCStr>(&self, key: S, value: &Metadata) -> Result<(), ()> { if self.get_type() != MetadataType::KeyValueDataType { return Err(()); } @@ -299,7 +299,7 @@ impl Metadata { unsafe { BNMetadataSetValueForKey( self.handle, - key.into_bytes_with_nul().as_ref().as_ptr() as *const c_char, + key.to_cstr().as_ref().as_ptr() as *const c_char, value.handle, ) }; @@ -315,7 +315,7 @@ impl Metadata { Ok(()) } - pub fn remove_key<S: BnStrCompatible>(&self, key: S) -> Result<(), ()> { + pub fn remove_key<S: AsCStr>(&self, key: S) -> Result<(), ()> { if self.get_type() != MetadataType::KeyValueDataType { return Err(()); } @@ -323,7 +323,7 @@ impl Metadata { unsafe { BNMetadataRemoveKey( self.handle, - key.into_bytes_with_nul().as_ref().as_ptr() as *const c_char, + key.to_cstr().as_ref().as_ptr() as *const c_char, ) }; Ok(()) @@ -398,7 +398,7 @@ impl From<String> for Ref<Metadata> { fn from(value: String) -> Self { unsafe { Metadata::ref_from_raw(BNCreateMetadataStringData( - value.into_bytes_with_nul().as_ptr() as *const c_char, + value.to_cstr().as_ptr() as *const c_char )) } } @@ -408,7 +408,7 @@ impl From<&str> for Ref<Metadata> { fn from(value: &str) -> Self { unsafe { Metadata::ref_from_raw(BNCreateMetadataStringData( - value.into_bytes_with_nul().as_ptr() as *const c_char, + value.to_cstr().as_ptr() as *const c_char )) } } @@ -444,12 +444,10 @@ impl From<&Array<Metadata>> for Ref<Metadata> { } } -impl<S: BnStrCompatible> From<HashMap<S, Ref<Metadata>>> for Ref<Metadata> { +impl<S: AsCStr> From<HashMap<S, Ref<Metadata>>> for Ref<Metadata> { fn from(value: HashMap<S, Ref<Metadata>>) -> Self { - let data: Vec<(S::Result, Ref<Metadata>)> = value - .into_iter() - .map(|(k, v)| (k.into_bytes_with_nul(), v)) - .collect(); + let data: Vec<(S::Result, Ref<Metadata>)> = + value.into_iter().map(|(k, v)| (k.to_cstr(), v)).collect(); let mut keys: Vec<*const c_char> = data .iter() .map(|(k, _)| k.as_ref().as_ptr() as *const c_char) @@ -468,14 +466,12 @@ impl<S: BnStrCompatible> From<HashMap<S, Ref<Metadata>>> for Ref<Metadata> { impl<S, T> From<&[(S, T)]> for Ref<Metadata> where - S: BnStrCompatible + Copy, + S: AsCStr + Copy, for<'a> &'a T: Into<Ref<Metadata>>, { fn from(value: &[(S, T)]) -> Self { - let data: Vec<(S::Result, Ref<Metadata>)> = value - .iter() - .map(|(k, v)| (k.into_bytes_with_nul(), v.into())) - .collect(); + let data: Vec<(S::Result, Ref<Metadata>)> = + value.iter().map(|(k, v)| (k.to_cstr(), v.into())).collect(); let mut keys: Vec<*const c_char> = data .iter() .map(|(k, _)| k.as_ref().as_ptr() as *const c_char) @@ -494,7 +490,7 @@ where impl<S, T, const N: usize> From<[(S, T); N]> for Ref<Metadata> where - S: BnStrCompatible + Copy, + S: AsCStr + Copy, for<'a> &'a T: Into<Ref<Metadata>>, { fn from(value: [(S, T); N]) -> Self { @@ -548,11 +544,11 @@ impl From<&Vec<f64>> for Ref<Metadata> { } } -impl<S: BnStrCompatible> From<Vec<S>> for Ref<Metadata> { +impl<S: AsCStr> From<Vec<S>> for Ref<Metadata> { fn from(value: Vec<S>) -> Self { let mut refs = vec![]; for v in value { - refs.push(v.into_bytes_with_nul()); + refs.push(v.to_cstr()); } let mut pointers = vec![]; for r in &refs { |
