Skip to content

Commit df6f409

Browse files
committed
Replace lazy_static crate with std::sync::LazyLock
1 parent d36b962 commit df6f409

2 files changed

Lines changed: 53 additions & 62 deletions

File tree

rust/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ rust-version = "1.80.0"
99
noexports = []
1010

1111
[dependencies]
12-
lazy_static = "1.4.0"
1312
log = "0.4"
1413
rayon = { version = "1.8", optional = true }
1514
binaryninjacore-sys = { path = "binaryninjacore-sys" }

rust/src/types.rs

Lines changed: 53 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ use crate::{
3131
symbol::Symbol,
3232
};
3333

34-
use lazy_static::lazy_static;
35-
use std::ptr::null_mut;
3634
use std::{
3735
borrow::{Borrow, Cow},
3836
collections::{HashMap, HashSet},
@@ -44,7 +42,7 @@ use std::{
4442
ops::Range,
4543
os::raw::c_char,
4644
ptr, result, slice,
47-
sync::Mutex,
45+
sync::LazyLock,
4846
};
4947

5048
pub type Result<R> = result::Result<R, ()>;
@@ -1232,77 +1230,71 @@ impl fmt::Display for Type {
12321230
}
12331231
}
12341232

1235-
lazy_static! {
1236-
static ref TYPE_DEBUG_BV: Mutex<Option<Ref<BinaryView>>> =
1237-
Mutex::new(BinaryView::from_data(&FileMetadata::new(), &[]).ok());
1238-
}
1233+
static TYPE_DEBUG_BV: LazyLock<Option<Ref<BinaryView>>> =
1234+
LazyLock::new(|| BinaryView::from_data(&FileMetadata::new(), &[]).ok());
12391235

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

1246-
let printer = if f.alternate() {
1247-
unsafe { BNGetTypePrinterByName(c"_DebugTypePrinter".as_ptr()) }
1248-
} else {
1249-
unsafe { BNGetTypePrinterByName(c"CoreTypePrinter".as_ptr()) }
1250-
};
1251-
if printer.is_null() {
1252-
return Err(fmt::Error);
1253-
}
1242+
let container = unsafe { BNGetAnalysisTypeContainer(bv.handle) };
12541243

1255-
let mut name = QualifiedName::from("");
1256-
1257-
let mut lines: *mut BNTypeDefinitionLine = null_mut();
1258-
let mut count: usize = 0;
1259-
1260-
unsafe {
1261-
BNGetTypePrinterTypeLines(
1262-
printer,
1263-
self.handle,
1264-
container,
1265-
&mut name.0,
1266-
64,
1267-
false,
1268-
BNTokenEscapingType::NoTokenEscapingType,
1269-
&mut lines,
1270-
&mut count,
1271-
)
1272-
};
1273-
unsafe {
1274-
BNFreeTypeContainer(container);
1275-
}
1244+
let printer = if f.alternate() {
1245+
unsafe { BNGetTypePrinterByName(c"_DebugTypePrinter".as_ptr()) }
1246+
} else {
1247+
unsafe { BNGetTypePrinterByName(c"CoreTypePrinter".as_ptr()) }
1248+
};
1249+
if printer.is_null() {
1250+
return Err(fmt::Error);
1251+
}
12761252

1277-
if lines.is_null() {
1278-
return Err(fmt::Error);
1279-
}
1253+
let mut name = QualifiedName::from("");
12801254

1281-
let line_slice: &[BNTypeDefinitionLine] =
1282-
unsafe { slice::from_raw_parts(lines, count) };
1255+
let mut lines = ptr::null_mut();
1256+
let mut count = 0;
12831257

1284-
for (i, line) in line_slice.iter().enumerate() {
1285-
if i > 0 {
1286-
writeln!(f)?;
1287-
}
1258+
unsafe {
1259+
BNGetTypePrinterTypeLines(
1260+
printer,
1261+
self.handle,
1262+
container,
1263+
&mut name.0,
1264+
64,
1265+
false,
1266+
BNTokenEscapingType::NoTokenEscapingType,
1267+
&mut lines,
1268+
&mut count,
1269+
)
1270+
};
1271+
unsafe {
1272+
BNFreeTypeContainer(container);
1273+
}
12881274

1289-
let tokens: &[BNInstructionTextToken] =
1290-
unsafe { slice::from_raw_parts(line.tokens, line.count) };
1275+
if lines.is_null() {
1276+
return Err(fmt::Error);
1277+
}
12911278

1292-
for token in tokens {
1293-
let text: *const c_char = token.text;
1294-
let str = unsafe { CStr::from_ptr(text) };
1295-
write!(f, "{}", str.to_string_lossy())?;
1296-
}
1297-
}
1279+
let line_slice = unsafe { slice::from_raw_parts(lines, count) };
12981280

1299-
unsafe {
1300-
BNFreeTypeDefinitionLineList(lines, count);
1301-
}
1302-
return Ok(());
1281+
for (i, line) in line_slice.iter().enumerate() {
1282+
if i > 0 {
1283+
writeln!(f)?;
1284+
}
1285+
1286+
let tokens = unsafe { slice::from_raw_parts(line.tokens, line.count) };
1287+
1288+
for token in tokens {
1289+
let text = unsafe { CStr::from_ptr(token.text) };
1290+
write!(f, "{}", text.to_string_lossy())?;
13031291
}
13041292
}
1305-
Err(fmt::Error)
1293+
1294+
unsafe {
1295+
BNFreeTypeDefinitionLineList(lines, count);
1296+
}
1297+
Ok(())
13061298
}
13071299
}
13081300

0 commit comments

Comments
 (0)