Skip to content

core: Switch to unconditional no_std #3323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions tracing-core/src/callsite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 9 additions & 10 deletions tracing-core/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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,
};
Expand Down Expand Up @@ -182,7 +180,7 @@ enum Kind<T> {
}

#[cfg(feature = "std")]
thread_local! {
std::thread_local! {
static CURRENT_STATE: State = const {
State {
default: RefCell::new(None),
Expand Down Expand Up @@ -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},
Expand Down
21 changes: 12 additions & 9 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -649,9 +650,9 @@ impl Value for fmt::Arguments<'_> {
}
}

impl<T: ?Sized> crate::sealed::Sealed for crate::stdlib::boxed::Box<T> where T: Value {}
impl<T: ?Sized> crate::sealed::Sealed for Box<T> where T: Value {}

impl<T: ?Sized> Value for crate::stdlib::boxed::Box<T>
impl<T: ?Sized> Value for Box<T>
where
T: Value,
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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())
}
}
Expand All @@ -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")
}
}
Expand All @@ -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());
Expand Down
16 changes: 12 additions & 4 deletions tracing-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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::{
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 @@ -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;
Expand Down Expand Up @@ -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()
},
}
}
Expand Down Expand Up @@ -1047,7 +1047,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
4 changes: 3 additions & 1 deletion tracing-core/src/span.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
78 changes: 0 additions & 78 deletions tracing-core/src/stdlib.rs

This file was deleted.

7 changes: 2 additions & 5 deletions tracing-core/src/subscriber.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down
23 changes: 23 additions & 0 deletions tracing-core/src/sync.rs
Original file line number Diff line number Diff line change
@@ -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<T> {
inner: crate::spin::Mutex<T>,
}

impl<T> Mutex<T> {
// pub(crate) fn new(data: T) -> Self {
// Self {
// inner: crate::spin::Mutex::new(data),
// }
// }

pub(crate) fn lock(&self) -> Result<MutexGuard<'_, T>, ()> {
Ok(self.inner.lock())
}
}