Skip to content

Commit

Permalink
More rust cleanup
Browse files Browse the repository at this point in the history
- Add extra info to README.md
- Refactor components api
- Add components unit test
  • Loading branch information
emesare committed Jan 25, 2025
1 parent b495bbe commit 5cf6ee5
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 92 deletions.
2 changes: 2 additions & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ These bindings are still actively under development. Compatibility _will_ break
It is encouraged that you reference a specific commit to avoid having your plugin/application break when the API changes.
To specify a specific commit see the cargo documentation [here](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#choice-of-commit).

If you are worried about breaking changes avoid modules with warnings about instability!

**MSRV**: The Rust version specified in the `Cargo.toml`.

## Example
Expand Down
20 changes: 8 additions & 12 deletions rust/src/binary_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use binaryninjacore_sys::*;

use crate::architecture::{Architecture, CoreArchitecture};
use crate::basic_block::BasicBlock;
use crate::component::{Component, ComponentBuilder, IntoComponentGuid};
use crate::component::{Component, IntoComponentGuid};
use crate::confidence::Conf;
use crate::data_buffer::DataBuffer;
use crate::debuginfo::DebugInfo;
Expand Down Expand Up @@ -1422,39 +1422,35 @@ pub trait BinaryViewExt: BinaryViewBase {
.collect()
}

fn component_by_guid<S: BnStrCompatible>(&self, guid: S) -> Option<Component> {
fn component_by_guid<S: BnStrCompatible>(&self, guid: S) -> Option<Ref<Component>> {
let name = guid.into_bytes_with_nul();
let result = unsafe {
BNGetComponentByGuid(
self.as_ref().handle,
name.as_ref().as_ptr() as *const c_char,
)
};
NonNull::new(result).map(|h| unsafe { Component::from_raw(h) })
NonNull::new(result).map(|h| unsafe { Component::ref_from_raw(h) })
}

fn root_component(&self) -> Option<Component> {
fn root_component(&self) -> Option<Ref<Component>> {
let result = unsafe { BNGetRootComponent(self.as_ref().handle) };
NonNull::new(result).map(|h| unsafe { Component::from_raw(h) })
NonNull::new(result).map(|h| unsafe { Component::ref_from_raw(h) })
}

fn component_builder(&self) -> ComponentBuilder {
ComponentBuilder::new_from_raw(self.as_ref().handle)
}

fn component_by_path<P: BnStrCompatible>(&self, path: P) -> Option<Component> {
fn component_by_path<P: BnStrCompatible>(&self, path: P) -> Option<Ref<Component>> {
let path = path.into_bytes_with_nul();
let result = unsafe {
BNGetComponentByPath(
self.as_ref().handle,
path.as_ref().as_ptr() as *const c_char,
)
};
NonNull::new(result).map(|h| unsafe { Component::from_raw(h) })
NonNull::new(result).map(|h| unsafe { Component::ref_from_raw(h) })
}

fn remove_component(&self, component: &Component) -> bool {
unsafe { BNRemoveComponent(self.as_ref().handle, component.as_raw()) }
unsafe { BNRemoveComponent(self.as_ref().handle, component.handle.as_ptr()) }
}

fn remove_component_by_guid<P: IntoComponentGuid>(&self, guid: P) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions rust/src/collaboration.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! The collaboration API is **unstable** and as such will undergo breaking changes in the near future!
mod changeset;
mod file;
mod folder;
Expand Down
Loading

0 comments on commit 5cf6ee5

Please sign in to comment.