Skip to content

Instantly share code, notes, and snippets.

Created March 23, 2016 10:10
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/529349a3a8315c68869a to your computer and use it in GitHub Desktop.
Save anonymous/529349a3a8315c68869a to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![feature(unboxed_closures, fn_traits)]
fn get_fn_ptr<A, C: Fn<A>>(_: &C) -> extern "rust-call" fn(&(), A) -> C::Output {
let fn_ptr: extern "rust-call" fn(&(), A) -> C::Output = unsafe {
std::mem::transmute(<C as std::ops::Fn<A>>::call as usize)
};
fn_ptr
}
fn main() {
let closure = |x| { println!("hi {}", x) };
let ptr = get_fn_ptr(&closure);
ptr(&(), ("bstrie",));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment