Skip to content

Commit

Permalink
[#3] Allow static_mut_ref for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Oct 30, 2024
1 parent 336513d commit 5d96bfa
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion iceoryx2-bb/log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,21 @@ pub fn get_logger() -> &'static dyn Log {
unsafe { LOGGER = Some(&DEFAULT_LOGGER) };
});

unsafe { *LOGGER.as_ref().unwrap() }
// # From The Compiler
//
// shared references to mutable statics are dangerous; it's undefined behavior
// 1. if the static is mutated or
// 2. if a mutable reference is created for it while the shared reference lives
//
// # Safety
//
// 1. The logger is always an immutable threadsafe object with only interior mutability.
// 2. [`std::sync::Once`] is used to ensure it can only mutated on initialization and the
// lifetime is `'static`.
#[allow(static_mut_refs)]
unsafe {
*LOGGER.as_ref().unwrap()
}
}

#[doc(hidden)]
Expand Down

0 comments on commit 5d96bfa

Please sign in to comment.