For example this is what I want to achieve:
// some enum type in other crate actually
enum SomeType {
Type1,
Type2,
Type3,
}
struct Wrapper(SomeType);
#[pyclass]
impl Wrapper {
#[pystaticmethod]
fn some_other_functions() {}
// I want to generate the following functions,
// one for each variant in SomeType
#[pystaticmethod]
fn get_type1() -> Wrapper {
Wrapper(SomeType::Type1)
}
#[pystaticmethod]
fn get_type2() -> Wrapper {
Wrapper(SomeType::Type2)
}
#[pystaticmethod]
fn get_type3() -> Wrapper {
Wrapper(SomeType::Type3)
}
}
Some ways I have tried:
proc-macro-derive
but I have no access to the TokenStream of the enumbuild.rs
to generate thefn
code to some file, then include it in the impl block. Then I got error
non-impl item macro in impl item position: include
- use
build.rs
to generate the whole impl block, this works but it seems hard to add other functions to it if I want (multiple types like this would be implemented, maybe some of them needs additional methods). - have multiple impl blocks with
#[pyclass]
failed to compile
Haven't tried yet but am thinking of using build.rs
to generate the variant info (e.g. Vec<String>
) to some const
, then with a derive proc-macro to populate the needed fn
Is there any better way here?