Skip to content

Commit

Permalink
Reduce the scope of unsafe blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Apr 10, 2020
1 parent 30f7dd0 commit ba344ee
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1266,19 +1266,21 @@ fn set_logger_inner<F>(make_logger: F) -> Result<(), SetLoggerError>
where
F: FnOnce() -> &'static dyn Log,
{
unsafe {
match STATE.compare_and_swap(UNINITIALIZED, INITIALIZING, Ordering::SeqCst) {
UNINITIALIZED => {
match STATE.compare_and_swap(UNINITIALIZED, INITIALIZING, Ordering::SeqCst) {
UNINITIALIZED => {
unsafe {
LOGGER = make_logger();
STATE.store(INITIALIZED, Ordering::SeqCst);
Ok(())
}
INITIALIZING => {
while STATE.load(Ordering::SeqCst) == INITIALIZING {}
Err(SetLoggerError(()))
STATE.store(INITIALIZED, Ordering::SeqCst);
Ok(())
}
INITIALIZING => {
while STATE.load(Ordering::SeqCst) == INITIALIZING {
std::sync::atomic::spin_loop_hint();
}
_ => Err(SetLoggerError(())),
Err(SetLoggerError(()))
}
_ => Err(SetLoggerError(())),
}
}

Expand Down Expand Up @@ -1354,13 +1356,11 @@ impl error::Error for ParseLevelError {}
///
/// If a logger has not been set, a no-op implementation is returned.
pub fn logger() -> &'static dyn Log {
unsafe {
if STATE.load(Ordering::SeqCst) != INITIALIZED {
static NOP: NopLogger = NopLogger;
&NOP
} else {
LOGGER
}
if STATE.load(Ordering::SeqCst) != INITIALIZED {
static NOP: NopLogger = NopLogger;
&NOP
} else {
unsafe { LOGGER }
}
}

Expand Down

0 comments on commit ba344ee

Please sign in to comment.