Skip to content

Commit f82832f

Browse files
emesaremkrasnitski
andcommitted
[Rust] Simplify BnStrCompatible trait
Followup to #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>
1 parent de7a210 commit f82832f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1039
-1225
lines changed

rust/src/architecture.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::{
2727
platform::Platform,
2828
rc::*,
2929
relocation::CoreRelocationHandler,
30-
string::BnStrCompatible,
30+
string::AsCStr,
3131
string::*,
3232
types::{NameAndType, Type},
3333
Endianness,
@@ -1404,8 +1404,7 @@ impl CoreArchitecture {
14041404
}
14051405

14061406
pub fn by_name(name: &str) -> Option<Self> {
1407-
let handle =
1408-
unsafe { BNGetArchitectureByName(name.into_bytes_with_nul().as_ptr() as *mut _) };
1407+
let handle = unsafe { BNGetArchitectureByName(name.to_cstr().as_ptr() as *mut _) };
14091408
match handle.is_null() {
14101409
false => Some(CoreArchitecture { handle }),
14111410
true => None,
@@ -1953,8 +1952,8 @@ macro_rules! cc_func {
19531952

19541953
/// Contains helper methods for all types implementing 'Architecture'
19551954
pub trait ArchitectureExt: Architecture {
1956-
fn register_by_name<S: BnStrCompatible>(&self, name: S) -> Option<Self::Register> {
1957-
let name = name.into_bytes_with_nul();
1955+
fn register_by_name<S: AsCStr>(&self, name: S) -> Option<Self::Register> {
1956+
let name = name.to_cstr();
19581957

19591958
match unsafe {
19601959
BNGetArchitectureRegisterByName(self.as_ref().handle, name.as_ref().as_ptr() as *mut _)
@@ -2033,7 +2032,7 @@ pub trait ArchitectureExt: Architecture {
20332032

20342033
fn register_relocation_handler<S, R, F>(&self, name: S, func: F)
20352034
where
2036-
S: BnStrCompatible,
2035+
S: AsCStr,
20372036
R: 'static
20382037
+ RelocationHandler<Handle = CustomRelocationHandlerHandle<R>>
20392038
+ Send
@@ -2056,7 +2055,7 @@ impl<T: Architecture> ArchitectureExt for T {}
20562055

20572056
pub fn register_architecture<S, A, F>(name: S, func: F) -> &'static A
20582057
where
2059-
S: BnStrCompatible,
2058+
S: AsCStr,
20602059
A: 'static + Architecture<Handle = CustomArchitectureHandle<A>> + Send + Sync + Sized,
20612060
F: FnOnce(CustomArchitectureHandle<A>, CoreArchitecture) -> A,
20622061
{
@@ -3130,7 +3129,7 @@ where
31303129
custom_arch.skip_and_return_value(data, addr, val)
31313130
}
31323131

3133-
let name = name.into_bytes_with_nul();
3132+
let name = name.to_cstr();
31343133

31353134
let uninit_arch = ArchitectureBuilder {
31363135
arch: MaybeUninit::zeroed(),

rust/src/background_task.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ impl BackgroundTask {
4343
Self { handle }
4444
}
4545

46-
pub fn new<S: BnStrCompatible>(initial_text: S, can_cancel: bool) -> Ref<Self> {
47-
let text = initial_text.into_bytes_with_nul();
46+
pub fn new<S: AsCStr>(initial_text: S, can_cancel: bool) -> Ref<Self> {
47+
let text = initial_text.to_cstr();
4848
let handle = unsafe { BNBeginBackgroundTask(text.as_ref().as_ptr() as *mut _, can_cancel) };
4949
// We should always be returned a valid task.
5050
assert!(!handle.is_null());
@@ -75,8 +75,8 @@ impl BackgroundTask {
7575
unsafe { BnString::into_string(BNGetBackgroundTaskProgressText(self.handle)) }
7676
}
7777

78-
pub fn set_progress_text<S: BnStrCompatible>(&self, text: S) {
79-
let progress_text = text.into_bytes_with_nul();
78+
pub fn set_progress_text<S: AsCStr>(&self, text: S) {
79+
let progress_text = text.to_cstr();
8080
unsafe {
8181
BNSetBackgroundTaskProgressText(self.handle, progress_text.as_ref().as_ptr() as *mut _)
8282
}

0 commit comments

Comments
 (0)