Skip to content

Commit 0227156

Browse files
committed
Fix no_std support if serde is depended on
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
1 parent fc199d0 commit 0227156

File tree

7 files changed

+57
-16
lines changed

7 files changed

+57
-16
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ matrix:
2424
# Need to cd because of https://github.com/rust-lang/cargo/issues/5364
2525
- (cd num_enum && cargo build --target=thumbv6m-none-eabi --no-default-features -p num_enum)
2626
- (cd renamed_num_enum && cargo build --target=thumbv6m-none-eabi --no-default-features -p renamed_num_enum --lib)
27+
# Regression test for https://github.com/illicitonion/num_enum/issues/18
28+
- (cd serde_example && cargo build --target=thumbv6m-none-eabi -p serde_example --lib)
2729
- name: "cargo fmt"
2830
rust: stable
2931
script:

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["num_enum", "num_enum_derive", "renamed_num_enum"]
2+
members = ["num_enum", "num_enum_derive", "renamed_num_enum", "serde_example"]
33
# Exclude num_enum_derive because its useful doc comments import num_enum, which the crate doesn't do (because it would
44
# cause a circular dependency), so the doc tests don't actually compile.
5-
default-members = ["num_enum", "renamed_num_enum"]
5+
default-members = ["num_enum", "renamed_num_enum", "serde_example"]

num_enum_derive/Cargo.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ license = "BSD-3-Clause"
1616
proc-macro = true
1717

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

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

2832
[dependencies]
2933
proc-macro2 = "1"
30-
proc-macro-crate = "0.1.4"
34+
proc-macro-crate = { version = "0.1.4", optional = true }
3135
quote = "1"
3236
syn = "1"

num_enum_derive/src/lib.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,7 @@ pub fn derive_try_from_primitive(input: TokenStream) -> TokenStream {
183183
let (match_const_exprs, enum_keys): (Vec<Expr>, Vec<Ident>) =
184184
value_expressions_to_enum_keys.into_iter().unzip();
185185

186-
let krate = Ident::new(
187-
&::proc_macro_crate::crate_name("num_enum")
188-
.map(::std::borrow::Cow::from)
189-
.unwrap_or_else(|err| {
190-
eprintln!("Warning: {}\n => defaulting to `num_enum`", err,);
191-
"num_enum".into()
192-
}),
193-
Span::call_site(),
194-
);
186+
let krate = Ident::new(&get_crate_name(), Span::call_site());
195187

196188
TokenStream::from(quote! {
197189
impl ::#krate::TryFromPrimitive for #name {
@@ -244,6 +236,25 @@ pub fn derive_try_from_primitive(input: TokenStream) -> TokenStream {
244236
})
245237
}
246238

239+
#[cfg(feature = "proc-macro-crate")]
240+
fn get_crate_name() -> String {
241+
::proc_macro_crate::crate_name("num_enum").unwrap_or_else(|err| {
242+
eprintln!("Warning: {}\n => defaulting to `num_enum`", err,);
243+
String::from("num_enum")
244+
})
245+
}
246+
247+
// Don't depend on proc-macro-crate in no_std environments because it causes an awkward depndency
248+
// on serde with std.
249+
//
250+
// no_std dependees on num_enum cannot rename the num_enum crate when they depend on it. Sorry.
251+
//
252+
// See https://github.com/illicitonion/num_enum/issues/18
253+
#[cfg(not(feature = "proc-macro-crate"))]
254+
fn get_crate_name() -> String {
255+
String::from("num_enum")
256+
}
257+
247258
/// Generates a `unsafe fn from_unchecked (number: Primitive) -> Self`
248259
/// associated function.
249260
///

renamed_num_enum/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ version = "0.0.0"
44
edition = "2018"
55
publish = false
66

7-
[features]
8-
std = ["renamed/std"]
9-
107
[dependencies.renamed]
118
package = "num_enum"
129
path = "../num_enum"
1310
default-features = false
11+
features = ["std"]

serde_example/Cargo.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "serde_example"
3+
version = "0.1.0"
4+
authors = [
5+
"Daniel Wagner-Hall <dawagner@gmail.com>",
6+
"Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>",
7+
]
8+
description = "Example crate using num_enum and serde. Regression test for https://github.com/illicitonion/num_enum/issues/18."
9+
edition = "2018"
10+
repository = "https://github.com/illicitonion/num_enum"
11+
publish = false
12+
13+
[dependencies]
14+
num_enum = { path = "../num_enum", default-features = false }
15+
serde = { version = "1", default_features = false, features = ["derive"] }

serde_example/src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![no_std]
2+
3+
use num_enum::{IntoPrimitive, TryFromPrimitive, UnsafeFromPrimitive};
4+
use serde::{Deserialize, Serialize};
5+
6+
#[derive(Deserialize, IntoPrimitive, Serialize, TryFromPrimitive, UnsafeFromPrimitive)]
7+
#[repr(u8)]
8+
pub enum Number {
9+
Zero,
10+
One,
11+
}

0 commit comments

Comments
 (0)