diff --git a/tracing-core/src/callsite.rs b/tracing-core/src/callsite.rs index f7f4833eb9..66bd0d349b 100644 --- a/tracing-core/src/callsite.rs +++ b/tracing-core/src/callsite.rs @@ -96,26 +96,25 @@ //! [`Dispatch`]: crate::dispatcher::Dispatch //! [macros]: https://docs.rs/tracing/latest/tracing/#macros //! [instrument]: https://docs.rs/tracing/latest/tracing/attr.instrument.html -use crate::stdlib::{ + +use alloc::vec::Vec; +use core::{ any::TypeId, fmt, hash::{Hash, Hasher}, ptr, - sync::{ - atomic::{AtomicBool, AtomicPtr, AtomicU8, Ordering}, - Mutex, - }, - vec::Vec, + sync::atomic::{AtomicBool, AtomicPtr, AtomicU8, Ordering}, }; + +use self::dispatchers::Dispatchers; use crate::{ dispatcher::Dispatch, lazy::Lazy, metadata::{LevelFilter, Metadata}, subscriber::Interest, + sync::Mutex, }; -use self::dispatchers::Dispatchers; - /// Trait implemented by callsites. /// /// These functions are only intended to be called by the callsite registry, which @@ -516,6 +515,7 @@ mod private { #[cfg(feature = "std")] mod dispatchers { use crate::{dispatcher, lazy::Lazy}; + use alloc::vec::Vec; use std::sync::{ atomic::{AtomicBool, Ordering}, RwLock, RwLockReadGuard, RwLockWriteGuard, diff --git a/tracing-core/src/dispatcher.rs b/tracing-core/src/dispatcher.rs index c2895f8824..535df4a423 100644 --- a/tracing-core/src/dispatcher.rs +++ b/tracing-core/src/dispatcher.rs @@ -122,7 +122,7 @@ //! [`get_default`] function, which executes a closure with a reference to the //! currently default `Dispatch`. This is used primarily by `tracing` //! instrumentation. -//! + use core::ptr::addr_of; use crate::{ @@ -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, Ref, RefCell}, error, }; @@ -182,7 +180,7 @@ enum Kind { } #[cfg(feature = "std")] -thread_local! { +std::thread_local! { static CURRENT_STATE: State = const { State { default: RefCell::new(None), @@ -904,9 +902,10 @@ impl Drop for DefaultGuard { #[cfg(test)] mod test { - use super::*; #[cfg(feature = "std")] - use crate::stdlib::sync::atomic::{AtomicUsize, Ordering}; + use std::sync::atomic::{AtomicUsize, Ordering}; + + use super::*; use crate::{ callsite::Callsite, metadata::{Kind, Level, Metadata}, diff --git a/tracing-core/src/field.rs b/tracing-core/src/field.rs index 1e95f7243b..dc68b43218 100644 --- a/tracing-core/src/field.rs +++ b/tracing-core/src/field.rs @@ -109,17 +109,18 @@ //! [`record`]: super::subscriber::Subscriber::record //! [`event`]: super::subscriber::Subscriber::event //! [`Value::record`]: Value::record -use crate::callsite; -use crate::stdlib::{ + +use alloc::{boxed::Box, string::String}; +use core::{ borrow::Borrow, fmt::{self, Write}, hash::{Hash, Hasher}, num, ops::Range, - string::String, }; use self::private::ValidLen; +use crate::callsite; /// An opaque key allowing _O_(1) access to a field in a `Span`'s key-value /// data. @@ -649,9 +650,9 @@ impl Value for fmt::Arguments<'_> { } } -impl crate::sealed::Sealed for crate::stdlib::boxed::Box where T: Value {} +impl crate::sealed::Sealed for Box where T: Value {} -impl Value for crate::stdlib::boxed::Box +impl Value for Box where T: Value, { @@ -1120,9 +1121,11 @@ mod private { #[cfg(test)] mod test { + use alloc::{borrow::ToOwned, boxed::Box, string::String}; + use std::format; + use super::*; use crate::metadata::{Kind, Level, Metadata}; - use crate::stdlib::{borrow::ToOwned, string::String}; // Make sure TEST_CALLSITE_* have non-zero size, so they can't be located at the same address. struct TestCallsite1(); @@ -1235,7 +1238,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 (fmt::Debug)) { assert_eq!(field.callsite(), TEST_META_1.callsite()) } } @@ -1254,7 +1257,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 (fmt::Debug)) { assert_eq!(field.name(), "bar") } } @@ -1273,7 +1276,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()); diff --git a/tracing-core/src/lib.rs b/tracing-core/src/lib.rs index e7588d436c..43f5cf61b4 100644 --- a/tracing-core/src/lib.rs +++ b/tracing-core/src/lib.rs @@ -116,11 +116,12 @@ //! [`Dispatch`]: dispatcher::Dispatch //! [`tokio-rs/tracing`]: https://github.com/tokio-rs/tracing //! [`tracing`]: https://crates.io/crates/tracing + +#![no_std] #![doc( html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/main/assets/logo-type.png", issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/" )] -#![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(docsrs, feature(doc_cfg), deny(rustdoc::broken_intra_doc_links))] #![warn( missing_debug_implementations, @@ -144,9 +145,12 @@ unused_parens, while_true )] -#[cfg(not(feature = "std"))] + extern crate alloc; +#[cfg(feature = "std")] +extern crate std; + #[doc(hidden)] pub mod __macro_support { // Re-export the `core` functions that are used in macros. This allows @@ -273,7 +277,7 @@ pub(crate) mod spin; pub type Once = self::spin::Once<()>; #[cfg(feature = "std")] -pub use stdlib::sync::Once; +pub use std::sync::Once; pub mod callsite; pub mod dispatcher; @@ -282,8 +286,12 @@ pub mod field; pub mod metadata; mod parent; pub mod span; -pub(crate) mod stdlib; pub mod subscriber; +#[cfg(not(feature = "std"))] +mod sync; + +#[cfg(feature = "std")] +pub(crate) use std::sync; #[doc(inline)] pub use self::{ diff --git a/tracing-core/src/metadata.rs b/tracing-core/src/metadata.rs index 281b62582e..cac6d7a0ac 100644 --- a/tracing-core/src/metadata.rs +++ b/tracing-core/src/metadata.rs @@ -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}, @@ -548,7 +548,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; @@ -729,7 +729,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() }, } } @@ -1047,7 +1047,7 @@ impl PartialOrd for LevelFilter { #[cfg(test)] mod tests { use super::*; - use crate::stdlib::mem; + use core::mem; #[test] fn level_from_str() { diff --git a/tracing-core/src/span.rs b/tracing-core/src/span.rs index 44738b2903..3d8b02e5da 100644 --- a/tracing-core/src/span.rs +++ b/tracing-core/src/span.rs @@ -1,7 +1,9 @@ //! Spans represent periods of time in the execution of a program. + +use core::num::NonZeroU64; + use crate::field::FieldSet; use crate::parent::Parent; -use crate::stdlib::num::NonZeroU64; use crate::{field, Metadata}; /// Identifies a span within the context of a subscriber. diff --git a/tracing-core/src/stdlib.rs b/tracing-core/src/stdlib.rs deleted file mode 100644 index 741549519c..0000000000 --- a/tracing-core/src/stdlib.rs +++ /dev/null @@ -1,78 +0,0 @@ -//! Re-exports either the Rust `std` library or `core` and `alloc` when `std` is -//! disabled. -//! -//! `crate::stdlib::...` should be used rather than `std::` when adding code that -//! will be available with the standard library disabled. -//! -//! Note that this module is called `stdlib` rather than `std`, as Rust 1.34.0 -//! does not permit redefining the name `stdlib` (although this works on the -//! latest stable Rust). -#[cfg(feature = "std")] -pub(crate) use std::*; - -#[cfg(not(feature = "std"))] -pub(crate) use self::no_std::*; - -#[cfg(not(feature = "std"))] -mod no_std { - // We pre-emptively export everything from libcore/liballoc, (even modules - // we aren't using currently) to make adding new code easier. Therefore, - // some of these imports will be unused. - #![allow(unused_imports)] - - pub(crate) use core::{ - any, array, ascii, cell, char, clone, cmp, convert, default, f32, f64, ffi, future, hash, - hint, i128, i16, i8, isize, iter, marker, mem, num, ops, option, pin, ptr, result, task, - time, u128, u16, u32, u8, usize, - }; - - pub(crate) use alloc::{boxed, collections, rc, string, vec}; - - pub(crate) mod borrow { - pub(crate) use alloc::borrow::*; - pub(crate) use core::borrow::*; - } - - pub(crate) mod fmt { - pub(crate) use alloc::fmt::*; - pub(crate) use core::fmt::*; - } - - pub(crate) mod slice { - pub(crate) use alloc::slice::*; - pub(crate) use core::slice::*; - } - - pub(crate) mod str { - pub(crate) use alloc::str::*; - pub(crate) use core::str::*; - } - - pub(crate) mod sync { - pub(crate) use crate::spin::MutexGuard; - pub(crate) use alloc::sync::*; - pub(crate) use core::sync::*; - - /// This wraps `spin::Mutex` to return a `Result`, so that it can be - /// used with code written against `std::sync::Mutex`. - /// - /// Since `spin::Mutex` doesn't support poisoning, the `Result` returned - /// by `lock` will always be `Ok`. - #[derive(Debug, Default)] - pub(crate) struct Mutex { - inner: crate::spin::Mutex, - } - - impl Mutex { - // pub(crate) fn new(data: T) -> Self { - // Self { - // inner: crate::spin::Mutex::new(data), - // } - // } - - pub(crate) fn lock(&self) -> Result, ()> { - Ok(self.inner.lock()) - } - } - } -} diff --git a/tracing-core/src/subscriber.rs b/tracing-core/src/subscriber.rs index 17b6316971..fc7a344485 100644 --- a/tracing-core/src/subscriber.rs +++ b/tracing-core/src/subscriber.rs @@ -1,11 +1,8 @@ //! Collectors collect and record trace data. use crate::{span, Dispatch, Event, LevelFilter, Metadata}; -use crate::stdlib::{ - any::{Any, TypeId}, - boxed::Box, - sync::Arc, -}; +use alloc::{boxed::Box, sync::Arc}; +use core::any::{Any, TypeId}; /// Trait representing the functions required to collect trace data. /// diff --git a/tracing-core/src/sync.rs b/tracing-core/src/sync.rs new file mode 100644 index 0000000000..f53be7791a --- /dev/null +++ b/tracing-core/src/sync.rs @@ -0,0 +1,23 @@ +pub(crate) use crate::spin::MutexGuard; + +/// This wraps `spin::Mutex` to return a `Result`, so that it can be +/// used with code written against `std::sync::Mutex`. +/// +/// Since `spin::Mutex` doesn't support poisoning, the `Result` returned +/// by `lock` will always be `Ok`. +#[derive(Debug, Default)] +pub(crate) struct Mutex { + inner: crate::spin::Mutex, +} + +impl Mutex { + // pub(crate) fn new(data: T) -> Self { + // Self { + // inner: crate::spin::Mutex::new(data), + // } + // } + + pub(crate) fn lock(&self) -> Result, ()> { + Ok(self.inner.lock()) + } +}