Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider using an attribute macro for payload procedures #16

Open
NotNite opened this issue Jul 11, 2023 · 1 comment
Open

Consider using an attribute macro for payload procedures #16

NotNite opened this issue Jul 11, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@NotNite
Copy link

NotNite commented Jul 11, 2023

Hiya! Big fan of dll-syringe, used it in countless projects. Something that's bugged me about the payload-procedure macro is the fact it's function-like, requiring me to wrap my function in an indent and also lose formatting. I found myself making a <function name>_inner function and then calling it from the remote procedure so I could have rustfmt work again.

Out of boredom today I wrote an attribute macro that you can slap on your functions. It produces nearly the exact same result as the current macro, but it's just one extra line and doesn't affect formatting. The only thing that is different is an addition of braces inside of the function, which doesn't seem to affect anything. I've tested this in my own project (closed source so I can't link an example - sorry!), and it seems to work great.

The source for this is available in this gist (42 lines). Is this something worth PRing? Noting that it adds dependencies on quote and syn - also, this is my first time using those libraries, so I'm not entirely sure if I'm doing something in a bad way.

Here's a before and after viewed with cargo-expand, on both the current macro and my macro:

#[remote_procedure]
fn rpc_function_one(number: u32) -> bool {
    number % 2 == 0
}

dll_syringe::payload_procedure! {
    fn rpc_function_two(number: u32) -> bool {
        number % 2 == 0
    }
}
#[no_mangle]
pub unsafe extern "system" fn rpc_function_one(
    __args_and_params: *mut ::core::ffi::c_void,
) {
    ::dll_syringe::payload_utils::__payload_procedure_helper(
        __args_and_params,
        |__args| {
            let (number,) = __args;
            fn __inner(number: u32) -> bool {
                { number % 2 == 0 }
            }
            __inner(number)
        },
    );
}

#[no_mangle]
pub unsafe extern "system" fn rpc_function_two(
    __args_and_params: *mut ::core::ffi::c_void,
) {
    ::dll_syringe::payload_utils::__payload_procedure_helper(
        __args_and_params,
        |__args| {
            let (number,) = __args;
            fn __inner(number: u32) -> bool {
                number % 2 == 0
            }
            __inner(number)
        },
    );
}
@OpenByteDev OpenByteDev added the enhancement New feature or request label Jul 12, 2023
@OpenByteDev
Copy link
Owner

An attribute macro would certainly be nicer, I just didn't bother to make a seperate macro crate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants