{"id":15762372,"url":"https://github.com/williamvenner/null_fn","last_synced_at":"2025-03-31T09:41:44.739Z","repository":{"id":57646990,"uuid":"409767819","full_name":"WilliamVenner/null_fn","owner":"WilliamVenner","description":"✨ A proc attribute macro that allows for creating null function pointers in statics","archived":false,"fork":false,"pushed_at":"2021-09-24T00:31:02.000Z","size":5,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-11T11:33:28.129Z","etag":null,"topics":["ffi","fn","function","macro","null","pointer","pointers","proc-macro","proc-macro-attributes","ptr","rust","unsafe"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/null_fn","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WilliamVenner.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-23T23:07:00.000Z","updated_at":"2021-11-07T18:10:42.000Z","dependencies_parsed_at":"2022-09-19T06:20:42.009Z","dependency_job_id":null,"html_url":"https://github.com/WilliamVenner/null_fn","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WilliamVenner%2Fnull_fn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WilliamVenner%2Fnull_fn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WilliamVenner%2Fnull_fn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WilliamVenner%2Fnull_fn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WilliamVenner","download_url":"https://codeload.github.com/WilliamVenner/null_fn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246450425,"owners_count":20779406,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ffi","fn","function","macro","null","pointer","pointers","proc-macro","proc-macro-attributes","ptr","rust","unsafe"],"created_at":"2024-10-04T11:08:56.407Z","updated_at":"2025-03-31T09:41:44.713Z","avatar_url":"https://github.com/WilliamVenner.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![crates.io](https://img.shields.io/crates/v/null_fn.svg)](https://crates.io/crates/null_fn)\n\n# ✨ `null_fn`\n\nA proc attribute macro that allows for creating null function pointers in `static`s.\n\nThis crate is **unsafe and easy to cause UB with**, `Option\u003cfn()\u003e` is [FFI safe](https://doc.rust-lang.org/nomicon/ffi.html#the-nullable-pointer-optimization) and may be a more appropriate alternative if you value type safety.\n\n## Example\n\n```rust\nstatic mut UTIL_PlayerByUserId: unsafe extern \"C\" fn(userid: i32) -\u003e *mut c_void = unsafe { std::mem::transmute::\u003c*const (), _\u003e(std::ptr::null()) }; // error[E0080]: it is undefined behavior to use this value\n\n#[null_fn]\nstatic mut UTIL_PlayerByUserId: unsafe extern \"C\" fn(userid: i32) -\u003e *mut c_void = std::ptr::null(); // works!\n\nfn main() {\n    unsafe {\n        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.\n\n        UTIL_PlayerByUserId = /* magically find the pointer to the function; sigscan? */;\n\t\t// Setting the function's pointer to a null pointer is UB in Rust.\n\t\t// https://doc.rust-lang.org/nomicon/ffi.html#the-nullable-pointer-optimization\n\n        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.\n\n\t\t/* do something with our player! */\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilliamvenner%2Fnull_fn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwilliamvenner%2Fnull_fn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilliamvenner%2Fnull_fn/lists"}