From ab92ea526d455b402efbccc7160c8aec0237c88f Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 28 Apr 2014 20:36:08 -0700 Subject: std: Modernize the local_data api This commit brings the local_data api up to modern rust standards with a few key improvements: * The `pop` and `set` methods have been combined into one method, `replace` * The `get_mut` method has been removed. All interior mutability should be done through `RefCell`. * All functionality is now exposed as a method on the keys themselves. Instead of importing std::local_data, you now use "key.replace()" and "key.get()". * All closures have been removed in favor of RAII functionality. This means that get() and get_mut() no long require closures, but rather return Option where the smart pointer takes care of relinquishing the borrow and also implements the necessary Deref traits * The modify() function was removed to cut the local_data interface down to its bare essentials (similarly to how RefCell removed set/get). [breaking-change] --- src/liblog/lib.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src/liblog') diff --git a/src/liblog/lib.rs b/src/liblog/lib.rs index 035a81f4143..09e9cd83f3d 100644 --- a/src/liblog/lib.rs +++ b/src/liblog/lib.rs @@ -122,7 +122,6 @@ use std::cast; use std::fmt; use std::io::LineBufferedWriter; use std::io; -use std::local_data; use std::os; use std::rt; use std::slice; @@ -228,7 +227,7 @@ pub fn log(level: u32, loc: &'static LogLocation, args: &fmt::Arguments) { // Completely remove the local logger from TLS in case anyone attempts to // frob the slot while we're doing the logging. This will destroy any logger // set during logging. - let mut logger = local_data::pop(local_logger).unwrap_or_else(|| { + let mut logger = local_logger.replace(None).unwrap_or_else(|| { box DefaultLogger { handle: io::stderr() } as Box }); logger.log(&LogRecord { @@ -238,7 +237,7 @@ pub fn log(level: u32, loc: &'static LogLocation, args: &fmt::Arguments) { module_path: loc.module_path, line: loc.line, }); - local_data::set(local_logger, logger); + local_logger.replace(Some(logger)); } /// Getter for the global log level. This is a function so that it can be called @@ -250,9 +249,7 @@ pub fn log_level() -> u32 { unsafe { LOG_LEVEL } } /// Replaces the task-local logger with the specified logger, returning the old /// logger. pub fn set_logger(logger: Box) -> Option> { - let prev = local_data::pop(local_logger); - local_data::set(local_logger, logger); - return prev; + local_logger.replace(Some(logger)) } /// A LogRecord is created by the logging macros, and passed as the only -- cgit 1.4.1-3-g733a5