-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4e0c5bc
Showing
6 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "null_fn" | ||
version = "0.1.0" | ||
edition = "2018" | ||
authors = ["William Venner <[email protected]>"] | ||
license = "MIT" | ||
repository = "https://github.com/WilliamVenner/null_fn" | ||
description = "A proc attribute macro that allows for creating null function pointers in statics" | ||
categories = ["development-tools::ffi"] | ||
keywords = ["unsafe", "ffi", "null", "pointer", "static"] | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "1" | ||
syn = { version = "1", features = ["full"] } | ||
quote = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 William Venner | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# ✨ `null_fn` | ||
|
||
A proc attribute macro that allows for creating null function pointers in `static`s. | ||
|
||
## Example | ||
|
||
```rust | ||
static mut UTIL_PlayerByUserId: unsafe extern "C" fn(userid: i32) -> *mut c_void = unsafe { std::mem::transmute::<*const (), _>(std::ptr::null()) }; // error[E0080]: it is undefined behavior to use this value | ||
|
||
#[null_fn] | ||
static mut UTIL_PlayerByUserId: unsafe extern "C" fn(userid: i32) -> *mut c_void = std::ptr::null(); // works! | ||
|
||
fn main() { | ||
unsafe { | ||
UTIL_PlayerByUserId(20); // This would panic, as the pointer is NULL. | ||
|
||
let is_null = UTIL_PlayerByUserId.is_null(); // It's just a normal pointer, so we can call pointer-related functions on it. | ||
|
||
UTIL_PlayerByUserId = /* magically find the pointer to the function */; | ||
let player = UTIL_PlayerByUserId(20); // Now that we set the function pointer, we can call the function without panicking. | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#![doc = include_str!("../README.md")] | ||
|
||
use proc_macro::TokenStream; | ||
use proc_macro2::Ident; | ||
use quote::{quote, format_ident}; | ||
use syn::{Generics, ItemFn, ItemStatic, Signature, Token, parse_macro_input, spanned::Spanned}; | ||
|
||
#[proc_macro_attribute] | ||
#[doc = include_str!("../README.md")] | ||
pub fn null_fn(_: TokenStream, input: TokenStream) -> TokenStream { | ||
let mut static_item = parse_macro_input!(input as ItemStatic); | ||
|
||
let null_fn = ItemFn { | ||
attrs: vec![], | ||
vis: syn::parse_str("pub(super)").unwrap(), | ||
block: syn::parse_str("{ panic!(\"This function has not been initialized yet\") }").unwrap(), | ||
sig: Signature { | ||
constness: None, | ||
asyncness: None, | ||
unsafety: Some(Token![unsafe](static_item.span())), | ||
abi: None, | ||
fn_token: Default::default(), | ||
ident: Ident::new("null_fn", static_item.span()), | ||
generics: Generics::default(), | ||
paren_token: Default::default(), | ||
inputs: Default::default(), | ||
variadic: Default::default(), | ||
output: syn::ReturnType::Default | ||
}, | ||
}; | ||
|
||
static_item.expr = syn::parse_str(&format!("unsafe{{std::mem::transmute(__null_fn__{}::null_fn as *const ())}}", static_item.ident)).unwrap(); | ||
|
||
let mod_name = format_ident!("__null_fn__{}", static_item.ident); | ||
quote!( | ||
#[allow(non_snake_case)] | ||
mod #mod_name { | ||
#[allow(unused_variables)] | ||
#null_fn | ||
} | ||
#static_item | ||
).into() | ||
} |