From f668f31e02a1c9435c85481d3ced18540dee6241 Mon Sep 17 00:00:00 2001 From: Michael Krasnitski Date: Thu, 25 Jul 2024 14:18:21 -0400 Subject: [PATCH] Replace `lazy_static` crate with `std::sync::LazyLock` --- rust/Cargo.lock | 7 --- rust/Cargo.toml | 1 - rust/src/types.rs | 114 +++++++++++++++++++++------------------------- 3 files changed, 53 insertions(+), 69 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 4563bbff4..1ac0e9ca9 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -122,7 +122,6 @@ name = "binaryninja" version = "0.1.0" dependencies = [ "binaryninjacore-sys", - "lazy_static", "log", "rayon", ] @@ -553,12 +552,6 @@ dependencies = [ "libc", ] -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - [[package]] name = "libc" version = "0.2.153" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index ff016274b..809d82104 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -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" } diff --git a/rust/src/types.rs b/rust/src/types.rs index c01564547..f267d8dbf 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -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}, @@ -43,7 +41,7 @@ use std::{ ops::Range, os::raw::c_char, ptr, result, slice, - sync::Mutex, + sync::LazyLock, }; pub type Result = result::Result; @@ -1231,77 +1229,71 @@ impl fmt::Display for Type { } } -lazy_static! { - static ref TYPE_DEBUG_BV: Mutex>> = - Mutex::new(BinaryView::from_data(&FileMetadata::new(), &[]).ok()); -} +static TYPE_DEBUG_BV: LazyLock>> = + 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(()) } }