是否可以编写一个计算项目并生成枚举的宏?

时间:2016-08-19 20:54:37

标签: macros rust

我想要这样的事情:

/* { package, some more imports... } */

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.cache.annotation.Cacheable;

@Aspect
public class GetPropertyInterceptor
{
    @Around( "call(* *.getProperty(..))" )
    @Cacheable( cacheManager = "nonExistingCacheManager", value = "thisShouldBlowUp", key = "#nosuchkey" )
    public Object intercepting( ProceedingJoinPoint pjp ) throws Throwable
    {
        Object o;
        /* { modify o } */
        return o;
    }
}

生产:

define_enum_and_all_variants! ( Test {
    One, Two, Three
});

这里的问题是enum Test { One, Two, Three } const ALL_VARIANTS: [Test; 3] = [One, Two, Three]; ,我可以这样写:

3

但是如何计算macro_rules! define_enum_and_all_variants { ($Name:ident { $($Variant:ident),* }) => { #[derive(Debug)] enum $Name { $($Variant),*, } #[allow(dead_code)] const ALL_VARIANTS: [$Name; 3] = [$($Name::$Variant),*]; } } 中的元素数量?

1 个答案:

答案 0 :(得分:4)

这并没有真正回答这个问题,只是因为你有一个xy problem

而不是写

<%= form_for @message do |f| %>
你可以写

const ALL: [u32; 3] = [1, 2, 3];

因此你的宏应该是

const ALL: &'static [u32] = &[1, 2, 3];

回答另一个问题(“如何用宏计算”):这是一种简单的头尾式算法:

const ALL_VARIANTS: &'static [$Name] = &[$($Name::$Variant),*];