Skip to content

Commit

Permalink
Remove stdlib.rs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jyn514 committed Oct 3, 2020
1 parent 5fb114f commit 9f841bb
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 237 deletions.
18 changes: 8 additions & 10 deletions tracing-core/src/callsite.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
//! Callsites represent the source locations from which spans or events
//! originate.
use crate::stdlib::{
fmt,
hash::{Hash, Hasher},
ptr,
sync::{
atomic::{AtomicPtr, Ordering},
Mutex, MutexGuard,
},
vec::Vec,
};
use crate::{
dispatcher::{self, Dispatch},
metadata::{LevelFilter, Metadata},
subscriber::Interest,
sync::{Mutex, MutexGuard},
};
use alloc::vec::Vec;
use core::{
fmt,
hash::{Hash, Hasher},
ptr,
sync::atomic::{AtomicPtr, Ordering},
};

lazy_static! {
Expand Down
12 changes: 4 additions & 8 deletions tracing-core/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,15 @@ use crate::{
Event, LevelFilter, Metadata,
};

use crate::stdlib::{
use alloc::sync::{Arc, Weak};
use core::{
any::Any,
fmt,
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Arc, Weak,
},
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
};

#[cfg(feature = "std")]
use crate::stdlib::{
use std::{
cell::{Cell, RefCell, RefMut},
error,
};
Expand Down Expand Up @@ -777,8 +775,6 @@ impl Drop for DefaultGuard {
mod test {

use super::*;
#[cfg(feature = "std")]
use crate::stdlib::sync::atomic::{AtomicUsize, Ordering};
use crate::{
callsite::Callsite,
metadata::{Kind, Level, Metadata},
Expand Down
10 changes: 5 additions & 5 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//! [`record`]: super::subscriber::Subscriber::record
//! [`event`]: super::subscriber::Subscriber::event
use crate::callsite;
use crate::stdlib::{
use core::{
borrow::Borrow,
fmt,
hash::{Hash, Hasher},
Expand Down Expand Up @@ -828,7 +828,7 @@ impl_valid_len! {
mod test {
use super::*;
use crate::metadata::{Kind, Level, Metadata};
use crate::stdlib::{borrow::ToOwned, string::String};
use alloc::{borrow::ToOwned, string::String};

struct TestCallsite1;
static TEST_CALLSITE_1: TestCallsite1 = TestCallsite1;
Expand Down Expand Up @@ -929,7 +929,7 @@ mod test {

struct MyVisitor;
impl Visit for MyVisitor {
fn record_debug(&mut self, field: &Field, _: &dyn (crate::stdlib::fmt::Debug)) {
fn record_debug(&mut self, field: &Field, _: &dyn (core::fmt::Debug)) {
assert_eq!(field.callsite(), TEST_META_1.callsite())
}
}
Expand All @@ -948,7 +948,7 @@ mod test {

struct MyVisitor;
impl Visit for MyVisitor {
fn record_debug(&mut self, field: &Field, _: &dyn (crate::stdlib::fmt::Debug)) {
fn record_debug(&mut self, field: &Field, _: &dyn (core::fmt::Debug)) {
assert_eq!(field.name(), "bar")
}
}
Expand All @@ -967,7 +967,7 @@ mod test {
let valueset = fields.value_set(values);
let mut result = String::new();
valueset.record(&mut |_: &Field, value: &dyn fmt::Debug| {
use crate::stdlib::fmt::Write;
use core::fmt::Write;
write!(&mut result, "{:?}", value).unwrap();
});
assert_eq!(result, "123".to_owned());
Expand Down
25 changes: 17 additions & 8 deletions tracing-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
unused_parens,
while_true
)]
#[cfg(not(feature = "std"))]

extern crate alloc;

/// Statically constructs an [`Identifier`] for the provided [`Callsite`].
Expand Down Expand Up @@ -240,17 +240,27 @@ extern crate lazy_static;
#[macro_use]
mod lazy_static;

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

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

#[cfg(feature = "std")]
pub use stdlib::sync::Once;
mod sync {
pub(crate) use std::sync::{Mutex, MutexGuard};
pub use std::sync::Once;
}

#[doc(hidden)]
pub use self::sync::Once;

pub mod callsite;
pub mod dispatcher;
Expand All @@ -259,7 +269,6 @@ pub mod field;
pub mod metadata;
mod parent;
pub mod span;
pub(crate) mod stdlib;
pub mod subscriber;

#[doc(inline)]
Expand Down
8 changes: 4 additions & 4 deletions tracing-core/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Metadata describing trace data.
use super::{callsite, field};
use crate::stdlib::{
use core::{
cmp, fmt,
str::FromStr,
sync::atomic::{AtomicUsize, Ordering},
Expand Down Expand Up @@ -299,7 +299,7 @@ impl fmt::Display for Level {

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

impl FromStr for Level {
type Err = ParseLevelError;
Expand Down Expand Up @@ -481,7 +481,7 @@ impl LevelFilter {
// the inputs to `set_max` to the set of valid discriminants.
// Therefore, **as long as `MAX_VALUE` is only ever set by
// `set_max`**, this is safe.
crate::stdlib::hint::unreachable_unchecked()
core::hint::unreachable_unchecked()
},
}
}
Expand Down Expand Up @@ -799,7 +799,7 @@ impl PartialOrd<Level> for LevelFilter {
#[cfg(test)]
mod tests {
use super::*;
use crate::stdlib::mem;
use core::mem;

#[test]
fn level_from_str() {
Expand Down
2 changes: 1 addition & 1 deletion tracing-core/src/span.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Spans represent periods of time in the execution of a program.
use crate::parent::Parent;
use crate::stdlib::num::NonZeroU64;
use crate::{field, Metadata};
use core::num::NonZeroU64;

/// Identifies a span within the context of a subscriber.
///
Expand Down
6 changes: 3 additions & 3 deletions tracing-core/src/spin/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ impl<T: ?Sized> Mutex<T> {
///
/// The returned value may be dereferenced for data access
/// and the lock will be dropped when the guard falls out of scope.
pub(crate) fn lock(&self) -> MutexGuard<'_, T> {
pub(crate) fn lock(&self) -> Result<MutexGuard<'_, T>, core::convert::Infallible> {
self.obtain_lock();
MutexGuard {
Ok(MutexGuard {
lock: &self.lock,
data: unsafe { &mut *self.data.get() },
}
})
}

/// Tries to lock the mutex. If it is already locked, it will return None. Otherwise it returns
Expand Down
78 changes: 0 additions & 78 deletions tracing-core/src/stdlib.rs

This file was deleted.

2 changes: 1 addition & 1 deletion tracing-core/src/subscriber.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Subscribers collect and record trace data.
use crate::{span, Event, LevelFilter, Metadata};

use crate::stdlib::any::{Any, TypeId};
use core::any::{Any, TypeId};

/// Trait representing the functions required to collect trace data.
///
Expand Down
2 changes: 1 addition & 1 deletion tracing-futures/src/executor/futures_preview.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stdlib::future::Future;
use core::future::Future;
use crate::{Instrument, Instrumented, WithDispatch};
use futures_core_preview::{
future::FutureObj,
Expand Down
12 changes: 5 additions & 7 deletions tracing-futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@
#[cfg(feature = "std-future")]
use pin_project::pin_project;

pub(crate) mod stdlib;

#[cfg(feature = "std-future")]
use crate::stdlib::{pin::Pin, task::Context};
use core::{pin::Pin, task::Context};

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

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

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

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

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> crate::stdlib::task::Poll<Self::Output> {
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> core::task::Poll<Self::Output> {
let this = self.project();
let dispatch = this.dispatch;
let future = this.inner;
Expand Down
44 changes: 0 additions & 44 deletions tracing-futures/src/stdlib.rs

This file was deleted.

Loading

0 comments on commit 9f841bb

Please sign in to comment.