Splits a closure into its data part and its code part, allowing it to be used as a callback by FFI code.

use std :: ffi :: c_void ; let mut total = 0 ; let mut some_closure = | n : usize | { total + = n ; total }; type Callback = unsafe extern "C" fn ( * mut c_void , usize ) - > usize ; unsafe fn some_c_function ( max_value : usize , cb : Callback , user_data : * mut c_void ) { for i in 0 .. max_value { let got = cb ( user_data , i ); println ! ( "iteration: {}, total: {}" , i , got ); } } unsafe { let ( state , callback ) = ffi_helpers :: split_closure ( & mut some_closure ); some_c_function ( 42 , callback , state ); } assert_eq ! ( total , ( 0 .. 42 ). sum ());