#function #macro #argument #conditional-compilation #attributes #proc #specify

macro fn_has_this

A proc attribute macro that allows you to specify whether a function has a this argument (for conditional compilation)

1 unstable release

0.1.1 Sep 26, 2021
0.1.0 Sep 26, 2021

#17 in #conditional-compilation

Download history 5/week @ 2023-11-30 1/week @ 2023-12-07 10/week @ 2023-12-14 10/week @ 2023-12-21 2/week @ 2023-12-28 16/week @ 2024-01-04 35/week @ 2024-01-11 10/week @ 2024-01-18 265/week @ 2024-01-25 111/week @ 2024-02-01 22/week @ 2024-02-08 110/week @ 2024-02-15 135/week @ 2024-02-22 64/week @ 2024-02-29 99/week @ 2024-03-07 38/week @ 2024-03-14

384 downloads per month
Used in 2 crates (via gmod)

MIT license

6KB
80 lines

crates.io

fn_has_this

A proc attribute macro that adds a this argument to a function.

This macro is well suited for conditional compilation. For example, some compilers will optimize away a this argument in a non-static function if it is unused, sometimes depending on calling convention. This macro makes it easy to conditionally compile your Rust programs to interact over FFI with functions compiled in this way.

Example

#[macro_use]
extern crate fn_has_this;

use core::ffi::c_void;

#[has_this("*mut c_void")] // Will create an argument called `this` in the function
pub unsafe extern "fastcall" fn print_pointer() {
    println!("{:x?}", this);
}

#[has_this("me: *mut c_void")] // You can also specify a custom name for the `this` argument
pub unsafe extern "thiscall" fn print_pointer() {
    println!("{:x?}", me);
}

#[cfg_attr(target_os = "windows", has_this("me: *mut c_void"))] // Using `cfg_attr`, you can conditionally compile this attribute macro
pub unsafe extern "thiscall" fn print_pointer() {
    println!("{:x?}", me);
}

Dependencies

~1.5MB
~34K SLoC