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/settings.rs | 207 +++++++++++++++++++-------------------------------- 1 file changed, 78 insertions(+), 129 deletions(-) (limited to 'rust/src/settings.rs') diff --git a/rust/src/settings.rs b/rust/src/settings.rs index 4b4d418f..b49dbdea 100644 --- a/rust/src/settings.rs +++ b/rust/src/settings.rs @@ -20,7 +20,7 @@ use std::fmt::Debug; use crate::binary_view::BinaryView; use crate::rc::*; -use crate::string::{BnStrCompatible, BnString}; +use crate::string::{AsCStr, BnString}; use crate::function::Function; @@ -44,8 +44,8 @@ impl Settings { Self::new_with_id(GLOBAL_INSTANCE_ID) } - pub fn new_with_id(instance_id: S) -> Ref { - let instance_id = instance_id.into_bytes_with_nul(); + pub fn new_with_id(instance_id: S) -> Ref { + let instance_id = instance_id.to_cstr(); unsafe { let handle = BNCreateSettings(instance_id.as_ref().as_ptr() as *mut _); debug_assert!(!handle.is_null()); @@ -53,8 +53,8 @@ impl Settings { } } - pub fn set_resource_id(&self, resource_id: S) { - let resource_id = resource_id.into_bytes_with_nul(); + pub fn set_resource_id(&self, resource_id: S) { + let resource_id = resource_id.to_cstr(); unsafe { BNSettingsSetResourceId(self.handle, resource_id.as_ref().as_ptr() as *mut _) }; } @@ -62,16 +62,16 @@ impl Settings { unsafe { BnString::into_string(BNSettingsSerializeSchema(self.handle)) } } - pub fn deserialize_schema(&self, schema: S) -> bool { + pub fn deserialize_schema(&self, schema: S) -> bool { self.deserialize_schema_with_scope(schema, SettingsScope::SettingsAutoScope) } - pub fn deserialize_schema_with_scope( + pub fn deserialize_schema_with_scope( &self, schema: S, scope: SettingsScope, ) -> bool { - let schema = schema.into_bytes_with_nul(); + let schema = schema.to_cstr(); unsafe { BNSettingsDeserializeSchema( self.handle, @@ -82,8 +82,8 @@ impl Settings { } } - pub fn contains(&self, key: S) -> bool { - let key = key.into_bytes_with_nul(); + pub fn contains(&self, key: S) -> bool { + let key = key.to_cstr(); unsafe { BNSettingsContains(self.handle, key.as_ref().as_ptr() as *mut _) } } @@ -97,16 +97,12 @@ impl Settings { // TODO Update the settings API to take an optional BinaryView or Function. Separate functions or...? - pub fn get_bool(&self, key: S) -> bool { + pub fn get_bool(&self, key: S) -> bool { self.get_bool_with_opts(key, &mut QueryOptions::default()) } - pub fn get_bool_with_opts( - &self, - key: S, - options: &mut QueryOptions, - ) -> bool { - let key = key.into_bytes_with_nul(); + pub fn get_bool_with_opts(&self, key: S, options: &mut QueryOptions) -> bool { + let key = key.to_cstr(); let view_ptr = match options.view.as_ref() { Some(view) => view.handle, _ => std::ptr::null_mut(), @@ -126,16 +122,12 @@ impl Settings { } } - pub fn get_double(&self, key: S) -> f64 { + pub fn get_double(&self, key: S) -> f64 { self.get_double_with_opts(key, &mut QueryOptions::default()) } - pub fn get_double_with_opts( - &self, - key: S, - options: &mut QueryOptions, - ) -> f64 { - let key = key.into_bytes_with_nul(); + pub fn get_double_with_opts(&self, key: S, options: &mut QueryOptions) -> f64 { + let key = key.to_cstr(); let view_ptr = match options.view.as_ref() { Some(view) => view.handle, _ => std::ptr::null_mut(), @@ -155,16 +147,12 @@ impl Settings { } } - pub fn get_integer(&self, key: S) -> u64 { + pub fn get_integer(&self, key: S) -> u64 { self.get_integer_with_opts(key, &mut QueryOptions::default()) } - pub fn get_integer_with_opts( - &self, - key: S, - options: &mut QueryOptions, - ) -> u64 { - let key = key.into_bytes_with_nul(); + pub fn get_integer_with_opts(&self, key: S, options: &mut QueryOptions) -> u64 { + let key = key.to_cstr(); let view_ptr = match options.view.as_ref() { Some(view) => view.handle, _ => std::ptr::null_mut(), @@ -184,16 +172,12 @@ impl Settings { } } - pub fn get_string(&self, key: S) -> String { + pub fn get_string(&self, key: S) -> String { self.get_string_with_opts(key, &mut QueryOptions::default()) } - pub fn get_string_with_opts( - &self, - key: S, - options: &mut QueryOptions, - ) -> String { - let key = key.into_bytes_with_nul(); + pub fn get_string_with_opts(&self, key: S, options: &mut QueryOptions) -> String { + let key = key.to_cstr(); let view_ptr = match options.view.as_ref() { Some(view) => view.handle, _ => std::ptr::null_mut(), @@ -213,16 +197,16 @@ impl Settings { } } - pub fn get_string_list(&self, key: S) -> Array { + pub fn get_string_list(&self, key: S) -> Array { self.get_string_list_with_opts(key, &mut QueryOptions::default()) } - pub fn get_string_list_with_opts( + pub fn get_string_list_with_opts( &self, key: S, options: &mut QueryOptions, ) -> Array { - let key = key.into_bytes_with_nul(); + let key = key.to_cstr(); let view_ptr = match options.view.as_ref() { Some(view) => view.handle, _ => std::ptr::null_mut(), @@ -248,16 +232,12 @@ impl Settings { } } - pub fn get_json(&self, key: S) -> String { + pub fn get_json(&self, key: S) -> String { self.get_json_with_opts(key, &mut QueryOptions::default()) } - pub fn get_json_with_opts( - &self, - key: S, - options: &mut QueryOptions, - ) -> String { - let key = key.into_bytes_with_nul(); + pub fn get_json_with_opts(&self, key: S, options: &mut QueryOptions) -> String { + let key = key.to_cstr(); let view_ptr = match options.view.as_ref() { Some(view) => view.handle, _ => std::ptr::null_mut(), @@ -277,17 +257,12 @@ impl Settings { } } - pub fn set_bool(&self, key: S, value: bool) { + pub fn set_bool(&self, key: S, value: bool) { self.set_bool_with_opts(key, value, &QueryOptions::default()) } - pub fn set_bool_with_opts( - &self, - key: S, - value: bool, - options: &QueryOptions, - ) { - let key = key.into_bytes_with_nul(); + pub fn set_bool_with_opts(&self, key: S, value: bool, options: &QueryOptions) { + let key = key.to_cstr(); let view_ptr = match options.view.as_ref() { Some(view) => view.handle, _ => std::ptr::null_mut(), @@ -308,16 +283,11 @@ impl Settings { } } - pub fn set_double(&self, key: S, value: f64) { + pub fn set_double(&self, key: S, value: f64) { self.set_double_with_opts(key, value, &QueryOptions::default()) } - pub fn set_double_with_opts( - &self, - key: S, - value: f64, - options: &QueryOptions, - ) { - let key = key.into_bytes_with_nul(); + pub fn set_double_with_opts(&self, key: S, value: f64, options: &QueryOptions) { + let key = key.to_cstr(); let view_ptr = match options.view.as_ref() { Some(view) => view.handle, _ => std::ptr::null_mut(), @@ -338,17 +308,12 @@ impl Settings { } } - pub fn set_integer(&self, key: S, value: u64) { + pub fn set_integer(&self, key: S, value: u64) { self.set_integer_with_opts(key, value, &QueryOptions::default()) } - pub fn set_integer_with_opts( - &self, - key: S, - value: u64, - options: &QueryOptions, - ) { - let key = key.into_bytes_with_nul(); + pub fn set_integer_with_opts(&self, key: S, value: u64, options: &QueryOptions) { + let key = key.to_cstr(); let view_ptr = match options.view.as_ref() { Some(view) => view.handle, _ => std::ptr::null_mut(), @@ -369,18 +334,18 @@ impl Settings { } } - pub fn set_string(&self, key: S1, value: S2) { + pub fn set_string(&self, key: S1, value: S2) { self.set_string_with_opts(key, value, &QueryOptions::default()) } - pub fn set_string_with_opts( + pub fn set_string_with_opts( &self, key: S1, value: S2, options: &QueryOptions, ) { - let key = key.into_bytes_with_nul(); - let value = value.into_bytes_with_nul(); + let key = key.to_cstr(); + let value = value.to_cstr(); let view_ptr = match options.view.as_ref() { Some(view) => view.handle, _ => std::ptr::null_mut(), @@ -401,7 +366,7 @@ impl Settings { } } - pub fn set_string_list>( + pub fn set_string_list>( &self, key: S1, value: I, @@ -409,18 +374,14 @@ impl Settings { self.set_string_list_with_opts(key, value, &QueryOptions::default()) } - pub fn set_string_list_with_opts< - S1: BnStrCompatible, - S2: BnStrCompatible, - I: Iterator, - >( + pub fn set_string_list_with_opts>( &self, key: S1, value: I, options: &QueryOptions, ) -> bool { - let key = key.into_bytes_with_nul(); - let raw_list: Vec<_> = value.map(|s| s.into_bytes_with_nul()).collect(); + let key = key.to_cstr(); + let raw_list: Vec<_> = value.map(|s| s.to_cstr()).collect(); let mut raw_list_ptr: Vec<_> = raw_list .iter() .map(|s| s.as_ref().as_ptr() as *const c_char) @@ -447,18 +408,18 @@ impl Settings { } } - pub fn set_json(&self, key: S1, value: S2) -> bool { + pub fn set_json(&self, key: S1, value: S2) -> bool { self.set_json_with_opts(key, value, &QueryOptions::default()) } - pub fn set_json_with_opts( + pub fn set_json_with_opts( &self, key: S1, value: S2, options: &QueryOptions, ) -> bool { - let key = key.into_bytes_with_nul(); - let value = value.into_bytes_with_nul(); + let key = key.to_cstr(); + let value = value.to_cstr(); let view_ptr = match options.view.as_ref() { Some(view) => view.handle, _ => std::ptr::null_mut(), @@ -479,9 +440,9 @@ impl Settings { } } - pub fn get_property_string(&self, key: S, property: S) -> String { - let key = key.into_bytes_with_nul(); - let property = property.into_bytes_with_nul(); + pub fn get_property_string(&self, key: S, property: S) -> String { + let key = key.to_cstr(); + let property = property.to_cstr(); unsafe { BnString::into_string(BNSettingsQueryPropertyString( self.handle, @@ -491,13 +452,9 @@ impl Settings { } } - pub fn get_property_string_list( - &self, - key: S, - property: S, - ) -> Array { - let key = key.into_bytes_with_nul(); - let property = property.into_bytes_with_nul(); + pub fn get_property_string_list(&self, key: S, property: S) -> Array { + let key = key.to_cstr(); + let property = property.to_cstr(); let mut size: usize = 0; unsafe { Array::new( @@ -513,9 +470,9 @@ impl Settings { } } - pub fn update_bool_property(&self, key: S, property: S, value: bool) { - let key = key.into_bytes_with_nul(); - let property = property.into_bytes_with_nul(); + pub fn update_bool_property(&self, key: S, property: S, value: bool) { + let key = key.to_cstr(); + let property = property.to_cstr(); unsafe { BNSettingsUpdateBoolProperty( self.handle, @@ -526,9 +483,9 @@ impl Settings { } } - pub fn update_integer_property(&self, key: S, property: S, value: u64) { - let key = key.into_bytes_with_nul(); - let property = property.into_bytes_with_nul(); + pub fn update_integer_property(&self, key: S, property: S, value: u64) { + let key = key.to_cstr(); + let property = property.to_cstr(); unsafe { BNSettingsUpdateUInt64Property( self.handle, @@ -539,9 +496,9 @@ impl Settings { } } - pub fn update_double_property(&self, key: S, property: S, value: f64) { - let key = key.into_bytes_with_nul(); - let property = property.into_bytes_with_nul(); + pub fn update_double_property(&self, key: S, property: S, value: f64) { + let key = key.to_cstr(); + let property = property.to_cstr(); unsafe { BNSettingsUpdateDoubleProperty( self.handle, @@ -552,10 +509,10 @@ impl Settings { } } - pub fn update_string_property(&self, key: S, property: S, value: S) { - let key = key.into_bytes_with_nul(); - let property = property.into_bytes_with_nul(); - let value = value.into_bytes_with_nul(); + pub fn update_string_property(&self, key: S, property: S, value: S) { + let key = key.to_cstr(); + let property = property.to_cstr(); + let value = value.to_cstr(); unsafe { BNSettingsUpdateStringProperty( self.handle, @@ -566,15 +523,15 @@ impl Settings { } } - pub fn update_string_list_property>( + pub fn update_string_list_property>( &self, key: S, property: S, value: I, ) { - let key = key.into_bytes_with_nul(); - let property = property.into_bytes_with_nul(); - let raw_list: Vec<_> = value.map(|s| s.into_bytes_with_nul()).collect(); + let key = key.to_cstr(); + let property = property.to_cstr(); + let raw_list: Vec<_> = value.map(|s| s.to_cstr()).collect(); let mut raw_list_ptr: Vec<_> = raw_list .iter() .map(|s| s.as_ref().as_ptr() as *const c_char) @@ -591,13 +548,9 @@ impl Settings { } } - pub fn register_group( - &self, - group: S1, - title: S2, - ) -> bool { - let group = group.into_bytes_with_nul(); - let title = title.into_bytes_with_nul(); + pub fn register_group(&self, group: S1, title: S2) -> bool { + let group = group.to_cstr(); + let title = title.to_cstr(); unsafe { BNSettingsRegisterGroup( @@ -608,13 +561,9 @@ impl Settings { } } - pub fn register_setting_json( - &self, - group: S1, - properties: S2, - ) -> bool { - let group = group.into_bytes_with_nul(); - let properties = properties.into_bytes_with_nul(); + pub fn register_setting_json(&self, group: S1, properties: S2) -> bool { + let group = group.to_cstr(); + let properties = properties.to_cstr(); unsafe { BNSettingsRegisterSetting( -- cgit v1.3.1