Giter Site home page Giter Site logo

williamvenner / null_fn Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 5 KB

✨ A proc attribute macro that allows for creating null function pointers in statics

Home Page: https://crates.io/crates/null_fn

License: MIT License

Rust 100.00%
proc-macro proc-macro-attributes rust macro null function pointer ptr fn ffi

null_fn's Introduction

crates.io

null_fn

A proc attribute macro that allows for creating null function pointers in statics.

This crate is unsafe and easy to cause UB with, Option<fn()> is FFI safe and may be a more appropriate alternative if you value type safety.

Example

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 we have not initialized the function yet. By default the function is set to a small stub function that panics when called.

        UTIL_PlayerByUserId = /* magically find the pointer to the function; sigscan? */;
		// Setting the function's pointer to a null pointer is UB in Rust.
		// https://doc.rust-lang.org/nomicon/ffi.html#the-nullable-pointer-optimization

        let player = UTIL_PlayerByUserId(20); // Now that we set the function pointer, we can call the function without panicking, assuming we found the pointer correctly.

		/* do something with our player! */
    }
}

null_fn's People

Contributors

williamvenner avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.