Skip to content

Commit

Permalink
subscriber: use macros for module declarations (#1009)
Browse files Browse the repository at this point in the history
## Motivation

This is code refactoring. Currently, there is a lot of code that declares
modules as follows:

```rust
#[cfg(feature = "registry")]
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
pub use registry::Registry;

#[cfg(feature = "registry")]
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
pub fn registry() -> Registry {
    Registry::default()
}
```

It is verbose to write a module declaration multiple times using the feature
attribute. Also, if the definition for the same feature spans multiple places,
it will be a little harder to read the code.

## Solution

You can combine features attributes in one place by writing as follows. It also
eliminates the need to write the same code over and over again.

```rust
cfg_feature!("registry", {
    pub use registry::Registry;

    pub fn registry() -> Registry {
        Registry::default()
    }
});

```

If this code is accepted, we will extend the declaration of the module using
macros to other files as well as `tracing-subscriber/lib.rs`.
  • Loading branch information
k-nasa authored Oct 6, 2020
1 parent dcad8d0 commit cb1dd95
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
46 changes: 13 additions & 33 deletions tracing-subscriber/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,10 @@
use tracing_core::span::Id;

#[macro_use]
macro_rules! try_lock {
($lock:expr) => {
try_lock!($lock, else return)
};
($lock:expr, else $els:expr) => {
if let Ok(l) = $lock {
l
} else if std::thread::panicking() {
$els
} else {
panic!("lock poisoned")
}
};
}
mod macros;

pub mod field;
pub mod filter;
#[cfg(feature = "fmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "fmt")))]
pub mod fmt;
pub mod layer;
pub mod prelude;
pub mod registry;
Expand All @@ -132,24 +116,20 @@ pub use filter::EnvFilter;

pub use layer::Layer;

#[cfg(feature = "registry")]
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
pub use registry::Registry;

///
#[cfg(feature = "registry")]
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
pub fn registry() -> Registry {
Registry::default()
}
cfg_feature!("fmt", {
pub mod fmt;
pub use fmt::fmt;
pub use fmt::Subscriber as FmtSubscriber;
});

#[cfg(feature = "fmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "fmt")))]
pub use fmt::Subscriber as FmtSubscriber;
cfg_feature!("registry", {
pub use registry::Registry;

#[cfg(feature = "fmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "fmt")))]
pub use fmt::fmt;
///
pub fn registry() -> Registry {
Registry::default()
}
});

use std::default::Default;
/// Tracks the currently executing span on a per-thread basis.
Expand Down
24 changes: 24 additions & 0 deletions tracing-subscriber/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
macro_rules! try_lock {
($lock:expr) => {
try_lock!($lock, else return)
};
($lock:expr, else $els:expr) => {
if let Ok(l) = $lock {
l
} else if std::thread::panicking() {
$els
} else {
panic!("lock poisoned")
}
};
}

macro_rules! cfg_feature {
($name:literal, { $($item:item)* }) => {
$(
#[cfg(feature = $name)]
#[cfg_attr(docsrs, doc(cfg(feature = $name)))]
$item
)*
}
}

0 comments on commit cb1dd95

Please sign in to comment.