Skip to content

Commit

Permalink
Replace lazy_static crate with std::sync::LazyLock
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski committed Dec 15, 2024
1 parent b34ddd0 commit f0ca06b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 62 deletions.
1 change: 0 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ rust-version = "1.83.0"
noexports = []

[dependencies]
lazy_static = "1.4.0"
log = { version = "0.4", features = ["std"] }
rayon = { version = "1.8", optional = true }
binaryninjacore-sys = { path = "binaryninjacore-sys" }
Expand Down
114 changes: 53 additions & 61 deletions rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ use crate::{
symbol::Symbol,
};

use lazy_static::lazy_static;
use std::ptr::null_mut;
use std::{
borrow::{Borrow, Cow},
collections::{HashMap, HashSet},
Expand All @@ -43,7 +41,7 @@ use std::{
ops::Range,
os::raw::c_char,
ptr, result, slice,
sync::Mutex,
sync::LazyLock,
};

pub type Result<R> = result::Result<R, ()>;
Expand Down Expand Up @@ -1231,77 +1229,71 @@ impl fmt::Display for Type {
}
}

lazy_static! {
static ref TYPE_DEBUG_BV: Mutex<Option<Ref<BinaryView>>> =
Mutex::new(BinaryView::from_data(&FileMetadata::new(), &[]).ok());
}
static TYPE_DEBUG_BV: LazyLock<Option<Ref<BinaryView>>> =
LazyLock::new(|| BinaryView::from_data(&FileMetadata::new(), &[]).ok());

impl fmt::Debug for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Ok(lock) = TYPE_DEBUG_BV.lock() {
if let Some(bv) = &*lock {
let container = unsafe { BNGetAnalysisTypeContainer(bv.handle) };
let Some(bv) = &*TYPE_DEBUG_BV else {
return Err(fmt::Error);
};

let printer = if f.alternate() {
unsafe { BNGetTypePrinterByName(c"_DebugTypePrinter".as_ptr()) }
} else {
unsafe { BNGetTypePrinterByName(c"CoreTypePrinter".as_ptr()) }
};
if printer.is_null() {
return Err(fmt::Error);
}
let container = unsafe { BNGetAnalysisTypeContainer(bv.handle) };

let mut name = QualifiedName::from("");

let mut lines: *mut BNTypeDefinitionLine = null_mut();
let mut count: usize = 0;

unsafe {
BNGetTypePrinterTypeLines(
printer,
self.handle,
container,
&mut name.0,
64,
false,
BNTokenEscapingType::NoTokenEscapingType,
&mut lines,
&mut count,
)
};
unsafe {
BNFreeTypeContainer(container);
}
let printer = if f.alternate() {
unsafe { BNGetTypePrinterByName(c"_DebugTypePrinter".as_ptr()) }
} else {
unsafe { BNGetTypePrinterByName(c"CoreTypePrinter".as_ptr()) }
};
if printer.is_null() {
return Err(fmt::Error);
}

if lines.is_null() {
return Err(fmt::Error);
}
let mut name = QualifiedName::from("");

let line_slice: &[BNTypeDefinitionLine] =
unsafe { slice::from_raw_parts(lines, count) };
let mut lines = ptr::null_mut();
let mut count = 0;

for (i, line) in line_slice.iter().enumerate() {
if i > 0 {
writeln!(f)?;
}
unsafe {
BNGetTypePrinterTypeLines(
printer,
self.handle,
container,
&mut name.0,
64,
false,
BNTokenEscapingType::NoTokenEscapingType,
&mut lines,
&mut count,
)
};
unsafe {
BNFreeTypeContainer(container);
}

let tokens: &[BNInstructionTextToken] =
unsafe { slice::from_raw_parts(line.tokens, line.count) };
if lines.is_null() {
return Err(fmt::Error);
}

for token in tokens {
let text: *const c_char = token.text;
let str = unsafe { CStr::from_ptr(text) };
write!(f, "{}", str.to_string_lossy())?;
}
}
let line_slice = unsafe { slice::from_raw_parts(lines, count) };

unsafe {
BNFreeTypeDefinitionLineList(lines, count);
}
return Ok(());
for (i, line) in line_slice.iter().enumerate() {
if i > 0 {
writeln!(f)?;
}

let tokens = unsafe { slice::from_raw_parts(line.tokens, line.count) };

for token in tokens {
let text = unsafe { CStr::from_ptr(token.text) };
write!(f, "{}", text.to_string_lossy())?;
}
}
Err(fmt::Error)

unsafe {
BNFreeTypeDefinitionLineList(lines, count);
}
Ok(())
}
}

Expand Down

0 comments on commit f0ca06b

Please sign in to comment.