Skip to content

Commit

Permalink
Auto merge of #460 - xanathar:wip/mmp/runloop_ext, r=jrmuizel
Browse files Browse the repository at this point in the history
Adds run_in_mode wrapper over CFRunLoopRunInMode

Adds a `run_in_mode` wrapper function which just makes `CFRunLoopRunInMode` a little more "rusty".

Also adds a `CFRunLoopRunResult` enum to type the return value.
  • Loading branch information
bors-servo committed Oct 27, 2021
2 parents 3841d2b + d83794c commit 4960762
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions core-foundation/src/runloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ declare_TCFType!(CFRunLoop, CFRunLoopRef);
impl_TCFType!(CFRunLoop, CFRunLoopRef, CFRunLoopGetTypeID);
impl_CFTypeDescription!(CFRunLoop);

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum CFRunLoopRunResult {
Finished = 1,
Stopped = 2,
TimedOut = 3,
HandledSource = 4,
}

impl CFRunLoop {
pub fn get_current() -> CFRunLoop {
unsafe {
Expand All @@ -47,6 +55,24 @@ impl CFRunLoop {
}
}

pub fn run_in_mode(
mode: CFStringRef,
duration: std::time::Duration,
return_after_source_handled: bool,
) -> CFRunLoopRunResult {
let seconds = duration.as_secs_f64();
let return_after_source_handled = if return_after_source_handled { 1 } else { 0 };

unsafe {
match CFRunLoopRunInMode(mode, seconds, return_after_source_handled) {
2 => CFRunLoopRunResult::Stopped,
3 => CFRunLoopRunResult::TimedOut,
4 => CFRunLoopRunResult::HandledSource,
_ => CFRunLoopRunResult::Finished,
}
}
}

pub fn stop(&self) {
unsafe {
CFRunLoopStop(self.0);
Expand Down

0 comments on commit 4960762

Please sign in to comment.