Skip to content

Commit

Permalink
Remove use of decl_macro by using a different hack.
Browse files Browse the repository at this point in the history
  • Loading branch information
jebrosen committed May 9, 2019
1 parent 7c2dda2 commit 5867389
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ indexmap = "1.0"
quote = "0.6.1"
rocket_http = { version = "0.4.0", path = "../http/" }
devise = "0.2"
rand = "0.6.5"

[build-dependencies]
yansi = "0.5"
Expand Down
19 changes: 14 additions & 5 deletions core/codegen/src/attribute/route.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use rand::{Rng, distributions::Alphanumeric};

use proc_macro::{TokenStream, Span};
use proc_macro2::TokenStream as TokenStream2;
use devise::{syn, Spanned, SpanWrapped, Result, FromMeta, ext::TypeExt};
Expand Down Expand Up @@ -321,15 +323,22 @@ fn generate_internal_uri_macro(route: &Route) -> TokenStream2 {
.map(|name| route.inputs.iter().find(|(ident, ..)| ident == name).unwrap())
.map(|(ident, _, ty)| quote!(#ident: #ty));

let random_part = rand::thread_rng().sample_iter(&Alphanumeric).take(8).collect::<String>();

let generated_macro_name = route.function.ident.prepend(URI_MACRO_PREFIX);
let inner_generated_macro_name = generated_macro_name.append(&random_part);
let route_uri = route.attribute.path.origin.0.to_string();

quote! {
pub macro #generated_macro_name($($token:tt)*) {{
extern crate std;
extern crate rocket;
rocket::rocket_internal_uri!(#route_uri, (#(#dynamic_args),*), $($token)*)
}}
#[macro_export]
macro_rules! #inner_generated_macro_name {
($($token:tt)*) => {{
extern crate std;
extern crate rocket;
rocket::rocket_internal_uri!(#route_uri, (#(#dynamic_args),*), $($token)*)
}};
}
pub use #inner_generated_macro_name as #generated_macro_name;
}
}

Expand Down
1 change: 1 addition & 0 deletions core/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#[macro_use] extern crate quote;
extern crate devise;
extern crate proc_macro;
extern crate rand;
extern crate rocket_http as http;
extern crate indexmap;

Expand Down
5 changes: 5 additions & 0 deletions core/codegen/src/syn_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ pub fn syn_to_diag(error: syn::parse::Error) -> Diagnostic {

pub trait IdentExt {
fn prepend(&self, string: &str) -> syn::Ident;
fn append(&self, string: &str) -> syn::Ident;
}

impl IdentExt for syn::Ident {
fn prepend(&self, string: &str) -> syn::Ident {
syn::Ident::new(&format!("{}{}", string, self), self.span())
}

fn append(&self, string: &str) -> syn::Ident {
syn::Ident::new(&format!("{}{}", self, string), self.span())
}
}

pub trait ReturnTypeExt {
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_2018/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(proc_macro_hygiene)]

#[cfg(test)] mod tests;

Expand Down

0 comments on commit 5867389

Please sign in to comment.