Skip to content

Commit b6e7321

Browse files
committed
Replace stdlib module with direct std imports
… except std::sync::Mutex, which there is a polyfill for.
1 parent 06d0d5f commit b6e7321

File tree

9 files changed

+62
-116
lines changed

9 files changed

+62
-116
lines changed

tracing-core/src/callsite.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,25 @@
9696
//! [`Dispatch`]: crate::dispatcher::Dispatch
9797
//! [macros]: https://docs.rs/tracing/latest/tracing/#macros
9898
//! [instrument]: https://docs.rs/tracing/latest/tracing/attr.instrument.html
99-
use crate::stdlib::{
99+
100+
use alloc::vec::Vec;
101+
use core::{
100102
any::TypeId,
101103
fmt,
102104
hash::{Hash, Hasher},
103105
ptr,
104-
sync::{
105-
atomic::{AtomicBool, AtomicPtr, AtomicU8, Ordering},
106-
Mutex,
107-
},
108-
vec::Vec,
106+
sync::atomic::{AtomicBool, AtomicPtr, AtomicU8, Ordering},
109107
};
108+
109+
use self::dispatchers::Dispatchers;
110110
use crate::{
111111
dispatcher::Dispatch,
112112
lazy::Lazy,
113113
metadata::{LevelFilter, Metadata},
114114
subscriber::Interest,
115+
sync::Mutex,
115116
};
116117

117-
use self::dispatchers::Dispatchers;
118-
119118
/// Trait implemented by callsites.
120119
///
121120
/// These functions are only intended to be called by the callsite registry, which

tracing-core/src/dispatcher.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,15 @@ use crate::{
131131
Event, LevelFilter, Metadata,
132132
};
133133

134-
use crate::stdlib::{
134+
use alloc::sync::{Arc, Weak};
135+
use core::{
135136
any::Any,
136137
fmt,
137-
sync::{
138-
atomic::{AtomicBool, AtomicUsize, Ordering},
139-
Arc, Weak,
140-
},
138+
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
141139
};
142140

143141
#[cfg(feature = "std")]
144-
use crate::stdlib::{
142+
use std::{
145143
cell::{Cell, Ref, RefCell},
146144
error,
147145
};
@@ -904,9 +902,10 @@ impl Drop for DefaultGuard {
904902

905903
#[cfg(test)]
906904
mod test {
907-
use super::*;
908905
#[cfg(feature = "std")]
909-
use crate::stdlib::sync::atomic::{AtomicUsize, Ordering};
906+
use std::sync::atomic::{AtomicUsize, Ordering};
907+
908+
use super::*;
910909
use crate::{
911910
callsite::Callsite,
912911
metadata::{Kind, Level, Metadata},

tracing-core/src/field.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,18 @@
109109
//! [`record`]: super::subscriber::Subscriber::record
110110
//! [`event`]: super::subscriber::Subscriber::event
111111
//! [`Value::record`]: Value::record
112-
use crate::callsite;
113-
use crate::stdlib::{
112+
113+
use alloc::{boxed::Box, string::String};
114+
use core::{
114115
borrow::Borrow,
115116
fmt::{self, Write},
116117
hash::{Hash, Hasher},
117118
num,
118119
ops::Range,
119-
string::String,
120120
};
121121

122122
use self::private::ValidLen;
123+
use crate::callsite;
123124

124125
/// An opaque key allowing _O_(1) access to a field in a `Span`'s key-value
125126
/// data.
@@ -649,9 +650,9 @@ impl Value for fmt::Arguments<'_> {
649650
}
650651
}
651652

652-
impl<T: ?Sized> crate::sealed::Sealed for crate::stdlib::boxed::Box<T> where T: Value {}
653+
impl<T: ?Sized> crate::sealed::Sealed for Box<T> where T: Value {}
653654

654-
impl<T: ?Sized> Value for crate::stdlib::boxed::Box<T>
655+
impl<T: ?Sized> Value for Box<T>
655656
where
656657
T: Value,
657658
{
@@ -1120,12 +1121,11 @@ mod private {
11201121

11211122
#[cfg(test)]
11221123
mod test {
1123-
use alloc::boxed::Box;
1124+
use alloc::{borrow::ToOwned, boxed::Box, string::String};
11241125
use std::format;
11251126

11261127
use super::*;
11271128
use crate::metadata::{Kind, Level, Metadata};
1128-
use crate::stdlib::{borrow::ToOwned, string::String};
11291129

11301130
// Make sure TEST_CALLSITE_* have non-zero size, so they can't be located at the same address.
11311131
struct TestCallsite1();
@@ -1238,7 +1238,7 @@ mod test {
12381238

12391239
struct MyVisitor;
12401240
impl Visit for MyVisitor {
1241-
fn record_debug(&mut self, field: &Field, _: &dyn (crate::stdlib::fmt::Debug)) {
1241+
fn record_debug(&mut self, field: &Field, _: &dyn (fmt::Debug)) {
12421242
assert_eq!(field.callsite(), TEST_META_1.callsite())
12431243
}
12441244
}
@@ -1257,7 +1257,7 @@ mod test {
12571257

12581258
struct MyVisitor;
12591259
impl Visit for MyVisitor {
1260-
fn record_debug(&mut self, field: &Field, _: &dyn (crate::stdlib::fmt::Debug)) {
1260+
fn record_debug(&mut self, field: &Field, _: &dyn (fmt::Debug)) {
12611261
assert_eq!(field.name(), "bar")
12621262
}
12631263
}
@@ -1276,7 +1276,7 @@ mod test {
12761276
let valueset = fields.value_set(values);
12771277
let mut result = String::new();
12781278
valueset.record(&mut |_: &Field, value: &dyn fmt::Debug| {
1279-
use crate::stdlib::fmt::Write;
1279+
use core::fmt::Write;
12801280
write!(&mut result, "{:?}", value).unwrap();
12811281
});
12821282
assert_eq!(result, "123".to_owned());

tracing-core/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ pub(crate) mod spin;
277277
pub type Once = self::spin::Once<()>;
278278

279279
#[cfg(feature = "std")]
280-
pub use stdlib::sync::Once;
280+
pub use std::sync::Once;
281281

282282
pub mod callsite;
283283
pub mod dispatcher;
@@ -286,8 +286,12 @@ pub mod field;
286286
pub mod metadata;
287287
mod parent;
288288
pub mod span;
289-
pub(crate) mod stdlib;
290289
pub mod subscriber;
290+
#[cfg(not(feature = "std"))]
291+
mod sync;
292+
293+
#[cfg(feature = "std")]
294+
pub(crate) use std::sync;
291295

292296
#[doc(inline)]
293297
pub use self::{

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},
@@ -548,7 +548,7 @@ impl fmt::Display for Level {
548548

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

553553
impl FromStr for Level {
554554
type Err = ParseLevelError;
@@ -729,7 +729,7 @@ impl LevelFilter {
729729
// the inputs to `set_max` to the set of valid discriminants.
730730
// Therefore, **as long as `MAX_VALUE` is only ever set by
731731
// `set_max`**, this is safe.
732-
crate::stdlib::hint::unreachable_unchecked()
732+
core::hint::unreachable_unchecked()
733733
},
734734
}
735735
}
@@ -1047,7 +1047,7 @@ impl PartialOrd<Level> for LevelFilter {
10471047
#[cfg(test)]
10481048
mod tests {
10491049
use super::*;
1050-
use crate::stdlib::mem;
1050+
use core::mem;
10511051

10521052
#[test]
10531053
fn level_from_str() {

tracing-core/src/span.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! Spans represent periods of time in the execution of a program.
2+
3+
use core::num::NonZeroU64;
4+
25
use crate::field::FieldSet;
36
use crate::parent::Parent;
4-
use crate::stdlib::num::NonZeroU64;
57
use crate::{field, Metadata};
68

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

tracing-core/src/stdlib.rs

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

tracing-core/src/subscriber.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
//! Collectors collect and record trace data.
22
use crate::{span, Dispatch, Event, LevelFilter, Metadata};
33

4-
use crate::stdlib::{
5-
any::{Any, TypeId},
6-
boxed::Box,
7-
sync::Arc,
8-
};
4+
use alloc::{boxed::Box, sync::Arc};
5+
use core::any::{Any, TypeId};
96

107
/// Trait representing the functions required to collect trace data.
118
///

tracing-core/src/sync.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub(crate) use crate::spin::MutexGuard;
2+
3+
/// This wraps `spin::Mutex` to return a `Result`, so that it can be
4+
/// used with code written against `std::sync::Mutex`.
5+
///
6+
/// Since `spin::Mutex` doesn't support poisoning, the `Result` returned
7+
/// by `lock` will always be `Ok`.
8+
#[derive(Debug, Default)]
9+
pub(crate) struct Mutex<T> {
10+
inner: crate::spin::Mutex<T>,
11+
}
12+
13+
impl<T> Mutex<T> {
14+
// pub(crate) fn new(data: T) -> Self {
15+
// Self {
16+
// inner: crate::spin::Mutex::new(data),
17+
// }
18+
// }
19+
20+
pub(crate) fn lock(&self) -> Result<MutexGuard<'_, T>, ()> {
21+
Ok(self.inner.lock())
22+
}
23+
}

0 commit comments

Comments
 (0)