Allow to return something from the examples/tutorials main() wrapper

This commit is contained in:
Sebastian Dröge 2017-11-12 20:11:25 +01:00
parent a01f1385ec
commit 4ab5893359
2 changed files with 108 additions and 20 deletions

View file

@ -2,22 +2,66 @@
/// on the main thread in order to open windows and use OpenGL. /// on the main thread in order to open windows and use OpenGL.
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
#[link(name = "foundation", kind = "framework")] mod runloop {
extern "C" { use std::os::raw::c_void;
fn CFRunLoopRun(); #[repr(C)]
pub struct CFRunLoop(*mut c_void);
#[link(name = "foundation", kind = "framework")]
extern "C" {
fn CFRunLoopRun();
fn CFRunLoopGetMain() -> *mut c_void;
fn CFRunLoopStop(l: *mut c_void);
}
impl CFRunLoop {
pub fn run() {
unsafe {
CFRunLoopRun();
}
}
pub fn get_main() -> CFRunLoop {
unsafe {
let r = CFRunLoopGetMain();
assert!(!r.is_null());
CFRunLoop(r)
}
}
pub fn stop(&self) {
unsafe { CFRunLoopStop(self.0) }
}
}
unsafe impl Send for CFRunLoop {}
} }
/// On macOS this launches the callback function on a thread. /// On macOS this launches the callback function on a thread.
/// On other platforms it's just executed immediately. /// On other platforms it's just executed immediately.
#[cfg(not(target_os = "macos"))] #[cfg(not(target_os = "macos"))]
pub fn run<F: FnOnce() + Send + 'static>(main: F) { pub fn run<T, F: FnOnce() -> T + Send + 'static>(main: F) -> T
main(); where
T: Send + 'static,
{
main()
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub fn run<F: FnOnce() + Send + 'static>(main: F) { pub fn run<T, F: FnOnce() -> T + Send + 'static>(main: F) -> T
::std::thread::spawn(main); where
unsafe { T: Send + 'static,
CFRunLoopRun(); {
} use std::thread;
let l = runloop::CFRunLoop::get_main();
let t = thread::spawn(move || {
let res = main();
l.stop();
res
});
runloop::CFRunLoop::run();
t.join().unwrap()
} }

View file

@ -2,22 +2,66 @@
/// on the main thread in order to open windows and use OpenGL. /// on the main thread in order to open windows and use OpenGL.
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
#[link(name = "foundation", kind = "framework")] mod runloop {
extern "C" { use std::os::raw::c_void;
fn CFRunLoopRun(); #[repr(C)]
pub struct CFRunLoop(*mut c_void);
#[link(name = "foundation", kind = "framework")]
extern "C" {
fn CFRunLoopRun();
fn CFRunLoopGetMain() -> *mut c_void;
fn CFRunLoopStop(l: *mut c_void);
}
impl CFRunLoop {
pub fn run() {
unsafe {
CFRunLoopRun();
}
}
pub fn get_main() -> CFRunLoop {
unsafe {
let r = CFRunLoopGetMain();
assert!(!r.is_null());
CFRunLoop(r)
}
}
pub fn stop(&self) {
unsafe { CFRunLoopStop(self.0) }
}
}
unsafe impl Send for CFRunLoop {}
} }
/// On macOS this launches the callback function on a thread. /// On macOS this launches the callback function on a thread.
/// On other platforms it's just executed immediately. /// On other platforms it's just executed immediately.
#[cfg(not(target_os = "macos"))] #[cfg(not(target_os = "macos"))]
pub fn run<F: FnOnce() + Send + 'static>(main: F) { pub fn run<T, F: FnOnce() -> T + Send + 'static>(main: F) -> T
main(); where
T: Send + 'static,
{
main()
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub fn run<F: FnOnce() + Send + 'static>(main: F) { pub fn run<T, F: FnOnce() -> T + Send + 'static>(main: F) -> T
::std::thread::spawn(main); where
unsafe { T: Send + 'static,
CFRunLoopRun(); {
} use std::thread;
let l = runloop::CFRunLoop::get_main();
let t = thread::spawn(move || {
let res = main();
l.stop();
res
});
runloop::CFRunLoop::run();
t.join().unwrap()
} }