From d83794c3abb4be952edfb6da2cd0025ec4e9bdcd Mon Sep 17 00:00:00 2001 From: Marco Mastropaolo Date: Wed, 11 Aug 2021 14:17:40 +0200 Subject: [PATCH] Adds run_in_mode wrapper over CFRunLoopRunInMode --- core-foundation/src/runloop.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/core-foundation/src/runloop.rs b/core-foundation/src/runloop.rs index 8f22c09..be06f4e 100644 --- a/core-foundation/src/runloop.rs +++ b/core-foundation/src/runloop.rs @@ -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 { @@ -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);