From f0ca06bf8e47515439f7cd36118351c3b745b2ac 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.toml | 1 - rust/src/types.rs | 114 +++++++++++++++++++++------------------------- 2 files changed, 53 insertions(+), 62 deletions(-) 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(()) } }