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/metadata.rs | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) (limited to 'rust/src/metadata.rs') 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(&self, key: S) -> Result>, ()> { + pub fn get(&self, key: S) -> Result>, ()> { 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(&self, key: S, value: &Metadata) -> Result<(), ()> { + pub fn insert(&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(&self, key: S) -> Result<(), ()> { + pub fn remove_key(&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 for Ref { 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 { 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> for Ref { } } -impl From>> for Ref { +impl From>> for Ref { fn from(value: HashMap>) -> Self { - let data: Vec<(S::Result, Ref)> = value - .into_iter() - .map(|(k, v)| (k.into_bytes_with_nul(), v)) - .collect(); + let data: Vec<(S::Result, Ref)> = + 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 From>> for Ref { impl From<&[(S, T)]> for Ref where - S: BnStrCompatible + Copy, + S: AsCStr + Copy, for<'a> &'a T: Into>, { fn from(value: &[(S, T)]) -> Self { - let data: Vec<(S::Result, Ref)> = value - .iter() - .map(|(k, v)| (k.into_bytes_with_nul(), v.into())) - .collect(); + let data: Vec<(S::Result, Ref)> = + 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 From<[(S, T); N]> for Ref where - S: BnStrCompatible + Copy, + S: AsCStr + Copy, for<'a> &'a T: Into>, { fn from(value: [(S, T); N]) -> Self { @@ -548,11 +544,11 @@ impl From<&Vec> for Ref { } } -impl From> for Ref { +impl From> for Ref { fn from(value: Vec) -> 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 { -- cgit v1.3.1