Skip to content

Commit

Permalink
Replace BnStrCompatible with AsCStr trait
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski committed Jan 21, 2025
1 parent 3f1533e commit 193b836
Show file tree
Hide file tree
Showing 43 changed files with 985 additions and 1,981 deletions.
2 changes: 1 addition & 1 deletion plugins/warp/src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ pub struct PlatformID(u64);
impl From<&Platform> for PlatformID {
fn from(value: &Platform) -> Self {
let mut hasher = DefaultHasher::new();
hasher.write(value.name().to_bytes());
hasher.write(value.name().as_bytes());
Self(hasher.finish())
}
}
Expand Down
20 changes: 7 additions & 13 deletions rust/src/architecture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
platform::Platform,
rc::*,
relocation::CoreRelocationHandler,
string::BnStrCompatible,
string::AsCStr,
string::*,
types::{NameAndType, Type},
Endianness,
Expand Down Expand Up @@ -1397,8 +1397,7 @@ impl CoreArchitecture {
}

pub fn by_name(name: &str) -> Option<Self> {
let handle =
unsafe { BNGetArchitectureByName(name.into_bytes_with_nul().as_ptr() as *mut _) };
let handle = unsafe { BNGetArchitectureByName(name.as_cstr().as_ptr()) };
match handle.is_null() {
false => Some(CoreArchitecture { handle }),
true => None,
Expand Down Expand Up @@ -1928,11 +1927,9 @@ macro_rules! cc_func {

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

fn register_by_name<S: AsCStr>(&self, name: S) -> Option<Self::Register> {
match unsafe {
BNGetArchitectureRegisterByName(self.as_ref().handle, name.as_ref().as_ptr() as *mut _)
BNGetArchitectureRegisterByName(self.as_ref().handle, name.as_cstr().as_ptr())
} {
0xffff_ffff => None,
reg => self.register_from_id(reg.into()),
Expand Down Expand Up @@ -2008,7 +2005,7 @@ pub trait ArchitectureExt: Architecture {

fn register_relocation_handler<S, R, F>(&self, name: S, func: F)
where
S: BnStrCompatible,
S: AsCStr,
R: 'static
+ RelocationHandler<Handle = CustomRelocationHandlerHandle<R>>
+ Send
Expand All @@ -2031,7 +2028,7 @@ impl<T: Architecture> ArchitectureExt for T {}

pub fn register_architecture<S, A, F>(name: S, func: F) -> &'static A
where
S: BnStrCompatible,
S: AsCStr,
A: 'static + Architecture<Handle = CustomArchitectureHandle<A>> + Send + Sync + Sized,
F: FnOnce(CustomArchitectureHandle<A>, CoreArchitecture) -> A,
{
Expand Down Expand Up @@ -3114,8 +3111,6 @@ where
custom_arch.skip_and_return_value(data, addr, val)
}

let name = name.into_bytes_with_nul();

let uninit_arch = ArchitectureBuilder {
arch: MaybeUninit::zeroed(),
func: Some(func),
Expand Down Expand Up @@ -3205,8 +3200,7 @@ where
};

unsafe {
let res =
BNRegisterArchitecture(name.as_ref().as_ptr() as *mut _, &mut custom_arch as *mut _);
let res = BNRegisterArchitecture(name.as_cstr().as_ptr(), &mut custom_arch as *mut _);

assert!(!res.is_null());

Expand Down
12 changes: 4 additions & 8 deletions rust/src/background_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ impl BackgroundTask {
Self { handle }
}

pub fn new<S: BnStrCompatible>(initial_text: S, can_cancel: bool) -> Ref<Self> {
let text = initial_text.into_bytes_with_nul();
let handle = unsafe { BNBeginBackgroundTask(text.as_ref().as_ptr() as *mut _, can_cancel) };
pub fn new<S: AsCStr>(initial_text: S, can_cancel: bool) -> Ref<Self> {
let handle = unsafe { BNBeginBackgroundTask(initial_text.as_cstr().as_ptr(), can_cancel) };
// We should always be returned a valid task.
assert!(!handle.is_null());
unsafe { Ref::new(Self { handle }) }
Expand Down Expand Up @@ -75,11 +74,8 @@ impl BackgroundTask {
unsafe { BnString::from_raw(BNGetBackgroundTaskProgressText(self.handle)) }
}

pub fn set_progress_text<S: BnStrCompatible>(&self, text: S) {
let progress_text = text.into_bytes_with_nul();
unsafe {
BNSetBackgroundTaskProgressText(self.handle, progress_text.as_ref().as_ptr() as *mut _)
}
pub fn set_progress_text<S: AsCStr>(&self, text: S) {
unsafe { BNSetBackgroundTaskProgressText(self.handle, text.as_cstr().as_ptr()) }
}

pub fn running_tasks() -> Array<BackgroundTask> {
Expand Down
Loading

0 comments on commit 193b836

Please sign in to comment.