Skip to content

Commit 9c6dd2d

Browse files
authored
serde: allow tracing-serde to work on no_std. (#960)
Allows `tracing-serde` to work on devices without `std`. * Enable `#[no_std]` in `tracing-serde` when `cfg(not(feature = std))` * No longer have unnecessary dependencies on std in serde/tracing. No behavior differences or actual code changes other than allowing for core/alloc fallbacks. Signed-off-by: Ana Hobden <operator@hoverbear.org>
1 parent 95d5511 commit 9c6dd2d

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

tracing-serde/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ categories = [
1717
]
1818
keywords = ["logging", "tracing", "serialization"]
1919

20+
[features]
21+
default = ["std"]
22+
std = ["serde/std", "tracing-core/std"]
23+
2024
[dependencies]
21-
serde = "1"
22-
tracing-core = { path = "../tracing-core", version = "0.2"}
25+
serde = { version = "1", default-features = false, features = ["alloc"] }
26+
tracing-core = { path = "../tracing-core", version = "0.2", default-features = false }
2327

2428
[dev-dependencies]
2529
serde_json = "1"

tracing-serde/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@ After you implement your `Subscriber`, you can use your `tracing`
9898
subscriber (`JsonSubscriber` in the above example) to record serialized
9999
trace data.
100100

101+
## Crate Feature Flags
102+
103+
The following crate feature flags are available:
104+
105+
* `std`: Depend on the Rust standard library (enabled by default).
106+
107+
`no_std` users may disable this feature with `default-features = false`:
108+
109+
```toml
110+
[dependencies]
111+
tracing-serde = { version = "0.2", default-features = false }
112+
```
113+
114+
**Note**:`tracing-serde`'s `no_std` support requires `liballoc`.
115+
116+
101117
## Supported Rust Versions
102118

103119
Tracing is built against the latest stable release. The minimum supported

tracing-serde/src/lib.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@
109109
//! subscriber (`JsonSubscriber` in the above example) to record serialized
110110
//! trace data.
111111
//!
112+
//! ## Crate Feature Flags
113+
//!
114+
//! The following crate feature flags are available:
115+
//!
116+
//! * `std`: Depend on the Rust standard library (enabled by default).
117+
//!
118+
//! `no_std` users may disable this feature with `default-features = false`:
119+
//!
120+
//! ```toml
121+
//! [dependencies]
122+
//! tracing-serde = { version = "0.2", default-features = false }
123+
//! ```
124+
//
125+
//! **Note**:`tracing-serde`'s `no_std` support requires `liballoc`.
126+
//!
112127
//! ## Supported Rust Versions
113128
//!
114129
//! Tracing is built against the latest stable release. The minimum supported
@@ -153,7 +168,10 @@
153168
unused_parens,
154169
while_true
155170
)]
156-
use std::fmt;
171+
// Support using tracing-serde without the standard library!
172+
#![cfg_attr(not(feature = "std"), no_std)]
173+
174+
use core::fmt;
157175

158176
use serde::{
159177
ser::{SerializeMap, SerializeSeq, SerializeStruct, SerializeTupleStruct, Serializer},

0 commit comments

Comments
 (0)