Skip to content

Instantly share code, notes, and snippets.

Created March 23, 2016 09:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a1bc5830927b9f1b98ca to your computer and use it in GitHub Desktop.
Save anonymous/a1bc5830927b9f1b98ca to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![feature(unboxed_closures, fn_traits)]
fn get_fn_ptr<C: Fn() -> ()>(_: &C) -> extern "rust-call" fn(&(), ()) -> () {
let fn_ptr: extern "rust-call" fn(&(), ()) -> () = unsafe { std::mem::transmute(<C as std::ops::Fn<()>>::call as usize) };
fn_ptr
}
fn main() {
let closure = ||{ println!("hi") };
let ptr = get_fn_ptr(&closure);
ptr(&(), ());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment