Skip to content

Commit 5b988a6

Browse files
committed
Remove FlowGraphNode lifetime in Rust API
1 parent b63319b commit 5b988a6

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

rust/src/flowgraph.rs

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616
1717
use crate::disassembly::DisassemblyTextLine;
1818
use binaryninjacore_sys::*;
19-
use std::slice;
2019

2120
use crate::rc::*;
2221

2322
use crate::basic_block::{BasicBlock, BlockContext};
2423
use crate::function::HighlightColor;
2524
use crate::render_layer::CoreRenderLayer;
26-
use std::marker::PhantomData;
2725

2826
pub type BranchType = BNBranchType;
2927
pub type EdgePenStyle = BNEdgePenStyle;
@@ -48,30 +46,18 @@ impl FlowGraph {
4846
unsafe { FlowGraph::ref_from_raw(BNCreateFlowGraph()) }
4947
}
5048

51-
pub fn nodes<'a>(&self) -> Vec<Ref<FlowGraphNode<'a>>> {
49+
pub fn nodes(&self) -> Array<FlowGraphNode> {
5250
let mut count: usize = 0;
5351
let nodes_ptr = unsafe { BNGetFlowGraphNodes(self.handle, &mut count as *mut usize) };
54-
55-
let nodes = unsafe { slice::from_raw_parts_mut(nodes_ptr, count) };
56-
57-
let mut result = vec![];
58-
result.reserve(count);
59-
60-
for i in 0..count {
61-
result.push(unsafe { RefCountable::inc_ref(&FlowGraphNode::from_raw(nodes[i])) });
62-
}
63-
64-
unsafe { BNFreeFlowGraphNodeList(nodes_ptr, count) };
65-
66-
result
52+
unsafe { Array::new(nodes_ptr, count, ()) }
6753
}
6854

69-
pub fn get_node<'a>(&self, i: usize) -> Option<Ref<FlowGraphNode<'a>>> {
55+
pub fn get_node(&self, i: usize) -> Option<Ref<FlowGraphNode>> {
7056
let node_ptr = unsafe { BNGetFlowGraphNode(self.handle, i) };
7157
if node_ptr.is_null() {
7258
None
7359
} else {
74-
Some(unsafe { Ref::new(FlowGraphNode::from_raw(node_ptr)) })
60+
Some(unsafe { FlowGraphNode::ref_from_raw(node_ptr) })
7561
}
7662
}
7763

@@ -146,24 +132,17 @@ impl ToOwned for FlowGraph {
146132
}
147133

148134
#[derive(PartialEq, Eq, Hash)]
149-
pub struct FlowGraphNode<'a> {
135+
pub struct FlowGraphNode {
150136
pub(crate) handle: *mut BNFlowGraphNode,
151-
_data: PhantomData<&'a ()>,
152137
}
153138

154-
impl<'a> FlowGraphNode<'a> {
139+
impl FlowGraphNode {
155140
pub(crate) unsafe fn from_raw(raw: *mut BNFlowGraphNode) -> Self {
156-
Self {
157-
handle: raw,
158-
_data: PhantomData,
159-
}
141+
Self { handle: raw }
160142
}
161143

162144
pub(crate) unsafe fn ref_from_raw(raw: *mut BNFlowGraphNode) -> Ref<Self> {
163-
Ref::new(Self {
164-
handle: raw,
165-
_data: PhantomData,
166-
})
145+
Ref::new(Self { handle: raw })
167146
}
168147

169148
pub fn new(graph: &FlowGraph) -> Ref<Self> {
@@ -233,7 +212,7 @@ impl<'a> FlowGraphNode<'a> {
233212
pub fn add_outgoing_edge(
234213
&self,
235214
type_: BranchType,
236-
target: &'a FlowGraphNode,
215+
target: &FlowGraphNode,
237216
edge_style: EdgeStyle,
238217
) {
239218
unsafe {
@@ -242,11 +221,10 @@ impl<'a> FlowGraphNode<'a> {
242221
}
243222
}
244223

245-
unsafe impl RefCountable for FlowGraphNode<'_> {
224+
unsafe impl RefCountable for FlowGraphNode {
246225
unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
247226
Ref::new(Self {
248227
handle: BNNewFlowGraphNodeReference(handle.handle),
249-
_data: PhantomData,
250228
})
251229
}
252230

@@ -255,14 +233,30 @@ unsafe impl RefCountable for FlowGraphNode<'_> {
255233
}
256234
}
257235

258-
impl ToOwned for FlowGraphNode<'_> {
236+
impl ToOwned for FlowGraphNode {
259237
type Owned = Ref<Self>;
260238

261239
fn to_owned(&self) -> Self::Owned {
262240
unsafe { RefCountable::inc_ref(self) }
263241
}
264242
}
265243

244+
impl CoreArrayProvider for FlowGraphNode {
245+
type Raw = *mut BNFlowGraphNode;
246+
type Context = ();
247+
type Wrapped<'a> = Guard<'a, FlowGraphNode>;
248+
}
249+
250+
unsafe impl CoreArrayProviderInner for FlowGraphNode {
251+
unsafe fn free(raw: *mut Self::Raw, count: usize, _: &Self::Context) {
252+
BNFreeFlowGraphNodeList(raw, count);
253+
}
254+
255+
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
256+
Guard::new(Self::from_raw(*raw), context)
257+
}
258+
}
259+
266260
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
267261
pub struct EdgeStyle {
268262
style: EdgePenStyle,

0 commit comments

Comments
 (0)