structx icon indicating copy to clipboard operation
structx copied to clipboard

nested macros panic

Open roee30 opened this issue 1 year ago • 0 comments

First of all, thank you for this library! The lack of this feature in rust prevents some useful applications of type computation.

Unfortunately, the following code, which uses structx! inside another macro, makes the compiler panic upon macro expansion with called `Result::unwrap()` on an `Err` value: Error("expected identifier or integer")

#![allow(semicolon_in_expressions_from_macros, unused_imports, dead_code)]
use bson::oid::ObjectId;
use std::fmt::Debug;
use structx::*;
#[derive(Debug, Default)]
struct Post {
    id: ObjectId,
    likes: u32,
    user: String,
    contents: String,
}
fn debug<T: Debug>(x: &T) {
    println!("{:?}", x);
}
fn default<T: Default>() -> T {
    Default::default()
}
macro_rules! projection {
    ($source:expr, $($field:ident)*, $last:ident) => {
        $source.into_iter().map(|s|
            structx! {
                ($($field: s.$field),*),
                $last: s.$last
            }
        ).collect::<Vec<_>>()
    }
}

pub fn main() {
    let ps: Vec<Post> = vec![default(), default()];
    let ps = projection!(ps, likes, user);
    debug(&ps);
}

Complete log attached log.txt

roee30 avatar Jan 15 '25 18:01 roee30