Skip to content

Commit d3a36c6

Browse files
committed
Misc cleanup in Rust API
1 parent 38a175c commit d3a36c6

File tree

3 files changed

+26
-35
lines changed

3 files changed

+26
-35
lines changed

rust/src/function.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,7 @@ impl Function {
485485
language: &str,
486486
) -> Option<Ref<CoreLanguageRepresentationFunction>> {
487487
let lang_name = language.to_cstr();
488-
let repr = unsafe {
489-
BNGetFunctionLanguageRepresentation(
490-
self.handle,
491-
lang_name.as_ptr(),
492-
)
493-
};
488+
let repr = unsafe { BNGetFunctionLanguageRepresentation(self.handle, lang_name.as_ptr()) };
494489
NonNull::new(repr)
495490
.map(|handle| unsafe { CoreLanguageRepresentationFunction::ref_from_raw(handle) })
496491
}
@@ -504,10 +499,7 @@ impl Function {
504499
) -> Option<Ref<CoreLanguageRepresentationFunction>> {
505500
let lang_name = language.to_cstr();
506501
let repr = unsafe {
507-
BNGetFunctionLanguageRepresentationIfAvailable(
508-
self.handle,
509-
lang_name.as_ptr(),
510-
)
502+
BNGetFunctionLanguageRepresentationIfAvailable(self.handle, lang_name.as_ptr())
511503
};
512504
NonNull::new(repr)
513505
.map(|handle| unsafe { CoreLanguageRepresentationFunction::ref_from_raw(handle) })

rust/src/string.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414

1515
//! String wrappers for core-owned strings and strings being passed to the core
1616
17-
use crate::rc::*;
18-
use crate::type_archive::TypeArchiveSnapshotId;
19-
use crate::types::QualifiedName;
17+
use binaryninjacore_sys::*;
2018
use std::borrow::Cow;
2119
use std::ffi::{c_char, CStr, CString};
2220
use std::fmt;
@@ -25,6 +23,10 @@ use std::mem;
2523
use std::ops::Deref;
2624
use std::path::{Path, PathBuf};
2725

26+
use crate::rc::*;
27+
use crate::type_archive::TypeArchiveSnapshotId;
28+
use crate::types::QualifiedName;
29+
2830
// TODO: Remove or refactor this.
2931
pub(crate) fn raw_to_string(ptr: *const c_char) -> Option<String> {
3032
if ptr.is_null() {
@@ -66,8 +68,7 @@ pub struct BnString {
6668
}
6769

6870
impl BnString {
69-
pub fn new<S: IntoCStr>(s: S) -> Self {
70-
use binaryninjacore_sys::BNAllocString;
71+
pub fn new(s: impl IntoCStr) -> Self {
7172
let raw = s.to_cstr();
7273
unsafe { Self::from_raw(BNAllocString(raw.as_ptr())) }
7374
}
@@ -79,14 +80,13 @@ impl BnString {
7980
Self::from_raw(raw).to_string_lossy().to_string()
8081
}
8182

82-
/// Construct a BnString from an owned const char* allocated by BNAllocString
83+
/// Construct a BnString from an owned const char* allocated by [`BNAllocString`].
8384
pub(crate) unsafe fn from_raw(raw: *mut c_char) -> Self {
8485
Self { raw }
8586
}
8687

87-
/// Free a raw string allocated by BNAllocString.
88+
/// Free a raw string allocated by [`BNAllocString`].
8889
pub(crate) unsafe fn free_raw(raw: *mut c_char) {
89-
use binaryninjacore_sys::BNFreeString;
9090
if !raw.is_null() {
9191
BNFreeString(raw);
9292
}
@@ -115,7 +115,6 @@ impl Drop for BnString {
115115

116116
impl Clone for BnString {
117117
fn clone(&self) -> Self {
118-
use binaryninjacore_sys::BNAllocString;
119118
unsafe {
120119
Self {
121120
raw: BNAllocString(self.raw),
@@ -178,7 +177,6 @@ impl CoreArrayProvider for BnString {
178177

179178
unsafe impl CoreArrayProviderInner for BnString {
180179
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
181-
use binaryninjacore_sys::BNFreeStringList;
182180
BNFreeStringList(raw, count);
183181
}
184182

@@ -187,101 +185,101 @@ unsafe impl CoreArrayProviderInner for BnString {
187185
}
188186
}
189187

190-
pub unsafe trait IntoCStr {
188+
pub trait IntoCStr {
191189
type Result: Deref<Target = CStr>;
192190

193191
fn to_cstr(self) -> Self::Result;
194192
}
195193

196-
unsafe impl IntoCStr for &CStr {
194+
impl IntoCStr for &CStr {
197195
type Result = Self;
198196

199197
fn to_cstr(self) -> Self::Result {
200198
self
201199
}
202200
}
203201

204-
unsafe impl IntoCStr for BnString {
202+
impl IntoCStr for BnString {
205203
type Result = Self;
206204

207205
fn to_cstr(self) -> Self::Result {
208206
self
209207
}
210208
}
211209

212-
unsafe impl IntoCStr for &BnString {
210+
impl IntoCStr for &BnString {
213211
type Result = BnString;
214212

215213
fn to_cstr(self) -> Self::Result {
216214
self.clone()
217215
}
218216
}
219217

220-
unsafe impl IntoCStr for CString {
218+
impl IntoCStr for CString {
221219
type Result = Self;
222220

223221
fn to_cstr(self) -> Self::Result {
224222
self
225223
}
226224
}
227225

228-
unsafe impl IntoCStr for &str {
226+
impl IntoCStr for &str {
229227
type Result = CString;
230228

231229
fn to_cstr(self) -> Self::Result {
232230
CString::new(self).expect("can't pass strings with internal nul bytes to core!")
233231
}
234232
}
235233

236-
unsafe impl IntoCStr for String {
234+
impl IntoCStr for String {
237235
type Result = CString;
238236

239237
fn to_cstr(self) -> Self::Result {
240238
CString::new(self).expect("can't pass strings with internal nul bytes to core!")
241239
}
242240
}
243241

244-
unsafe impl IntoCStr for &String {
242+
impl IntoCStr for &String {
245243
type Result = CString;
246244

247245
fn to_cstr(self) -> Self::Result {
248246
self.clone().to_cstr()
249247
}
250248
}
251249

252-
unsafe impl<'a> IntoCStr for &'a Cow<'a, str> {
250+
impl<'a> IntoCStr for &'a Cow<'a, str> {
253251
type Result = CString;
254252

255253
fn to_cstr(self) -> Self::Result {
256254
self.to_string().to_cstr()
257255
}
258256
}
259257

260-
unsafe impl IntoCStr for Cow<'_, str> {
258+
impl IntoCStr for Cow<'_, str> {
261259
type Result = CString;
262260

263261
fn to_cstr(self) -> Self::Result {
264262
self.to_string().to_cstr()
265263
}
266264
}
267265

268-
unsafe impl IntoCStr for &QualifiedName {
266+
impl IntoCStr for &QualifiedName {
269267
type Result = CString;
270268

271269
fn to_cstr(self) -> Self::Result {
272270
self.to_string().to_cstr()
273271
}
274272
}
275273

276-
unsafe impl IntoCStr for PathBuf {
274+
impl IntoCStr for PathBuf {
277275
type Result = CString;
278276

279277
fn to_cstr(self) -> Self::Result {
280278
self.as_path().to_cstr()
281279
}
282280
}
283281

284-
unsafe impl IntoCStr for &Path {
282+
impl IntoCStr for &Path {
285283
type Result = CString;
286284

287285
fn to_cstr(self) -> Self::Result {
@@ -290,7 +288,7 @@ unsafe impl IntoCStr for &Path {
290288
}
291289
}
292290

293-
unsafe impl IntoCStr for TypeArchiveSnapshotId {
291+
impl IntoCStr for TypeArchiveSnapshotId {
294292
type Result = CString;
295293

296294
fn to_cstr(self) -> Self::Result {

rust/tests/line_formatter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ fn test_custom_line_formatter() {
2020
let _session = Session::new().expect("Failed to initialize session");
2121
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
2222
let line_formatter = register_line_formatter("my_line_formatter", MyLineFormatter {});
23-
assert_eq!(line_formatter.name().as_str(), "my_line_formatter");
23+
assert_eq!(line_formatter.name(), "my_line_formatter".into());
24+
// TODO: Finish this test.
2425
}

0 commit comments

Comments
 (0)