#`cargo-extern-fn` Proc-macro code review

16 messages · Page 1 of 1 (latest)

broken socket
#

Hey, I've been hacking around proc-macro for a few days due to some need would like some guidance on how to best combines syn and quote: https://github.com/kraktus/cargo-extern-fn/tree/aab6bfc414c0f15ef85f1a1037162402fb3b4930 (code only living at src/main.rs)

I'm especially unpleased by call_function_from_sig and union internal functions, but more generally how to combine TokenStream together. For now I'm mostly using String back and forth, something re-quoteing, sometimes parsing, this does not feel very organised.

Thank you for looking into it, I know this type of code can be rather dense, and in my case has not been polished.

pallid depot
#

I think you should be able to do something along the lines of:

let fn_ident = {
    let Sig { ident, .. } = &sig;
    let ty =
        self.current_impl_ty.as_ref()
            // trick to make the `Option` be `#( … )*`-interpolable
            .map_or(&[][..], ::core::slice_from_ref)
    ;
    quote!(
        #( < #ty > ::)* 
        #ident
    )
};
broken socket
#

That's some quote! magic! 😄 Nice thanks

broken socket
#

An annoying issue is that syn removes comment and transform doc comments /// doc into doc comment attribute #[doc = "doc"]

pallid depot
#

Yeah, comments are not part of Rustc tokens ferrisPensive

broken socket
#

When parsing back is it possible to tell syn/quote to remap doc comments to use the /// doc syntax? Or need to be post-processed manually

pallid depot
#

?eval "/// test".parse::<::proc_macro2::TokenStream>().unwrap().to_string()

timid juniperBOT
#
     Running `target/debug/playground`

"# [doc = \" test\"]"
pallid depot
#

Yeah, it looks like TokenStream's own .to_string() logic use #[doc = "…"], so you'd need to preprocess manually

#

?fmt #[doc = "test"] extern {}

timid juniperBOT
#
#[doc = "test"]
extern "C" {}
pallid depot
#

Hmm, I could have hoped that a formatter would do this, but it doesn't seem to be the case ferrisPensive

#

Ah, wait, maybe prettyplease does