Skip to content

Instantly share code, notes, and snippets.

@brendanzab
Created October 20, 2012 00:03
Show Gist options
  • Select an option

  • Save brendanzab/3921366 to your computer and use it in GitHub Desktop.

Select an option

Save brendanzab/3921366 to your computer and use it in GitHub Desktop.
/**
* Because I have a large number of foriegn callback functions to wrap it'd be
* great I you could use a generic handle for local data setting and getting.
* Unfortunately when I try this I get:
*
* generic_tls.rs:23:64: 12:65 error: unexpected token: `,`
* generic_tls.rs:23 unsafe { task::local_data::local_data_set(tls_handle<FooFun>, cbfun); }
*
*/
// #[nolink]
// extern mod bindings {
// fn setFooCallback(cbfun: *u8);
// }
// A generic function used to make space in TLS
fn tls_handle<T>(_v: @T) {}
type FooFun = @fn(x: int);
pub fn set_foo_callback(cbfun: @FooFun) {
unsafe { task::local_data::local_data_set(tls_handle<FooFun>, cbfun); }
// bindings::setFooCallback(foo_callback)
}
extern fn foo_callback(x: libc::c_int) -> c_int {
unsafe {
match task::local_data::local_data_get(tls_handle<FooFun>) {
Some(f) => { (*f)(x as int) }
None => {}
}
}
}
fn main() {
set_foo_callback(|x| io::println(fmt!("x = %d", x)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment