Skip to content

Commit

Permalink
Fix no_std support if serde is depended on
Browse files Browse the repository at this point in the history
The proc-macro-crate depends on toml, which in turn depends on serde
_with_ std. Only depend on proc-macro-crate if std is enabled.

This means that no_std consumer of num_enum cannot rename their num_enum
dependency. This seems like a reasonable restriction.

Works around rust-lang/cargo#5730
  • Loading branch information
illicitonion committed May 8, 2020
1 parent fc199d0 commit 34012e6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ matrix:
# Need to cd because of https://github.com/rust-lang/cargo/issues/5364
- (cd num_enum && cargo build --target=thumbv6m-none-eabi --no-default-features -p num_enum)
- (cd renamed_num_enum && cargo build --target=thumbv6m-none-eabi --no-default-features -p renamed_num_enum --lib)
# Regression test for https://github.com/illicitonion/num_enum/issues/18
- (cd serde_example && cargo build --target=thumbv6m-none-eabi -p serde_example --lib)
- name: "cargo fmt"
rust: stable
script:
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["num_enum", "num_enum_derive", "renamed_num_enum"]
members = ["num_enum", "num_enum_derive", "renamed_num_enum", "serde_example"]
# Exclude num_enum_derive because its useful doc comments import num_enum, which the crate doesn't do (because it would
# cause a circular dependency), so the doc tests don't actually compile.
default-members = ["num_enum", "renamed_num_enum"]
default-members = ["num_enum", "renamed_num_enum", "serde_example"]
8 changes: 6 additions & 2 deletions num_enum_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ license = "BSD-3-Clause"
proc-macro = true

[features]
std = []
# Don't depend on proc-macro-crate in no_std environments because it causes an awkward depndency
# on serde with std.
#
# See https://github.com/illicitonion/num_enum/issues/18
std = ["proc-macro-crate"]
complex-expressions = ["syn/full"]
external_doc = []

Expand All @@ -27,6 +31,6 @@ features = ["external_doc"]

[dependencies]
proc-macro2 = "1"
proc-macro-crate = "0.1.4"
proc-macro-crate = { version = "0.1.4", optional = true }
quote = "1"
syn = "1"
29 changes: 20 additions & 9 deletions num_enum_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,7 @@ pub fn derive_try_from_primitive(input: TokenStream) -> TokenStream {
let (match_const_exprs, enum_keys): (Vec<Expr>, Vec<Ident>) =
value_expressions_to_enum_keys.into_iter().unzip();

let krate = Ident::new(
&::proc_macro_crate::crate_name("num_enum")
.map(::std::borrow::Cow::from)
.unwrap_or_else(|err| {
eprintln!("Warning: {}\n => defaulting to `num_enum`", err,);
"num_enum".into()
}),
Span::call_site(),
);
let krate = Ident::new(&get_crate_name(), Span::call_site());

TokenStream::from(quote! {
impl ::#krate::TryFromPrimitive for #name {
Expand Down Expand Up @@ -244,6 +236,25 @@ pub fn derive_try_from_primitive(input: TokenStream) -> TokenStream {
})
}

#[cfg(feature = "proc-macro-crate")]
fn get_crate_name() -> String {
::proc_macro_crate::crate_name("num_enum").unwrap_or_else(|err| {
eprintln!("Warning: {}\n => defaulting to `num_enum`", err,);
String::from("num_enum")
})
}

// Don't depend on proc-macro-crate in no_std environments because it causes an awkward depndency
// on serde with std.
//
// no_std dependees on num_enum cannot rename the num_enum crate when they depend on it. Sorry.
//
// See https://github.com/illicitonion/num_enum/issues/18
#[cfg(not(feature = "proc-macro-crate"))]
fn get_crate_name() -> String {
String::from("num_enum")
}

/// Generates a `unsafe fn from_unchecked (number: Primitive) -> Self`
/// associated function.
///
Expand Down
15 changes: 15 additions & 0 deletions serde_example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "serde_example"
version = "0.1.0"
authors = [
"Daniel Wagner-Hall <dawagner@gmail.com>",
"Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>",
]
description = "Example crate using num_enum and serde. Regression test for https://github.com/illicitonion/num_enum/issues/18."
edition = "2018"
repository = "https://github.com/illicitonion/num_enum"
publish = false

[dependencies]
num_enum = { path = "../num_enum", default-features = false }
serde = { version = "1", default_features = false, features = ["derive"] }
11 changes: 11 additions & 0 deletions serde_example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![no_std]

use num_enum::{IntoPrimitive, TryFromPrimitive, UnsafeFromPrimitive};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, IntoPrimitive, Serialize, TryFromPrimitive, UnsafeFromPrimitive)]
#[repr(u8)]
pub enum Number {
Zero,
One,
}

0 comments on commit 34012e6

Please sign in to comment.