Skip to content

Commit

Permalink
Allow no_std use with Default and PhantomData
Browse files Browse the repository at this point in the history
  • Loading branch information
birkenfeld committed Nov 11, 2018
1 parent 891f7cc commit 2683225
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Expand Up @@ -14,5 +14,9 @@ proc-macro2 = "0.4"
quote = "0.6"
syn = "0.15"

[features]
default = ["std"]
std = []

[workspace]
members = ["testcrate"]
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -10,6 +10,8 @@ way to initialise a struct or an enum.
Implementation uses macros 1.1 custom derive (which works in stable Rust from
1.15 onwards).

`#[no_std]` is fully supported if you switch off the default feature `"std"`.

## Examples

Cargo.toml:
Expand Down
12 changes: 10 additions & 2 deletions src/lib.rs
Expand Up @@ -254,7 +254,11 @@ enum FieldAttr {
impl FieldAttr {
pub fn as_tokens(&self) -> proc_macro2::TokenStream {
match *self {
FieldAttr::Default => my_quote!(::std::default::Default::default()),
FieldAttr::Default => if cfg!(feature = "std") {
my_quote!(::std::default::Default::default())
} else {
my_quote!(::core::default::Default::default())
},
FieldAttr::Value(ref s) => my_quote!(#s),
}
}
Expand Down Expand Up @@ -371,7 +375,11 @@ impl<'a> FieldExt<'a> {
pub fn as_init(&self) -> proc_macro2::TokenStream {
let f_name = &self.ident;
let init = if self.is_phantom_data() {
my_quote!(::std::marker::PhantomData)
if cfg!(feature = "std") {
my_quote!(::std::marker::PhantomData)
} else {
my_quote!(::core::marker::PhantomData)
}
} else {
match self.attr {
None => my_quote!(#f_name),
Expand Down

0 comments on commit 2683225

Please sign in to comment.