Skip to content

Commit 17deaaa

Browse files
committed
Remove IntoActivityName trait
1 parent b34ddd0 commit 17deaaa

File tree

1 file changed

+75
-60
lines changed

1 file changed

+75
-60
lines changed

rust/src/workflow.rs

Lines changed: 75 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use std::ptr::NonNull;
44

55
use crate::architecture::CoreArchitecture;
66
use crate::basicblock::BasicBlock;
7+
use crate::binaryview::BinaryView;
78
use crate::flowgraph::FlowGraph;
89
use crate::function::{Function, NativeBlock};
910
use crate::llil::{self, FunctionForm, Mutable};
1011
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable};
1112
use crate::string::{BnStrCompatible, BnString};
1213
use crate::{hlil, mlil};
13-
use crate::binaryview::BinaryView;
1414

1515
#[repr(transparent)]
1616
/// The AnalysisContext struct is used to represent the current state of
@@ -236,22 +236,6 @@ unsafe impl RefCountable for Activity {
236236
}
237237
}
238238

239-
pub trait IntoActivityName {
240-
fn activity_name(self) -> BnString;
241-
}
242-
243-
impl IntoActivityName for &Activity {
244-
fn activity_name(self) -> BnString {
245-
self.name()
246-
}
247-
}
248-
249-
impl<S: BnStrCompatible> IntoActivityName for S {
250-
fn activity_name(self) -> BnString {
251-
BnString::new(self)
252-
}
253-
}
254-
255239
// TODO: We need to hide the JSON here behind a sensible/typed API.
256240
#[repr(transparent)]
257241
pub struct Workflow {
@@ -289,12 +273,12 @@ impl Workflow {
289273
/// * `name` - the name for the new [Workflow]
290274
/// * `root_activity` - perform the clone operation with this activity as the root
291275
#[must_use]
292-
pub fn new_from_copy_with_root<S: BnStrCompatible + Clone, A: IntoActivityName>(
276+
pub fn new_from_copy_with_root<S: BnStrCompatible + Clone, A: BnStrCompatible>(
293277
name: S,
294278
root_activity: A,
295279
) -> Workflow {
296280
let raw_name = name.clone().into_bytes_with_nul();
297-
let activity = root_activity.activity_name();
281+
let activity = root_activity.into_bytes_with_nul();
298282
// I can't think of a single reason as to why we should let users pass a workflow handle into this.
299283
// To prevent warning being emitted we default to the name.
300284
let placeholder_workflow = Workflow::instance(name);
@@ -366,14 +350,16 @@ impl Workflow {
366350
) -> Result<Activity, ()>
367351
where
368352
I: IntoIterator,
369-
I::Item: IntoActivityName,
353+
I::Item: BnStrCompatible,
370354
{
371-
let subactivities_raw: Vec<BnString> = subactivities
355+
let subactivities_raw: Vec<_> = subactivities
372356
.into_iter()
373-
.map(|x| x.activity_name())
357+
.map(|x| x.into_bytes_with_nul())
358+
.collect();
359+
let mut subactivities_ptr: Vec<*const _> = subactivities_raw
360+
.iter()
361+
.map(|x| x.as_ref().as_ptr() as *const _)
374362
.collect();
375-
let mut subactivities_ptr: Vec<*const _> =
376-
subactivities_raw.iter().map(|x| x.as_ptr()).collect();
377363
let result = unsafe {
378364
BNWorkflowRegisterActivity(
379365
self.handle.as_ptr(),
@@ -387,17 +373,26 @@ impl Workflow {
387373
}
388374

389375
/// Determine if an Activity exists in this [Workflow].
390-
pub fn contains<A: IntoActivityName>(&self, activity: A) -> bool {
391-
unsafe { BNWorkflowContains(self.handle.as_ptr(), activity.activity_name().as_ptr()) }
376+
pub fn contains<A: BnStrCompatible>(&self, activity: A) -> bool {
377+
unsafe {
378+
BNWorkflowContains(
379+
self.handle.as_ptr(),
380+
activity.into_bytes_with_nul().as_ref().as_ptr() as *const _,
381+
)
382+
}
392383
}
393384

394385
/// Retrieve the configuration as an adjacency list in JSON for the
395386
/// [Workflow], or if specified just for the given `activity`.
396387
///
397388
/// `activity` - if specified, return the configuration for the `activity`
398-
pub fn configuration<A: IntoActivityName>(&self, activity: A) -> BnString {
399-
let result =
400-
unsafe { BNWorkflowGetConfiguration(self.handle.as_ptr(), activity.activity_name().as_ptr()) };
389+
pub fn configuration<A: BnStrCompatible>(&self, activity: A) -> BnString {
390+
let result = unsafe {
391+
BNWorkflowGetConfiguration(
392+
self.handle.as_ptr(),
393+
activity.into_bytes_with_nul().as_ref().as_ptr() as *const _,
394+
)
395+
};
401396
assert!(!result.is_null());
402397
unsafe { BnString::from_raw(result) }
403398
}
@@ -424,10 +419,14 @@ impl Workflow {
424419
/// specified just for the given `activity`.
425420
///
426421
/// * `activity` - if specified, return the roots for the `activity`
427-
pub fn activity_roots<A: IntoActivityName>(&self, activity: A) -> Array<BnString> {
422+
pub fn activity_roots<A: BnStrCompatible>(&self, activity: A) -> Array<BnString> {
428423
let mut count = 0;
429424
let result = unsafe {
430-
BNWorkflowGetActivityRoots(self.handle.as_ptr(), activity.activity_name().as_ptr(), &mut count)
425+
BNWorkflowGetActivityRoots(
426+
self.handle.as_ptr(),
427+
activity.into_bytes_with_nul().as_ref().as_ptr() as *const _,
428+
&mut count,
429+
)
431430
};
432431
assert!(!result.is_null());
433432
unsafe { Array::new(result as *mut *mut c_char, count, ()) }
@@ -437,7 +436,7 @@ impl Workflow {
437436
///
438437
/// * `activity` - if specified, return the direct children and optionally the descendants of the `activity` (includes `activity`)
439438
/// * `immediate` - whether to include only direct children of `activity` or all descendants
440-
pub fn subactivities<A: IntoActivityName>(
439+
pub fn subactivities<A: BnStrCompatible>(
441440
&self,
442441
activity: A,
443442
immediate: bool,
@@ -446,7 +445,7 @@ impl Workflow {
446445
let result = unsafe {
447446
BNWorkflowGetSubactivities(
448447
self.handle.as_ptr(),
449-
activity.activity_name().as_ptr(),
448+
activity.into_bytes_with_nul().as_ref().as_ptr() as *const _,
450449
immediate,
451450
&mut count,
452451
)
@@ -461,20 +460,23 @@ impl Workflow {
461460
/// * `activities` - the list of Activities to assign
462461
pub fn assign_subactivities<A, I>(&self, activity: A, activities: I) -> bool
463462
where
464-
A: IntoActivityName,
463+
A: BnStrCompatible,
465464
I: IntoIterator,
466-
I::Item: IntoActivityName,
465+
I::Item: BnStrCompatible,
467466
{
468-
let mut input_list: Vec<BnString> =
469-
activities.into_iter().map(|a| a.activity_name()).collect();
470-
// SAFETY: this works because BnString and *mut ffi::c_char are
471-
// transmutable
472-
let input_list_ptr = input_list.as_mut_ptr() as *mut *const c_char;
467+
let input_list: Vec<_> = activities
468+
.into_iter()
469+
.map(|a| a.into_bytes_with_nul())
470+
.collect();
471+
let mut input_list: Vec<_> = input_list
472+
.into_iter()
473+
.map(|s| s.as_ref().as_ptr() as *const _)
474+
.collect();
473475
unsafe {
474476
BNWorkflowAssignSubactivities(
475477
self.handle.as_ptr(),
476-
activity.activity_name().as_ptr(),
477-
input_list_ptr,
478+
activity.into_bytes_with_nul().as_ref().as_ptr() as *const _,
479+
input_list.as_mut_ptr(),
478480
input_list.len(),
479481
)
480482
}
@@ -491,44 +493,52 @@ impl Workflow {
491493
/// * `activities` - the list of Activities to insert
492494
pub fn insert<A, I>(&self, activity: A, activities: I) -> bool
493495
where
494-
A: IntoActivityName,
496+
A: BnStrCompatible,
495497
I: IntoIterator,
496-
I::Item: IntoActivityName,
498+
I::Item: BnStrCompatible,
497499
{
498-
let mut input_list: Vec<BnString> =
499-
activities.into_iter().map(|a| a.activity_name()).collect();
500-
// SAFETY: this works because BnString and *mut ffi::c_char are
501-
// transmutable
502-
let input_list_ptr = input_list.as_mut_ptr() as *mut *const c_char;
500+
let input_list: Vec<_> = activities
501+
.into_iter()
502+
.map(|a| a.into_bytes_with_nul())
503+
.collect();
504+
let mut input_list: Vec<_> = input_list
505+
.into_iter()
506+
.map(|s| s.as_ref().as_ptr() as *const _)
507+
.collect();
503508
unsafe {
504509
BNWorkflowInsert(
505510
self.handle.as_ptr(),
506-
activity.activity_name().as_ptr(),
507-
input_list_ptr,
511+
activity.into_bytes_with_nul().as_ref().as_ptr() as *const _,
512+
input_list.as_mut_ptr(),
508513
input_list.len(),
509514
)
510515
}
511516
}
512517

513518
/// Remove the specified `activity`
514-
pub fn remove<A: IntoActivityName>(&self, activity: A) -> bool {
515-
unsafe { BNWorkflowRemove(self.handle.as_ptr(), activity.activity_name().as_ptr()) }
519+
pub fn remove<A: BnStrCompatible>(&self, activity: A) -> bool {
520+
unsafe {
521+
BNWorkflowRemove(
522+
self.handle.as_ptr(),
523+
activity.into_bytes_with_nul().as_ref().as_ptr() as *const _,
524+
)
525+
}
516526
}
517527

518528
/// Replace the specified `activity`.
519529
///
520530
/// * `activity` - the Activity to replace
521531
/// * `new_activity` - the replacement Activity
522-
pub fn replace<A: IntoActivityName, N: IntoActivityName>(
532+
pub fn replace<A: BnStrCompatible, N: BnStrCompatible>(
523533
&self,
524534
activity: A,
525535
new_activity: N,
526536
) -> bool {
527537
unsafe {
528538
BNWorkflowReplace(
529539
self.handle.as_ptr(),
530-
activity.activity_name().as_ptr(),
531-
new_activity.activity_name().as_ptr(),
540+
activity.into_bytes_with_nul().as_ref().as_ptr() as *const _,
541+
new_activity.into_bytes_with_nul().as_ref().as_ptr() as *const _,
532542
)
533543
}
534544
}
@@ -537,15 +547,20 @@ impl Workflow {
537547
///
538548
/// * `activity` - if specified, generate the Flowgraph using `activity` as the root
539549
/// * `sequential` - whether to generate a **Composite** or **Sequential** style graph
540-
pub fn graph<A: IntoActivityName>(
550+
pub fn graph<A: BnStrCompatible>(
541551
&self,
542552
activity: A,
543553
sequential: Option<bool>,
544554
) -> Option<FlowGraph> {
545555
let sequential = sequential.unwrap_or(false);
546-
let activity_name = activity.activity_name();
547-
let graph =
548-
unsafe { BNWorkflowGetGraph(self.handle.as_ptr(), activity_name.as_ptr(), sequential) };
556+
let activity_name = activity.into_bytes_with_nul();
557+
let graph = unsafe {
558+
BNWorkflowGetGraph(
559+
self.handle.as_ptr(),
560+
activity_name.as_ref().as_ptr() as *const _,
561+
sequential,
562+
)
563+
};
549564
if graph.is_null() {
550565
return None;
551566
}

0 commit comments

Comments
 (0)