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

Cannot add prefixes or suffixes to macro-generated function names #28984

Closed
ebkalderon opened this issue Oct 12, 2015 · 3 comments
Closed

Cannot add prefixes or suffixes to macro-generated function names #28984

ebkalderon opened this issue Oct 12, 2015 · 3 comments

Comments

@ebkalderon
Copy link
Contributor

If I write a macro intended to generate new functions at compile time, I am able to name the functions according to individual idents, but I cannot annotate or concatenate anything onto the ident if I wish.

Consider the code below:

#[macro_export]
macro_rules! components {
    ( $($cfield:ident => $component:ident),+ ) => {
        pub struct ComponentManager {
            $(pub $cfield: ComponentMap<$component>,)+
        }

        impl ComponentManager {
            pub fn new() -> ComponentManager {
                use std::collections::HashMap;
                ComponentManager {
                    $($cfield: HashMap::new(),)+
                }
            }

            // This below is the code in question!
            $(
            pub fn $cfield(&self) {
                println!("{}", self.$cfield.len()); // Placeholder
            }
            )+

            pub fn delete_all_belonging_to(&mut self, id: EntityId) {
                $(self.$cfield.remove(&id);)+
            }
        }
    };
}

Assuming that I write the following:

pub struct Position {
    pub x: f32,
    pub y: f32,
    pub z: f32
}

components! {
    positions => Position
}

then the ComponentManager now has a positions(&self) method. But if I wanted the method to be called print_positions(&self), and in the macro I were to try to implement the code in question like this:

// This below is the code in question!
$(
pub fn print_$cfield(&self) {
    println!("{}", self.$cfield.len()); // Placeholder
}
)+

then I am presented with the following error:

ecs/mod.rs:53:27: 53:34 error: expected one of `(` or `<`, found `light`
ecs/mod.rs:53             pub fn print_$cfield(&self) {
                                        ^~~~~~~
ecs/mod.rs:53:27: 53:34 error: expected one of `(` or `<`, found `light`
ecs/mod.rs:53             pub fn print_$cfield(&self) {
                                        ^~~~~~~
Build failed, waiting for other jobs to finish...
Could not compile `project`.

TL; DR: When using macros to generate function names, I am forced to use individual identifiers and cannot prefix/suffix them with other text. Is this behavior a bug in my code, a bug in rustc, or by design?

@eefriedman
Copy link
Contributor

Dup of #12249.

@alexcrichton
Copy link
Member

Indeed, thanks @eefriedman

@ebkalderon
Copy link
Contributor Author

Whoops! Sorry for the duplicate issue. I will be following #12249 very closely; hope it gets resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants