summaryrefslogtreecommitdiff
path: root/rust/src
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2025-05-08 21:01:42 -0400
committerMason Reed <35282038+emesare@users.noreply.github.com>2025-05-12 17:45:24 -0400
commit38fa66115c00ead8dcc38ec5c5451d3ce4444ab8 (patch)
treedd398b7bbb99c0eefcfdf559f8efcfcd532c1d25 /rust/src
parentc2d90f2e5a010546d0e1f1deeb1a6d8531ea4d4f (diff)
[Rust] Misc cleanup
Diffstat (limited to 'rust/src')
-rw-r--r--rust/src/function.rs12
-rw-r--r--rust/src/string.rs46
2 files changed, 24 insertions, 34 deletions
diff --git a/rust/src/function.rs b/rust/src/function.rs
index e7e5e9b0..92538384 100644
--- a/rust/src/function.rs
+++ b/rust/src/function.rs
@@ -485,12 +485,7 @@ impl Function {
language: &str,
) -> Option<Ref<CoreLanguageRepresentationFunction>> {
let lang_name = language.to_cstr();
- let repr = unsafe {
- BNGetFunctionLanguageRepresentation(
- self.handle,
- lang_name.as_ptr(),
- )
- };
+ let repr = unsafe { BNGetFunctionLanguageRepresentation(self.handle, lang_name.as_ptr()) };
NonNull::new(repr)
.map(|handle| unsafe { CoreLanguageRepresentationFunction::ref_from_raw(handle) })
}
@@ -504,10 +499,7 @@ impl Function {
) -> Option<Ref<CoreLanguageRepresentationFunction>> {
let lang_name = language.to_cstr();
let repr = unsafe {
- BNGetFunctionLanguageRepresentationIfAvailable(
- self.handle,
- lang_name.as_ptr(),
- )
+ BNGetFunctionLanguageRepresentationIfAvailable(self.handle, lang_name.as_ptr())
};
NonNull::new(repr)
.map(|handle| unsafe { CoreLanguageRepresentationFunction::ref_from_raw(handle) })
diff --git a/rust/src/string.rs b/rust/src/string.rs
index 97c31270..6083d300 100644
--- a/rust/src/string.rs
+++ b/rust/src/string.rs
@@ -14,9 +14,7 @@
//! String wrappers for core-owned strings and strings being passed to the core
-use crate::rc::*;
-use crate::type_archive::TypeArchiveSnapshotId;
-use crate::types::QualifiedName;
+use binaryninjacore_sys::*;
use std::borrow::Cow;
use std::ffi::{c_char, CStr, CString};
use std::fmt;
@@ -25,6 +23,10 @@ use std::mem;
use std::ops::Deref;
use std::path::{Path, PathBuf};
+use crate::rc::*;
+use crate::type_archive::TypeArchiveSnapshotId;
+use crate::types::QualifiedName;
+
// TODO: Remove or refactor this.
pub(crate) fn raw_to_string(ptr: *const c_char) -> Option<String> {
if ptr.is_null() {
@@ -66,8 +68,7 @@ pub struct BnString {
}
impl BnString {
- pub fn new<S: IntoCStr>(s: S) -> Self {
- use binaryninjacore_sys::BNAllocString;
+ pub fn new(s: impl IntoCStr) -> Self {
let raw = s.to_cstr();
unsafe { Self::from_raw(BNAllocString(raw.as_ptr())) }
}
@@ -79,14 +80,13 @@ impl BnString {
Self::from_raw(raw).to_string_lossy().to_string()
}
- /// Construct a BnString from an owned const char* allocated by BNAllocString
+ /// Construct a BnString from an owned const char* allocated by [`BNAllocString`].
pub(crate) unsafe fn from_raw(raw: *mut c_char) -> Self {
Self { raw }
}
- /// Free a raw string allocated by BNAllocString.
+ /// Free a raw string allocated by [`BNAllocString`].
pub(crate) unsafe fn free_raw(raw: *mut c_char) {
- use binaryninjacore_sys::BNFreeString;
if !raw.is_null() {
BNFreeString(raw);
}
@@ -115,7 +115,6 @@ impl Drop for BnString {
impl Clone for BnString {
fn clone(&self) -> Self {
- use binaryninjacore_sys::BNAllocString;
unsafe {
Self {
raw: BNAllocString(self.raw),
@@ -178,7 +177,6 @@ impl CoreArrayProvider for BnString {
unsafe impl CoreArrayProviderInner for BnString {
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
- use binaryninjacore_sys::BNFreeStringList;
BNFreeStringList(raw, count);
}
@@ -187,13 +185,13 @@ unsafe impl CoreArrayProviderInner for BnString {
}
}
-pub unsafe trait IntoCStr {
+pub trait IntoCStr {
type Result: Deref<Target = CStr>;
fn to_cstr(self) -> Self::Result;
}
-unsafe impl IntoCStr for &CStr {
+impl IntoCStr for &CStr {
type Result = Self;
fn to_cstr(self) -> Self::Result {
@@ -201,7 +199,7 @@ unsafe impl IntoCStr for &CStr {
}
}
-unsafe impl IntoCStr for BnString {
+impl IntoCStr for BnString {
type Result = Self;
fn to_cstr(self) -> Self::Result {
@@ -209,7 +207,7 @@ unsafe impl IntoCStr for BnString {
}
}
-unsafe impl IntoCStr for &BnString {
+impl IntoCStr for &BnString {
type Result = BnString;
fn to_cstr(self) -> Self::Result {
@@ -217,7 +215,7 @@ unsafe impl IntoCStr for &BnString {
}
}
-unsafe impl IntoCStr for CString {
+impl IntoCStr for CString {
type Result = Self;
fn to_cstr(self) -> Self::Result {
@@ -225,7 +223,7 @@ unsafe impl IntoCStr for CString {
}
}
-unsafe impl IntoCStr for &str {
+impl IntoCStr for &str {
type Result = CString;
fn to_cstr(self) -> Self::Result {
@@ -233,7 +231,7 @@ unsafe impl IntoCStr for &str {
}
}
-unsafe impl IntoCStr for String {
+impl IntoCStr for String {
type Result = CString;
fn to_cstr(self) -> Self::Result {
@@ -241,7 +239,7 @@ unsafe impl IntoCStr for String {
}
}
-unsafe impl IntoCStr for &String {
+impl IntoCStr for &String {
type Result = CString;
fn to_cstr(self) -> Self::Result {
@@ -249,7 +247,7 @@ unsafe impl IntoCStr for &String {
}
}
-unsafe impl<'a> IntoCStr for &'a Cow<'a, str> {
+impl<'a> IntoCStr for &'a Cow<'a, str> {
type Result = CString;
fn to_cstr(self) -> Self::Result {
@@ -257,7 +255,7 @@ unsafe impl<'a> IntoCStr for &'a Cow<'a, str> {
}
}
-unsafe impl IntoCStr for Cow<'_, str> {
+impl IntoCStr for Cow<'_, str> {
type Result = CString;
fn to_cstr(self) -> Self::Result {
@@ -265,7 +263,7 @@ unsafe impl IntoCStr for Cow<'_, str> {
}
}
-unsafe impl IntoCStr for &QualifiedName {
+impl IntoCStr for &QualifiedName {
type Result = CString;
fn to_cstr(self) -> Self::Result {
@@ -273,7 +271,7 @@ unsafe impl IntoCStr for &QualifiedName {
}
}
-unsafe impl IntoCStr for PathBuf {
+impl IntoCStr for PathBuf {
type Result = CString;
fn to_cstr(self) -> Self::Result {
@@ -281,7 +279,7 @@ unsafe impl IntoCStr for PathBuf {
}
}
-unsafe impl IntoCStr for &Path {
+impl IntoCStr for &Path {
type Result = CString;
fn to_cstr(self) -> Self::Result {
@@ -290,7 +288,7 @@ unsafe impl IntoCStr for &Path {
}
}
-unsafe impl IntoCStr for TypeArchiveSnapshotId {
+impl IntoCStr for TypeArchiveSnapshotId {
type Result = CString;
fn to_cstr(self) -> Self::Result {