From 9c6dd2d58568a5dabee8425f8815b236d4c421fe Mon Sep 17 00:00:00 2001 From: Ana Hobden Date: Thu, 1 Oct 2020 16:05:02 -0700 Subject: [PATCH] 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 --- tracing-serde/Cargo.toml | 8 ++++++-- tracing-serde/README.md | 16 ++++++++++++++++ tracing-serde/src/lib.rs | 20 +++++++++++++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/tracing-serde/Cargo.toml b/tracing-serde/Cargo.toml index 350866ccda..89c9892aa3 100644 --- a/tracing-serde/Cargo.toml +++ b/tracing-serde/Cargo.toml @@ -17,9 +17,13 @@ categories = [ ] keywords = ["logging", "tracing", "serialization"] +[features] +default = ["std"] +std = ["serde/std", "tracing-core/std"] + [dependencies] -serde = "1" -tracing-core = { path = "../tracing-core", version = "0.2"} +serde = { version = "1", default-features = false, features = ["alloc"] } +tracing-core = { path = "../tracing-core", version = "0.2", default-features = false } [dev-dependencies] serde_json = "1" diff --git a/tracing-serde/README.md b/tracing-serde/README.md index 5282199e29..b0cbc7f320 100644 --- a/tracing-serde/README.md +++ b/tracing-serde/README.md @@ -98,6 +98,22 @@ After you implement your `Subscriber`, you can use your `tracing` subscriber (`JsonSubscriber` in the above example) to record serialized trace data. +## Crate Feature Flags + +The following crate feature flags are available: + +* `std`: Depend on the Rust standard library (enabled by default). + + `no_std` users may disable this feature with `default-features = false`: + + ```toml + [dependencies] + tracing-serde = { version = "0.2", default-features = false } + ``` + + **Note**:`tracing-serde`'s `no_std` support requires `liballoc`. + + ## Supported Rust Versions Tracing is built against the latest stable release. The minimum supported diff --git a/tracing-serde/src/lib.rs b/tracing-serde/src/lib.rs index b0901610b0..b6efede81b 100644 --- a/tracing-serde/src/lib.rs +++ b/tracing-serde/src/lib.rs @@ -109,6 +109,21 @@ //! subscriber (`JsonSubscriber` in the above example) to record serialized //! trace data. //! +//! ## Crate Feature Flags +//! +//! The following crate feature flags are available: +//! +//! * `std`: Depend on the Rust standard library (enabled by default). +//! +//! `no_std` users may disable this feature with `default-features = false`: +//! +//! ```toml +//! [dependencies] +//! tracing-serde = { version = "0.2", default-features = false } +//! ``` +// +//! **Note**:`tracing-serde`'s `no_std` support requires `liballoc`. +//! //! ## Supported Rust Versions //! //! Tracing is built against the latest stable release. The minimum supported @@ -153,7 +168,10 @@ unused_parens, while_true )] -use std::fmt; +// Support using tracing-serde without the standard library! +#![cfg_attr(not(feature = "std"), no_std)] + +use core::fmt; use serde::{ ser::{SerializeMap, SerializeSeq, SerializeStruct, SerializeTupleStruct, Serializer},