From 2952e4bdaaf3e90b267a3207c755201eea2c0c25 Mon Sep 17 00:00:00 2001 From: Dmitry Ivanov <ivadmi5@gmail.com> Date: Tue, 21 May 2024 00:53:09 +0300 Subject: [PATCH] fix: rust 1.78 doesn't like references to `static mut` objects This patch fixes a couple of warnings that look like this: ``` warning: creating a shared reference to mutable static is discouraged --> src/error_injection.rs:23:43 | 23 | let Some(injected_errors) = (unsafe { &INJECTED_ERRORS }) else { | ^^^^^^^^^^^^^^^^ shared reference to mutable static | = note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447> = note: this will be a hard error in the 2024 edition = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior help: use `addr_of!` instead to create a raw pointer | 23 | let Some(injected_errors) = (unsafe { addr_of!(INJECTED_ERRORS) }) else { | ~~~~~~~~~~~~~~~~~~~~~~~~~ ``` --- src/error_injection.rs | 16 +++++++++------- src/http_server.rs | 13 +------------ src/tlog.rs | 8 ++++++-- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/error_injection.rs b/src/error_injection.rs index 8cc3a6a8ee..0fcde5897d 100644 --- a/src/error_injection.rs +++ b/src/error_injection.rs @@ -1,13 +1,15 @@ use crate::tlog; -use std::collections::HashSet; +use std::{collections::HashSet, ptr}; static mut INJECTED_ERRORS: Option<HashSet<String>> = None; #[inline(always)] pub fn enable(error: &str, enable: bool) { // SAFETY: safe as long as only called from tx thread - let injected_errors = unsafe { &mut INJECTED_ERRORS }; - let injected_errors = injected_errors.get_or_insert_with(Default::default); + let injected_errors = unsafe { ptr::addr_of_mut!(INJECTED_ERRORS).as_mut() } + .unwrap() + .get_or_insert_with(Default::default); + if enable { injected_errors.insert(error.into()); tlog!(Info, "ERROR INJECTION '{error}': fused"); @@ -20,10 +22,10 @@ pub fn enable(error: &str, enable: bool) { #[inline(always)] pub fn is_enabled(error: &str) -> bool { // SAFETY: safe as long as only called from tx thread - let Some(injected_errors) = (unsafe { &INJECTED_ERRORS }) else { - return false; - }; - injected_errors.contains(error) + match unsafe { ptr::addr_of!(INJECTED_ERRORS).as_ref() }.unwrap() { + Some(injected_errors) => injected_errors.contains(error), + None => false, + } } #[cfg(not(feature = "error_injection"))] diff --git a/src/http_server.rs b/src/http_server.rs index 4ca0ae1d73..44c9a0fd57 100644 --- a/src/http_server.rs +++ b/src/http_server.rs @@ -1,5 +1,5 @@ use serde::{Deserialize, Serialize}; -use std::{collections::HashMap, error::Error, fmt}; +use std::{collections::HashMap, error::Error}; use ::tarantool::net_box::{Conn, ConnOptions, Options}; use ::tarantool::tuple::Tuple; @@ -16,17 +16,6 @@ use crate::util::Uppercase; const DEFAULT_TIMEOUT: u64 = 60; const PICO_USER: &'static str = "pico_service"; -#[derive(Debug, Serialize)] -struct HttpError(String); - -impl fmt::Display for HttpError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -impl Error for HttpError {} - /// Struct for box.slab.info() result #[derive(Deserialize)] struct SlabInfo { diff --git a/src/tlog.rs b/src/tlog.rs index f7bc6481d0..5718defe04 100644 --- a/src/tlog.rs +++ b/src/tlog.rs @@ -1,6 +1,6 @@ use ::tarantool::log::{say, SayLevel}; use once_cell::sync::Lazy; -use std::collections::HashMap; +use std::{collections::HashMap, ptr}; static mut LOG_LEVEL: SayLevel = SayLevel::Info; @@ -48,7 +48,11 @@ pub fn clear_highlight() { #[inline] pub fn highlight_key(key: impl Into<String> + AsRef<str>, color: Option<Color>) { - let map = unsafe { &mut HIGHLIGHT }.get_or_insert_with(HashMap::new); + // SAFETY: safe as long as only called from tx thread + let map = unsafe { ptr::addr_of_mut!(HIGHLIGHT).as_mut() } + .unwrap() + .get_or_insert_with(HashMap::new); + if let Some(color) = color { map.insert(key.into(), color); } else { -- GitLab