English 中文(简体)
rust macro: combine params with literal string as function name error
原标题:
  • 时间:2023-05-25 03:00:42
  •  标签:
  • rust
  • macros

I create a macro in rust:

macro_rules! generate_i64_get_inc{
        ($($field_name:ident),*) => {
        $(
            pub(super) fn get_$field_name(&self) -> i64 {
                self. $field_name.load(Ordering::Acquire)
            }

            pub(super) fn inc_$field_name(&mut self, value: i64) -> i64 {
                self. $field_name.fetch_add(value, Ordering::AcqRel) + value
            }
        )*
    };
}

and invoke it:

impl TxStats {
    generate_i64_get_inc!(pg_alloc, pg_count);
}

but get error on get_$field_name:

expected one of ( or <, found pg_alloc

I hope it works well.

问题回答

暂无回答




相关问题
C macro processing

I m thinking about best way to write C define processor that would be able to handle macros. Unfortunately nothing intelligent comes to my mind. It should behave exactly like one in C, so it handles ...

Macros as arguments to preprocessor directives

Being faced with the question whether it s possible to choose #includes in the preprocessor I immediately thought not possible. .. Only to later find out that it is indeed possible and you only need ...

Is there a profitable way to record user actions in textarea?

I need to send bunch of commands to the server on timer - like: put(0,"hello") del(4,1) put(4," is around the corner") so I need to monitor and record all of the user input and compile/flush it on ...

taking java method names as function arg in clojure

All, I want to create a function that takes a symbol representing a java method and applies it to some object: (user=> (defn f [m] (. "foo" (m))) When I execute this, I get a result much ...

Removing macro in legacy code

I have a lot of legacy code using macro of the form: #define FXX(x) pField->GetValue(x) The macro forces variable pField be in the scope: ..... FIELD *pField = .... ..... int i = FXX(3); int j = ...

eval during emacs lisp macro expansion

How can I fix the simple macro foo in (elisp)Eval During Expansion? None of the followings work: (defmacro foo1 (a) `(setq (eval ,a) t)) (defmacro foo2 (a) `(setq ,(eval a) t)) (defmacro foo3 (...

热门标签