From b371f91145704e5f999184eb30081cfc42152538 Mon Sep 17 00:00:00 2001 From: Justin Karneges Date: Tue, 7 Jan 2025 13:56:16 -0800 Subject: [PATCH] fix warnings on latest rust --- src/connmgr/batch.rs | 2 +- src/core/log.rs | 24 +++++++++--------------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/connmgr/batch.rs b/src/connmgr/batch.rs index ec123fc0..bdf773da 100644 --- a/src/connmgr/batch.rs +++ b/src/connmgr/batch.rs @@ -147,7 +147,7 @@ impl Batch { self.nodes.remove(key.nkey); } - pub fn take_group<'a, 'b: 'a, F>(&'a mut self, get_id: F) -> Option + pub fn take_group<'a, 'b: 'a, F>(&'a mut self, get_id: F) -> Option> where F: Fn(usize) -> Option<(&'b [u8], u32)>, { diff --git a/src/core/log.rs b/src/core/log.rs index b5ee8d89..19e0758f 100644 --- a/src/core/log.rs +++ b/src/core/log.rs @@ -18,9 +18,8 @@ use log::{Level, Log, Metadata, Record}; use std::fs::File; use std::io::{self, Write}; -use std::mem; use std::str; -use std::sync::{Mutex, Once}; +use std::sync::{Mutex, OnceLock}; use time::macros::format_description; use time::{OffsetDateTime, UtcOffset}; @@ -129,12 +128,10 @@ unsafe fn get_offset() -> Option { offset } -static mut LOGGER: mem::MaybeUninit = mem::MaybeUninit::uninit(); +static LOGGER: OnceLock = OnceLock::new(); pub fn ensure_init_simple_logger(output_file: Option, runner_mode: bool) { - static INIT: Once = Once::new(); - - INIT.call_once(|| { + LOGGER.get_or_init(|| { // SAFETY: we accept that this call is unsound. on some platforms it // is the only way to know the time zone, with a chance of UB if // another thread modifies environment vars during the call. the risk @@ -143,13 +140,10 @@ pub fn ensure_init_simple_logger(output_file: Option, runner_mode: bool) { // zone than not know the time zone let local_offset = unsafe { get_offset() }; - // SAFETY: call_once ensures this only happens from one place - unsafe { - LOGGER.write(SimpleLogger { - local_offset, - output_file: output_file.map(Mutex::new), - runner_mode, - }); + SimpleLogger { + local_offset, + output_file: output_file.map(Mutex::new), + runner_mode, } }); } @@ -157,8 +151,8 @@ pub fn ensure_init_simple_logger(output_file: Option, runner_mode: bool) { pub fn get_simple_logger() -> &'static SimpleLogger { ensure_init_simple_logger(None, false); - // SAFETY: logger is guaranteed to have been initialized - unsafe { LOGGER.assume_init_ref() } + // logger is guaranteed to have been initialized + LOGGER.get().expect("logger should be initialized") } pub fn local_offset_check() {