Auto merge of #130 - faern:runloop-source-and-observer, r=jdm

Add runloop source and observer functionality

I had the need to add a source (from `SCDynamicStoreCreateRunLoopSource`). And since all the add/contains/remove operations were fairly trivial I did not see any reason to not complete them for timers, sources and observers at the same time.

Also adding the `CFRunLoopMode` type alias to make the signatures reflect the original API a bit more.

Lastly, removing underscore from `_info` since it's actually used.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/130)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo
2017-11-23 08:07:22 -06:00
committed by GitHub
+74 -4
View File
@@ -18,6 +18,8 @@ use base::{TCFType};
use date::{CFAbsoluteTime, CFTimeInterval};
use string::{CFString};
pub type CFRunLoopMode = CFStringRef;
pub struct CFRunLoop(CFRunLoopRef);
impl Drop for CFRunLoop {
@@ -70,18 +72,60 @@ impl CFRunLoop {
}
}
pub fn contains_timer(&self, timer: &CFRunLoopTimer, mode: CFStringRef) -> bool {
pub fn contains_timer(&self, timer: &CFRunLoopTimer, mode: CFRunLoopMode) -> bool {
unsafe {
CFRunLoopContainsTimer(self.0, timer.0, mode) != 0
}
}
pub fn add_timer(&self, timer: &CFRunLoopTimer, mode: CFStringRef) {
pub fn add_timer(&self, timer: &CFRunLoopTimer, mode: CFRunLoopMode) {
unsafe {
CFRunLoopAddTimer(self.0, timer.0, mode);
}
}
pub fn remove_timer(&self, timer: &CFRunLoopTimer, mode: CFRunLoopMode) {
unsafe {
CFRunLoopRemoveTimer(self.0, timer.0, mode);
}
}
pub fn contains_source(&self, source: &CFRunLoopSource, mode: CFRunLoopMode) -> bool {
unsafe {
CFRunLoopContainsSource(self.0, source.0, mode) != 0
}
}
pub fn add_source(&self, source: &CFRunLoopSource, mode: CFRunLoopMode) {
unsafe {
CFRunLoopAddSource(self.0, source.0, mode);
}
}
pub fn remove_source(&self, source: &CFRunLoopSource, mode: CFRunLoopMode) {
unsafe {
CFRunLoopRemoveSource(self.0, source.0, mode);
}
}
pub fn contains_observer(&self, observer: &CFRunLoopObserver, mode: CFRunLoopMode) -> bool {
unsafe {
CFRunLoopContainsObserver(self.0, observer.0, mode) != 0
}
}
pub fn add_observer(&self, observer: &CFRunLoopObserver, mode: CFRunLoopMode) {
unsafe {
CFRunLoopAddObserver(self.0, observer.0, mode);
}
}
pub fn remove_observer(&self, observer: &CFRunLoopObserver, mode: CFRunLoopMode) {
unsafe {
CFRunLoopRemoveObserver(self.0, observer.0, mode);
}
}
}
pub struct CFRunLoopTimer(CFRunLoopTimerRef);
@@ -105,6 +149,32 @@ impl CFRunLoopTimer {
}
}
pub struct CFRunLoopSource(CFRunLoopSourceRef);
impl Drop for CFRunLoopSource {
fn drop(&mut self) {
unsafe {
CFRelease(self.as_CFTypeRef())
}
}
}
impl_TCFType!(CFRunLoopSource, CFRunLoopSourceRef, CFRunLoopSourceGetTypeID);
pub struct CFRunLoopObserver(CFRunLoopObserverRef);
impl Drop for CFRunLoopObserver {
fn drop(&mut self) {
unsafe {
CFRelease(self.as_CFTypeRef())
}
}
}
impl_TCFType!(CFRunLoopObserver, CFRunLoopObserverRef, CFRunLoopObserverGetTypeID);
#[cfg(test)]
mod test {
use super::*;
@@ -132,8 +202,8 @@ mod test {
CFRunLoop::run_current();
}
extern "C" fn timer_popped(_timer: CFRunLoopTimerRef, _info: *mut c_void) {
let previous_now_ptr: *const CFAbsoluteTime = unsafe { mem::transmute(_info) };
extern "C" fn timer_popped(_timer: CFRunLoopTimerRef, info: *mut c_void) {
let previous_now_ptr: *const CFAbsoluteTime = unsafe { mem::transmute(info) };
let previous_now = unsafe { *previous_now_ptr };
let now = CFDate::now().abs_time();
assert!(now - previous_now > 0.19 && now - previous_now < 0.21);