Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace BnStrCompatible with AsCStr trait #5897

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1945,11 +1944,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 @@ -2025,7 +2022,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 @@ -2048,7 +2045,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 @@ -3131,8 +3128,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 @@ -3222,8 +3217,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