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/command.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'rust/src/command.rs') diff --git a/rust/src/command.rs b/rust/src/command.rs index 48a91164..0297d6cf 100644 --- a/rust/src/command.rs +++ b/rust/src/command.rs @@ -42,7 +42,7 @@ use std::os::raw::c_void; use crate::binary_view::BinaryView; use crate::function::Function; -use crate::string::BnStrCompatible; +use crate::string::AsCStr; /// The trait required for generic commands. See [register_command] for example usage. pub trait Command: 'static + Sync { @@ -95,7 +95,7 @@ where /// ``` pub fn register_command(name: S, desc: S, command: C) where - S: BnStrCompatible, + S: AsCStr, C: Command, { extern "C" fn cb_action(ctxt: *mut c_void, view: *mut BNBinaryView) @@ -126,8 +126,8 @@ where }) } - let name = name.into_bytes_with_nul(); - let desc = desc.into_bytes_with_nul(); + let name = name.to_cstr(); + let desc = desc.to_cstr(); let name_ptr = name.as_ref().as_ptr() as *mut _; let desc_ptr = desc.as_ref().as_ptr() as *mut _; @@ -196,7 +196,7 @@ where /// ``` pub fn register_command_for_address(name: S, desc: S, command: C) where - S: BnStrCompatible, + S: AsCStr, C: AddressCommand, { extern "C" fn cb_action(ctxt: *mut c_void, view: *mut BNBinaryView, addr: u64) @@ -227,8 +227,8 @@ where }) } - let name = name.into_bytes_with_nul(); - let desc = desc.into_bytes_with_nul(); + let name = name.to_cstr(); + let desc = desc.to_cstr(); let name_ptr = name.as_ref().as_ptr() as *mut _; let desc_ptr = desc.as_ref().as_ptr() as *mut _; @@ -298,7 +298,7 @@ where /// ``` pub fn register_command_for_range(name: S, desc: S, command: C) where - S: BnStrCompatible, + S: AsCStr, C: RangeCommand, { extern "C" fn cb_action(ctxt: *mut c_void, view: *mut BNBinaryView, addr: u64, len: u64) @@ -334,8 +334,8 @@ where }) } - let name = name.into_bytes_with_nul(); - let desc = desc.into_bytes_with_nul(); + let name = name.to_cstr(); + let desc = desc.to_cstr(); let name_ptr = name.as_ref().as_ptr() as *mut _; let desc_ptr = desc.as_ref().as_ptr() as *mut _; @@ -405,7 +405,7 @@ where /// ``` pub fn register_command_for_function(name: S, desc: S, command: C) where - S: BnStrCompatible, + S: AsCStr, C: FunctionCommand, { extern "C" fn cb_action(ctxt: *mut c_void, view: *mut BNBinaryView, func: *mut BNFunction) @@ -446,8 +446,8 @@ where }) } - let name = name.into_bytes_with_nul(); - let desc = desc.into_bytes_with_nul(); + let name = name.to_cstr(); + let desc = desc.to_cstr(); let name_ptr = name.as_ref().as_ptr() as *mut _; let desc_ptr = desc.as_ref().as_ptr() as *mut _; -- cgit v1.3.1