No way to detect at compile-time whether we are in the main thread. This one is a bit weird, but on OS/X some GUI operations can only be done in the main thread. It would be nicer if this was detectable at compile-time.
This can be implemented using a data type that is only constructed at the start of the main thread and is required for any GUI operations that must occur in the main thread. Something like this:
#[derive(Copy, Clone)]
struct MainThread {
_prevent_construction: ()
}
impl !Send for MainThread {}
impl !Sync for MainThread {}
fn main() {
let token = MainThread{ _prevent_construction: () }
do_stuff_that_must_be_in_main_thread(token);
}
This way, if a function must be run in the main thread (and any function that calls such a function is) it can just accept a token. The token cannot exist outside the main thread, because it is only constructed in the main thread and cannot be sent to another.