From 299d1d5c16ebc85640712c1825e50e6ac0f761bb Mon Sep 17 00:00:00 2001 From: Mathias Svensson Date: Tue, 10 Dec 2019 11:36:36 +0100 Subject: [PATCH] Use Box::leak to get rid of some unsafe code --- src/lib.rs | 2 +- test_max_level_features/main.rs | 4 +--- tests/filters.rs | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c8f9377..fd87fcb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1199,7 +1199,7 @@ pub fn max_level() -> LevelFilter { /// [`set_logger`]: fn.set_logger.html #[cfg(all(feature = "std", atomic_cas))] pub fn set_boxed_logger(logger: Box) -> Result<(), SetLoggerError> { - set_logger_inner(|| unsafe { &*Box::into_raw(logger) }) + set_logger_inner(|| Box::leak(logger)) } /// Sets the global logger to a `&'static Log`. diff --git a/test_max_level_features/main.rs b/test_max_level_features/main.rs index ba5ce11..bfbf2b2 100644 --- a/test_max_level_features/main.rs +++ b/test_max_level_features/main.rs @@ -8,9 +8,7 @@ use log::{Level, LevelFilter, Log, Record, Metadata}; use log::set_boxed_logger; #[cfg(not(feature = "std"))] fn set_boxed_logger(logger: Box) -> Result<(), log::SetLoggerError> { - unsafe { - log::set_logger(&*Box::into_raw(logger)) - } + log::set_logger(Box::leak(logger)) } struct State { diff --git a/tests/filters.rs b/tests/filters.rs index e4d21a8..11761b9 100644 --- a/tests/filters.rs +++ b/tests/filters.rs @@ -9,7 +9,7 @@ use log::set_boxed_logger; #[cfg(not(feature = "std"))] fn set_boxed_logger(logger: Box) -> Result<(), log::SetLoggerError> { - log::set_logger(unsafe { &*Box::into_raw(logger) }) + log::set_logger(Box::leak(logger)) } struct State {