From 13886435a600bd049e4e9db6e22fac6a0113784a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 21 Mar 2017 16:49:48 -0700 Subject: [PATCH] Re-export derives from serde --- serde/Cargo.toml | 1 + serde/src/lib.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/serde/Cargo.toml b/serde/Cargo.toml index 2f28ef40..54a200b3 100644 --- a/serde/Cargo.toml +++ b/serde/Cargo.toml @@ -17,6 +17,7 @@ travis-ci = { repository = "serde-rs/serde" } [features] default = ["std"] +derive = ["serde_derive"] std = [] unstable = [] diff --git a/serde/src/lib.rs b/serde/src/lib.rs index 36eb6723..cfd2f7d0 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -106,3 +106,50 @@ mod utils; // Generated code uses these to support no_std. Not public API. #[doc(hidden)] pub mod export; + +// Re-export #[derive(Serialize, Deserialize)]. +// +// This is a workaround for https://github.com/rust-lang/cargo/issues/1286. +// Without this re-export, crates that put Serde derives behind a cfg_attr would +// need to use some silly feature name that depends on both serde and +// serde_derive. +// +// [features] +// serde-impls = ["serde", "serde_derive"] +// +// [dependencies] +// serde = { version = "1.0", optional = true } +// serde_derive = { version = "1.0", optional = true } +// +// # Used like this: +// # #[cfg(feature = "serde-impls")] +// # #[macro_use] +// # extern crate serde_derive; +// # +// # #[cfg_attr(feature = "serde-impls", derive(Serialize, Deserialize))] +// # struct S { /* ... */ } +// +// The re-exported derives allow crates to use "serde" as the name of their +// Serde feature which is more intuitive. +// +// [dependencies] +// serde = { version = "1.0", optional = true, features = ["derive"] } +// +// # Used like this: +// # #[cfg(feature = "serde")] +// # #[macro_use] +// # extern crate serde; +// # +// # #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +// # struct S { /* ... */ } +// +// The reason re-exporting is not enabled by default is that disabling it would +// be annoying for crates that provide handwritten impls or data formats. They +// would need to disable default features and then explicitly re-enable std. +#[cfg(feature = "serde_derive")] +#[allow(unused_imports)] +#[macro_use] +extern crate serde_derive; +#[cfg(feature = "serde_derive")] +#[doc(hidden)] +pub use serde_derive::*;