Skip to content

Commit cb1dd95

Browse files
authored
subscriber: use macros for module declarations (#1009)
## 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`.
1 parent dcad8d0 commit cb1dd95

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

tracing-subscriber/src/lib.rs

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,10 @@
9898
use tracing_core::span::Id;
9999

100100
#[macro_use]
101-
macro_rules! try_lock {
102-
($lock:expr) => {
103-
try_lock!($lock, else return)
104-
};
105-
($lock:expr, else $els:expr) => {
106-
if let Ok(l) = $lock {
107-
l
108-
} else if std::thread::panicking() {
109-
$els
110-
} else {
111-
panic!("lock poisoned")
112-
}
113-
};
114-
}
101+
mod macros;
115102

116103
pub mod field;
117104
pub mod filter;
118-
#[cfg(feature = "fmt")]
119-
#[cfg_attr(docsrs, doc(cfg(feature = "fmt")))]
120-
pub mod fmt;
121105
pub mod layer;
122106
pub mod prelude;
123107
pub mod registry;
@@ -132,24 +116,20 @@ pub use filter::EnvFilter;
132116

133117
pub use layer::Layer;
134118

135-
#[cfg(feature = "registry")]
136-
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
137-
pub use registry::Registry;
138-
139-
///
140-
#[cfg(feature = "registry")]
141-
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
142-
pub fn registry() -> Registry {
143-
Registry::default()
144-
}
119+
cfg_feature!("fmt", {
120+
pub mod fmt;
121+
pub use fmt::fmt;
122+
pub use fmt::Subscriber as FmtSubscriber;
123+
});
145124

146-
#[cfg(feature = "fmt")]
147-
#[cfg_attr(docsrs, doc(cfg(feature = "fmt")))]
148-
pub use fmt::Subscriber as FmtSubscriber;
125+
cfg_feature!("registry", {
126+
pub use registry::Registry;
149127

150-
#[cfg(feature = "fmt")]
151-
#[cfg_attr(docsrs, doc(cfg(feature = "fmt")))]
152-
pub use fmt::fmt;
128+
///
129+
pub fn registry() -> Registry {
130+
Registry::default()
131+
}
132+
});
153133

154134
use std::default::Default;
155135
/// Tracks the currently executing span on a per-thread basis.

tracing-subscriber/src/macros.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
macro_rules! try_lock {
2+
($lock:expr) => {
3+
try_lock!($lock, else return)
4+
};
5+
($lock:expr, else $els:expr) => {
6+
if let Ok(l) = $lock {
7+
l
8+
} else if std::thread::panicking() {
9+
$els
10+
} else {
11+
panic!("lock poisoned")
12+
}
13+
};
14+
}
15+
16+
macro_rules! cfg_feature {
17+
($name:literal, { $($item:item)* }) => {
18+
$(
19+
#[cfg(feature = $name)]
20+
#[cfg_attr(docsrs, doc(cfg(feature = $name)))]
21+
$item
22+
)*
23+
}
24+
}

0 commit comments

Comments
 (0)