Skip to content
Snippets Groups Projects
Commit 2952e4bd authored by Dmitry Ivanov's avatar Dmitry Ivanov
Browse files

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 {
   |                                           ~~~~~~~~~~~~~~~~~~~~~~~~~
```
parent 822236eb
No related branches found
No related tags found
1 merge request!1003fix: rust 1.78 doesn't like references to `static mut` objects
Pipeline #40353 passed
use crate::tlog; use crate::tlog;
use std::collections::HashSet; use std::{collections::HashSet, ptr};
static mut INJECTED_ERRORS: Option<HashSet<String>> = None; static mut INJECTED_ERRORS: Option<HashSet<String>> = None;
#[inline(always)] #[inline(always)]
pub fn enable(error: &str, enable: bool) { pub fn enable(error: &str, enable: bool) {
// SAFETY: safe as long as only called from tx thread // SAFETY: safe as long as only called from tx thread
let injected_errors = unsafe { &mut INJECTED_ERRORS }; let injected_errors = unsafe { ptr::addr_of_mut!(INJECTED_ERRORS).as_mut() }
let injected_errors = injected_errors.get_or_insert_with(Default::default); .unwrap()
.get_or_insert_with(Default::default);
if enable { if enable {
injected_errors.insert(error.into()); injected_errors.insert(error.into());
tlog!(Info, "ERROR INJECTION '{error}': fused"); tlog!(Info, "ERROR INJECTION '{error}': fused");
...@@ -20,10 +22,10 @@ pub fn enable(error: &str, enable: bool) { ...@@ -20,10 +22,10 @@ pub fn enable(error: &str, enable: bool) {
#[inline(always)] #[inline(always)]
pub fn is_enabled(error: &str) -> bool { pub fn is_enabled(error: &str) -> bool {
// SAFETY: safe as long as only called from tx thread // SAFETY: safe as long as only called from tx thread
let Some(injected_errors) = (unsafe { &INJECTED_ERRORS }) else { match unsafe { ptr::addr_of!(INJECTED_ERRORS).as_ref() }.unwrap() {
return false; Some(injected_errors) => injected_errors.contains(error),
}; None => false,
injected_errors.contains(error) }
} }
#[cfg(not(feature = "error_injection"))] #[cfg(not(feature = "error_injection"))]
......
use serde::{Deserialize, Serialize}; 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::net_box::{Conn, ConnOptions, Options};
use ::tarantool::tuple::Tuple; use ::tarantool::tuple::Tuple;
...@@ -16,17 +16,6 @@ use crate::util::Uppercase; ...@@ -16,17 +16,6 @@ use crate::util::Uppercase;
const DEFAULT_TIMEOUT: u64 = 60; const DEFAULT_TIMEOUT: u64 = 60;
const PICO_USER: &'static str = "pico_service"; 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 /// Struct for box.slab.info() result
#[derive(Deserialize)] #[derive(Deserialize)]
struct SlabInfo { struct SlabInfo {
......
use ::tarantool::log::{say, SayLevel}; use ::tarantool::log::{say, SayLevel};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::collections::HashMap; use std::{collections::HashMap, ptr};
static mut LOG_LEVEL: SayLevel = SayLevel::Info; static mut LOG_LEVEL: SayLevel = SayLevel::Info;
...@@ -48,7 +48,11 @@ pub fn clear_highlight() { ...@@ -48,7 +48,11 @@ pub fn clear_highlight() {
#[inline] #[inline]
pub fn highlight_key(key: impl Into<String> + AsRef<str>, color: Option<Color>) { 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 { if let Some(color) = color {
map.insert(key.into(), color); map.insert(key.into(), color);
} else { } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment