Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix no_std support if serde is depended on #19

Merged
merged 1 commit into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ matrix:
- rustup target add thumbv6m-none-eabi
# 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
4 changes: 1 addition & 3 deletions renamed_num_enum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ version = "0.0.0"
edition = "2018"
publish = false

[features]
std = ["renamed/std"]

[dependencies.renamed]
package = "num_enum"
path = "../num_enum"
default-features = false
features = ["std"]
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 <[email protected]>",
"Daniel Henry-Mantilla <[email protected]>",
]
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,
}