Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syn type/struct that represents the nested field offset parameter to offset_of! macro?

If I want to wrap a macro around offset_of!'s nested form, e.g. offset_of!(Foo, x.y.VariantA.0), what should x.y.VariantA.0 get parsed as in syn terms (e.g. which of these https://docs.rs/syn/latest/syn/index.html#structs)?

The closest seems to be ExprField, but afaict that represents just a single field, not the sequence.

like image 911
ajp Avatar asked Oct 29 '25 15:10

ajp


1 Answers

ExprField, its base can be any Expr, including Expr::Field(ExprField) and thus allows recursive parsing of "x.y.VariantA.0":

fn main() {
    let x: Result<syn::ExprField, _> = syn::parse2("x.y.VariantA.0".parse().unwrap());
    dbg!(x);
}

Relevant excerpt of the output:

[src/main.rs:3:5] x = Ok(
    ExprField {
        base: Expr::Field {
            base: Expr::Field {
                base: Expr::Path { x },
                member: Member::Named( y ),
            },
            member: Member::Named( VariantA ),
        },
        member: Member::Unnamed( 0 ),
    },
)

Playground

like image 96
cafce25 Avatar answered Nov 01 '25 14:11

cafce25



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!