Skip to content

Commit c6593e0

Browse files
committed
Remove stdlib.rs
Since `core` and `alloc` are unconditionally available, there was no need to use a facade module for most of std. The only exceptions were for `Mutex` and `Once`, which are only available with the full standard library. These had a `sync` module added to `tracing-core` which acts as a facade, re-exporting `std` if available and `spin` if not.
1 parent 9c6dd2d commit c6593e0

File tree

16 files changed

+48
-224
lines changed

16 files changed

+48
-224
lines changed

tracing-core/src/callsite.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Callsites represent the source locations from which spans or events
22
//! originate.
3-
use crate::stdlib::{
3+
use core::{
44
fmt,
55
hash::{Hash, Hasher},
6-
sync::Mutex,
7-
vec::Vec,
86
};
7+
use alloc::vec::Vec;
8+
use crate::sync::Mutex;
99
use crate::{
1010
dispatcher::{self, Dispatch},
1111
metadata::{LevelFilter, Metadata},

tracing-core/src/dispatcher.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,17 @@ use crate::{
138138
Event, LevelFilter, Metadata,
139139
};
140140

141-
use crate::stdlib::{
141+
use core::{
142142
any::Any,
143143
fmt,
144144
sync::{
145145
atomic::{AtomicBool, AtomicUsize, Ordering},
146-
Arc, Weak,
147146
},
148147
};
148+
use alloc::sync::{Arc, Weak};
149149

150150
#[cfg(feature = "std")]
151-
use crate::stdlib::{
151+
use std::{
152152
cell::{Cell, RefCell, RefMut},
153153
error,
154154
};
@@ -784,8 +784,6 @@ impl Drop for DefaultGuard {
784784
#[cfg(test)]
785785
mod test {
786786
use super::*;
787-
#[cfg(feature = "std")]
788-
use crate::stdlib::sync::atomic::{AtomicUsize, Ordering};
789787
use crate::{
790788
callsite::Callsite,
791789
metadata::{Kind, Level, Metadata},

tracing-core/src/field.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
//! [`Value::record`]: trait.Value.html#method.record
4040
//! [`Visit`]: trait.Visit.html
4141
use crate::callsite;
42-
use crate::stdlib::{
42+
use core::{
4343
borrow::Borrow,
4444
fmt,
4545
hash::{Hash, Hasher},
@@ -833,7 +833,7 @@ impl_valid_len! {
833833
mod test {
834834
use super::*;
835835
use crate::metadata::{Kind, Level, Metadata};
836-
use crate::stdlib::{borrow::ToOwned, string::String};
836+
use alloc::{borrow::ToOwned, string::String};
837837

838838
struct TestCallsite1;
839839
static TEST_CALLSITE_1: TestCallsite1 = TestCallsite1;
@@ -934,7 +934,7 @@ mod test {
934934

935935
struct MyVisitor;
936936
impl Visit for MyVisitor {
937-
fn record_debug(&mut self, field: &Field, _: &dyn (crate::stdlib::fmt::Debug)) {
937+
fn record_debug(&mut self, field: &Field, _: &dyn (core::fmt::Debug)) {
938938
assert_eq!(field.callsite(), TEST_META_1.callsite())
939939
}
940940
}
@@ -953,7 +953,7 @@ mod test {
953953

954954
struct MyVisitor;
955955
impl Visit for MyVisitor {
956-
fn record_debug(&mut self, field: &Field, _: &dyn (crate::stdlib::fmt::Debug)) {
956+
fn record_debug(&mut self, field: &Field, _: &dyn (core::fmt::Debug)) {
957957
assert_eq!(field.name(), "bar")
958958
}
959959
}
@@ -972,7 +972,7 @@ mod test {
972972
let valueset = fields.value_set(values);
973973
let mut result = String::new();
974974
valueset.record(&mut |_: &Field, value: &dyn fmt::Debug| {
975-
use crate::stdlib::fmt::Write;
975+
use core::fmt::Write;
976976
write!(&mut result, "{:?}", value).unwrap();
977977
});
978978
assert_eq!(result, "123".to_owned());

tracing-core/src/lib.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
unused_parens,
115115
while_true
116116
)]
117-
#[cfg(not(feature = "std"))]
117+
118118
extern crate alloc;
119119

120120
/// Statically constructs an [`Identifier`] for the provided [`Callsite`].
@@ -240,17 +240,27 @@ extern crate lazy_static;
240240
#[macro_use]
241241
mod lazy_static;
242242

243-
// Trimmed-down vendored version of spin 0.5.2 (0387621)
244-
// Dependency of no_std lazy_static, not required in a std build
243+
// Facade module: `no_std` uses spinlocks, `std` uses the mutexes in the standard library
245244
#[cfg(not(feature = "std"))]
246-
pub(crate) mod spin;
245+
mod sync {
246+
#[doc(hidden)]
247+
pub type Once = crate::spin::Once<()>;
248+
pub(crate) use crate::spin::*;
249+
}
247250

248251
#[cfg(not(feature = "std"))]
249-
#[doc(hidden)]
250-
pub type Once = self::spin::Once<()>;
252+
// Trimmed-down vendored version of spin 0.5.2 (0387621)
253+
// Dependency of no_std lazy_static, not required in a std build
254+
pub(crate) mod spin;
251255

252256
#[cfg(feature = "std")]
253-
pub use stdlib::sync::Once;
257+
mod sync {
258+
pub use std::sync::Once;
259+
pub(crate) use std::sync::Mutex;
260+
}
261+
262+
#[doc(hidden)]
263+
pub use self::sync::Once;
254264

255265
pub mod callsite;
256266
pub mod dispatcher;
@@ -259,7 +269,6 @@ pub mod field;
259269
pub mod metadata;
260270
mod parent;
261271
pub mod span;
262-
pub(crate) mod stdlib;
263272
pub mod subscriber;
264273

265274
#[doc(inline)]

tracing-core/src/metadata.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Metadata describing trace data.
22
use super::{callsite, field};
3-
use crate::stdlib::{
3+
use core::{
44
cmp, fmt,
55
str::FromStr,
66
sync::atomic::{AtomicUsize, Ordering},
@@ -299,7 +299,7 @@ impl fmt::Display for Level {
299299

300300
#[cfg(feature = "std")]
301301
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
302-
impl crate::stdlib::error::Error for ParseLevelError {}
302+
impl std::error::Error for ParseLevelError {}
303303

304304
impl FromStr for Level {
305305
type Err = ParseLevelError;
@@ -481,7 +481,7 @@ impl LevelFilter {
481481
// the inputs to `set_max` to the set of valid discriminants.
482482
// Therefore, **as long as `MAX_VALUE` is only ever set by
483483
// `set_max`**, this is safe.
484-
crate::stdlib::hint::unreachable_unchecked()
484+
core::hint::unreachable_unchecked()
485485
},
486486
}
487487
}
@@ -799,7 +799,7 @@ impl PartialOrd<Level> for LevelFilter {
799799
#[cfg(test)]
800800
mod tests {
801801
use super::*;
802-
use crate::stdlib::mem;
802+
use core::mem;
803803

804804
#[test]
805805
fn level_from_str() {

tracing-core/src/span.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Spans represent periods of time in the execution of a program.
22
use crate::parent::Parent;
3-
use crate::stdlib::num::NonZeroU64;
3+
use core::num::NonZeroU64;
44
use crate::{field, Metadata};
55

66
/// Identifies a span within the context of a subscriber.

tracing-core/src/spin/mutex.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ impl<T: ?Sized> Mutex<T> {
4949
///
5050
/// The returned value may be dereferenced for data access
5151
/// and the lock will be dropped when the guard falls out of scope.
52-
pub(crate) fn lock(&self) -> MutexGuard<'_, T> {
52+
pub(crate) fn lock(&self) -> Result<MutexGuard<'_, T>, core::convert::Infallible> {
5353
self.obtain_lock();
54-
MutexGuard {
54+
Ok(MutexGuard {
5555
lock: &self.lock,
5656
data: unsafe { &mut *self.data.get() },
57-
}
57+
})
5858
}
5959

6060
/// Tries to lock the mutex. If it is already locked, it will return None. Otherwise it returns

tracing-core/src/stdlib.rs

Lines changed: 0 additions & 78 deletions
This file was deleted.

tracing-core/src/subscriber.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Subscribers collect and record trace data.
22
use crate::{span, Event, LevelFilter, Metadata};
33

4-
use crate::stdlib::any::{Any, TypeId};
4+
use core::any::{Any, TypeId};
55

66
/// Trait representing the functions required to collect trace data.
77
///

tracing-futures/src/executor/futures_preview.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::stdlib::future::Future;
1+
use core::future::Future;
22
use crate::{Instrument, Instrumented, WithDispatch};
33
use futures_core_preview::{
44
future::FutureObj,

tracing-futures/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,8 @@
105105
#[cfg(feature = "std-future")]
106106
use pin_project::pin_project;
107107

108-
pub(crate) mod stdlib;
109-
110108
#[cfg(feature = "std-future")]
111-
use crate::stdlib::{pin::Pin, task::Context};
109+
use core::{pin::Pin, task::Context};
112110

113111
#[cfg(feature = "std")]
114112
use tracing::{dispatcher, Dispatch};
@@ -274,10 +272,10 @@ impl<T: Sized> Instrument for T {}
274272

275273
#[cfg(feature = "std-future")]
276274
#[cfg_attr(docsrs, doc(cfg(feature = "std-future")))]
277-
impl<T: crate::stdlib::future::Future> crate::stdlib::future::Future for Instrumented<T> {
275+
impl<T: core::future::Future> core::future::Future for Instrumented<T> {
278276
type Output = T::Output;
279277

280-
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> crate::stdlib::task::Poll<Self::Output> {
278+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> core::task::Poll<Self::Output> {
281279
let this = self.project();
282280
let _enter = this.span.enter();
283281
this.inner.poll(cx)
@@ -445,10 +443,10 @@ impl<T: futures_01::Future> futures_01::Future for WithDispatch<T> {
445443

446444
#[cfg(all(feature = "std-future", feature = "std"))]
447445
#[cfg_attr(docsrs, doc(cfg(all(feature = "std-future", feature = "std"))))]
448-
impl<T: crate::stdlib::future::Future> crate::stdlib::future::Future for WithDispatch<T> {
446+
impl<T: core::future::Future> core::future::Future for WithDispatch<T> {
449447
type Output = T::Output;
450448

451-
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> crate::stdlib::task::Poll<Self::Output> {
449+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> core::task::Poll<Self::Output> {
452450
let this = self.project();
453451
let dispatch = this.dispatch;
454452
let future = this.inner;

tracing-futures/src/stdlib.rs

Lines changed: 0 additions & 44 deletions
This file was deleted.

tracing/src/instrument.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::stdlib::pin::Pin;
2-
use crate::stdlib::task::{Context, Poll};
3-
use crate::stdlib::{future::Future, marker::Sized};
1+
use core::pin::Pin;
2+
use core::task::{Context, Poll};
3+
use core::{future::Future, marker::Sized};
44
use crate::{dispatcher, span::Span, Dispatch};
55
use pin_project_lite::pin_project;
66

0 commit comments

Comments
 (0)