Skip to content

Commit

Permalink
Merge pull request #367 from TethysSvensson/master
Browse files Browse the repository at this point in the history
Miscellaneous cleanup
  • Loading branch information
KodrAus authored Dec 11, 2019
2 parents 4597769 + 4f5de62 commit efcc39c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/kv/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod std_support {
use super::*;
use std::{error, io};

pub(super) type BoxedError = Box<error::Error + Send + Sync>;
pub(super) type BoxedError = Box<dyn error::Error + Send + Sync>;

impl Error {
/// Create an error from a standard error type.
Expand Down
24 changes: 12 additions & 12 deletions src/kv/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub trait Source {
///
/// A source should yield the same key-value pairs to a subsequent visitor unless
/// that visitor itself fails.
fn visit<'kvs>(&'kvs self, visitor: &mut Visitor<'kvs>) -> Result<(), Error>;
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error>;

/// Get the value for a given key.
///
Expand Down Expand Up @@ -82,7 +82,7 @@ impl<'a, T> Source for &'a T
where
T: Source + ?Sized,
{
fn visit<'kvs>(&'kvs self, visitor: &mut Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
Source::visit(&**self, visitor)
}

Expand All @@ -100,7 +100,7 @@ where
K: ToKey,
V: ToValue,
{
fn visit<'kvs>(&'kvs self, visitor: &mut Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
visitor.visit_pair(self.0.to_key(), self.1.to_value())
}

Expand All @@ -121,7 +121,7 @@ impl<S> Source for [S]
where
S: Source,
{
fn visit<'kvs>(&'kvs self, visitor: &mut Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
for source in self {
source.visit(visitor)?;
}
Expand All @@ -138,7 +138,7 @@ impl<S> Source for Option<S>
where
S: Source,
{
fn visit<'kvs>(&'kvs self, visitor: &mut Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
if let Some(ref source) = *self {
source.visit(visitor)?;
}
Expand Down Expand Up @@ -206,7 +206,7 @@ mod std_support {
where
S: Source + ?Sized,
{
fn visit<'kvs>(&'kvs self, visitor: &mut Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
Source::visit(&**self, visitor)
}

Expand All @@ -223,7 +223,7 @@ mod std_support {
where
S: Source,
{
fn visit<'kvs>(&'kvs self, visitor: &mut Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
Source::visit(&**self, visitor)
}

Expand Down Expand Up @@ -251,7 +251,7 @@ mod std_support {
V: ToValue,
S: BuildHasher,
{
fn visit<'kvs>(&'kvs self, visitor: &mut Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
for (key, value) in self {
visitor.visit_pair(key.to_key(), value.to_value())?;
}
Expand All @@ -272,7 +272,7 @@ mod std_support {
K: ToKey + Borrow<str> + Ord,
V: ToValue,
{
fn visit<'kvs>(&'kvs self, visitor: &mut Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
for (key, value) in self {
visitor.visit_pair(key.to_key(), value.to_value())?;
}
Expand Down Expand Up @@ -347,12 +347,12 @@ mod tests {

#[test]
fn source_is_object_safe() {
fn _check(_: &Source) {}
fn _check(_: &dyn Source) {}
}

#[test]
fn visitor_is_object_safe() {
fn _check(_: &Visitor) {}
fn _check(_: &dyn Visitor) {}
}

#[test]
Expand All @@ -363,7 +363,7 @@ mod tests {
}

impl Source for OnePair {
fn visit<'kvs>(&'kvs self, visitor: &mut Visitor<'kvs>) -> Result<(), Error> {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
visitor.visit_pair(self.key.to_key(), self.value.to_value())
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/kv/value/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ pub(super) enum Inner<'v> {
/// A simple primitive value that can be copied without allocating.
Primitive(Primitive<'v>),
/// A value that can be filled.
Fill(&'v Fill),
Fill(&'v dyn Fill),
/// A debuggable value.
Debug(&'v fmt::Debug),
Debug(&'v dyn fmt::Debug),
/// A displayable value.
Display(&'v fmt::Display),
Display(&'v dyn fmt::Display),

#[cfg(feature = "kv_unstable_sval")]
/// A structured value from `sval`.
Sval(&'v sval_support::Value),
Sval(&'v dyn sval_support::Value),
}

impl<'v> Inner<'v> {
pub(super) fn visit(&self, visitor: &mut Visitor) -> Result<(), Error> {
pub(super) fn visit(&self, visitor: &mut dyn Visitor) -> Result<(), Error> {
match *self {
Inner::Primitive(value) => match value {
Primitive::Signed(value) => visitor.i64(value),
Expand All @@ -47,8 +47,8 @@ impl<'v> Inner<'v> {

/// The internal serialization contract.
pub(super) trait Visitor {
fn debug(&mut self, v: &fmt::Debug) -> Result<(), Error>;
fn display(&mut self, v: &fmt::Display) -> Result<(), Error> {
fn debug(&mut self, v: &dyn fmt::Debug) -> Result<(), Error>;
fn display(&mut self, v: &dyn fmt::Display) -> Result<(), Error> {
self.debug(&format_args!("{}", v))
}

Expand All @@ -61,7 +61,7 @@ pub(super) trait Visitor {
fn none(&mut self) -> Result<(), Error>;

#[cfg(feature = "kv_unstable_sval")]
fn sval(&mut self, v: &sval_support::Value) -> Result<(), Error>;
fn sval(&mut self, v: &dyn sval_support::Value) -> Result<(), Error>;
}

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -119,7 +119,7 @@ mod fmt_support {
struct FmtVisitor<'a, 'b: 'a>(&'a mut fmt::Formatter<'b>);

impl<'a, 'b: 'a> Visitor for FmtVisitor<'a, 'b> {
fn debug(&mut self, v: &fmt::Debug) -> Result<(), Error> {
fn debug(&mut self, v: &dyn fmt::Debug) -> Result<(), Error> {
v.fmt(self.0)?;

Ok(())
Expand Down Expand Up @@ -154,7 +154,7 @@ mod fmt_support {
}

#[cfg(feature = "kv_unstable_sval")]
fn sval(&mut self, v: &sval_support::Value) -> Result<(), Error> {
fn sval(&mut self, v: &dyn sval_support::Value) -> Result<(), Error> {
sval_support::fmt(self.0, v)
}
}
Expand Down Expand Up @@ -188,7 +188,7 @@ pub(super) mod sval_support {

pub(in kv::value) use self::sval::Value;

pub(super) fn fmt(f: &mut fmt::Formatter, v: &sval::Value) -> Result<(), Error> {
pub(super) fn fmt(f: &mut fmt::Formatter, v: &dyn sval::Value) -> Result<(), Error> {
sval::fmt::debug(f, v)?;
Ok(())
}
Expand All @@ -206,7 +206,7 @@ pub(super) mod sval_support {
struct SvalVisitor<'a, 'b: 'a>(&'a mut sval::value::Stream<'b>);

impl<'a, 'b: 'a> Visitor for SvalVisitor<'a, 'b> {
fn debug(&mut self, v: &fmt::Debug) -> Result<(), Error> {
fn debug(&mut self, v: &dyn fmt::Debug) -> Result<(), Error> {
self.0
.fmt(format_args!("{:?}", v))
.map_err(Error::from_sval)
Expand Down Expand Up @@ -240,7 +240,7 @@ pub(super) mod sval_support {
self.0.none().map_err(Error::from_sval)
}

fn sval(&mut self, v: &sval::Value) -> Result<(), Error> {
fn sval(&mut self, v: &dyn sval::Value) -> Result<(), Error> {
self.0.any(v).map_err(Error::from_sval)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/kv/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ where
/// A value slot to fill using the [`Fill`](trait.Fill.html) trait.
pub struct Slot<'a> {
filled: bool,
visitor: &'a mut Visitor,
visitor: &'a mut dyn Visitor,
}

impl<'a> fmt::Debug for Slot<'a> {
Expand All @@ -65,7 +65,7 @@ impl<'a> fmt::Debug for Slot<'a> {
}

impl<'a> Slot<'a> {
fn new(visitor: &'a mut Visitor) -> Self {
fn new(visitor: &'a mut dyn Visitor) -> Self {
Slot {
visitor,
filled: false,
Expand Down Expand Up @@ -110,7 +110,7 @@ impl<'v> Value<'v> {
}
}

fn visit(&self, visitor: &mut Visitor) -> Result<(), Error> {
fn visit(&self, visitor: &mut dyn Visitor) -> Result<(), Error> {
self.inner.visit(visitor)
}
}
Expand All @@ -125,7 +125,7 @@ mod tests {

impl Fill for TestFill {
fn fill(&self, slot: &mut Slot) -> Result<(), Error> {
let dbg: &fmt::Debug = &1;
let dbg: &dyn fmt::Debug = &1;

slot.fill(Value::from_debug(&dbg))
}
Expand Down
4 changes: 2 additions & 2 deletions src/kv/value/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'v> Value<'v> {
struct TestVisitor(Option<Token>);

impl internal::Visitor for TestVisitor {
fn debug(&mut self, v: &fmt::Debug) -> Result<(), Error> {
fn debug(&mut self, v: &dyn fmt::Debug) -> Result<(), Error> {
self.0 = Some(Token::Str(format!("{:?}", v)));
Ok(())
}
Expand Down Expand Up @@ -67,7 +67,7 @@ impl<'v> Value<'v> {
}

#[cfg(feature = "kv_unstable_sval")]
fn sval(&mut self, _: &internal::sval_support::Value) -> Result<(), Error> {
fn sval(&mut self, _: &dyn internal::sval_support::Value) -> Result<(), Error> {
self.0 = Some(Token::Sval);
Ok(())
}
Expand Down
30 changes: 15 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ pub mod kv;

// The LOGGER static holds a pointer to the global logger. It is protected by
// the STATE static which determines whether LOGGER has been initialized yet.
static mut LOGGER: &'static Log = &NopLogger;
static mut LOGGER: &dyn Log = &NopLogger;

#[allow(deprecated)]
static STATE: AtomicUsize = ATOMIC_USIZE_INIT;
Expand All @@ -320,11 +320,11 @@ const INITIALIZED: usize = 2;
#[allow(deprecated)]
static MAX_LOG_LEVEL_FILTER: AtomicUsize = ATOMIC_USIZE_INIT;

static LOG_LEVEL_NAMES: [&'static str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
static LOG_LEVEL_NAMES: [&str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];

static SET_LOGGER_ERROR: &'static str = "attempted to set a logger after the logging system \
was already initialized";
static LEVEL_PARSE_ERROR: &'static str =
static SET_LOGGER_ERROR: &str = "attempted to set a logger after the logging system \
was already initialized";
static LEVEL_PARSE_ERROR: &str =
"attempted to convert a string that doesn't match an existing log level";

/// An enum representing the available verbosity levels of the logger.
Expand Down Expand Up @@ -746,7 +746,7 @@ pub struct Record<'a> {
// the underlying `Source`.
#[cfg(feature = "kv_unstable")]
#[derive(Clone)]
struct KeyValues<'a>(&'a kv::Source);
struct KeyValues<'a>(&'a dyn kv::Source);

#[cfg(feature = "kv_unstable")]
impl<'a> fmt::Debug for KeyValues<'a> {
Expand Down Expand Up @@ -827,7 +827,7 @@ impl<'a> Record<'a> {
/// The structued key-value pairs associated with the message.
#[cfg(feature = "kv_unstable")]
#[inline]
pub fn key_values(&self) -> &kv::Source {
pub fn key_values(&self) -> &dyn kv::Source {
self.key_values.0
}

Expand Down Expand Up @@ -990,7 +990,7 @@ impl<'a> RecordBuilder<'a> {
/// Set [`key_values`](struct.Record.html#method.key_values)
#[cfg(feature = "kv_unstable")]
#[inline]
pub fn key_values(&mut self, kvs: &'a kv::Source) -> &mut RecordBuilder<'a> {
pub fn key_values(&mut self, kvs: &'a dyn kv::Source) -> &mut RecordBuilder<'a> {
self.record.key_values = KeyValues(kvs);
self
}
Expand Down Expand Up @@ -1198,8 +1198,8 @@ pub fn max_level() -> LevelFilter {
///
/// [`set_logger`]: fn.set_logger.html
#[cfg(all(feature = "std", atomic_cas))]
pub fn set_boxed_logger(logger: Box<Log>) -> Result<(), SetLoggerError> {
set_logger_inner(|| unsafe { &*Box::into_raw(logger) })
pub fn set_boxed_logger(logger: Box<dyn Log>) -> Result<(), SetLoggerError> {
set_logger_inner(|| Box::leak(logger))
}

/// Sets the global logger to a `&'static Log`.
Expand Down Expand Up @@ -1256,14 +1256,14 @@ pub fn set_boxed_logger(logger: Box<Log>) -> Result<(), SetLoggerError> {
///
/// [`set_logger_racy`]: fn.set_logger_racy.html
#[cfg(atomic_cas)]
pub fn set_logger(logger: &'static Log) -> Result<(), SetLoggerError> {
pub fn set_logger(logger: &'static dyn Log) -> Result<(), SetLoggerError> {
set_logger_inner(|| logger)
}

#[cfg(atomic_cas)]
fn set_logger_inner<F>(make_logger: F) -> Result<(), SetLoggerError>
where
F: FnOnce() -> &'static Log,
F: FnOnce() -> &'static dyn Log,
{
unsafe {
match STATE.compare_and_swap(UNINITIALIZED, INITIALIZING, Ordering::SeqCst) {
Expand Down Expand Up @@ -1300,7 +1300,7 @@ where
/// (including all logging macros).
///
/// [`set_logger`]: fn.set_logger.html
pub unsafe fn set_logger_racy(logger: &'static Log) -> Result<(), SetLoggerError> {
pub unsafe fn set_logger_racy(logger: &'static dyn Log) -> Result<(), SetLoggerError> {
match STATE.load(Ordering::SeqCst) {
UNINITIALIZED => {
LOGGER = logger;
Expand Down Expand Up @@ -1360,7 +1360,7 @@ impl error::Error for ParseLevelError {
/// Returns a reference to the logger.
///
/// If a logger has not been set, a no-op implementation is returned.
pub fn logger() -> &'static Log {
pub fn logger() -> &'static dyn Log {
unsafe {
if STATE.load(Ordering::SeqCst) != INITIALIZED {
static NOP: NopLogger = NopLogger;
Expand Down Expand Up @@ -1405,7 +1405,7 @@ pub fn __private_api_log(
args: fmt::Arguments<'_>,
level: Level,
&(target, module_path, file, line): &(&str, &'static str, &'static str, u32),
kvs: Option<&[(&str, &kv::ToValue)]>,
kvs: Option<&[(&str, &dyn kv::ToValue)]>,
) {
logger().log(
&Record::builder()
Expand Down
4 changes: 1 addition & 3 deletions test_max_level_features/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Log>) -> Result<(), log::SetLoggerError> {
unsafe {
log::set_logger(&*Box::into_raw(logger))
}
log::set_logger(Box::leak(logger))
}

struct State {
Expand Down
4 changes: 2 additions & 2 deletions tests/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::sync::{Arc, Mutex};
use log::set_boxed_logger;

#[cfg(not(feature = "std"))]
fn set_boxed_logger(logger: Box<Log>) -> Result<(), log::SetLoggerError> {
log::set_logger(unsafe { &*Box::into_raw(logger) })
fn set_boxed_logger(logger: Box<dyn Log>) -> Result<(), log::SetLoggerError> {
log::set_logger(Box::leak(logger))
}

struct State {
Expand Down

0 comments on commit efcc39c

Please sign in to comment.