Skip to content

Code cleanup and warning fixes #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl TCFType<CFArrayRef> for CFArray {
}

#[inline]
fn type_id(_: Option<CFArray>) -> CFTypeID {
fn type_id() -> CFTypeID {
unsafe {
CFArrayGetTypeID()
}
Expand Down
11 changes: 4 additions & 7 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub type Boolean = u8;

pub type CFIndex = c_long;

#[allow(non_camel_case_types)]
pub type mach_port_t = c_uint;

pub trait CFIndexConvertible {
Expand Down Expand Up @@ -105,10 +106,7 @@ pub trait TCFType<ConcreteTypeRef> {
unsafe fn wrap_under_create_rule(obj: ConcreteTypeRef) -> Self;

/// Returns the type ID for this class.
///
/// FIXME(pcwalton): The dummy parameter is there to work around the current inexpressivity of
/// the Rust language.
fn type_id(dummy: Option<Self>) -> CFTypeID;
fn type_id() -> CFTypeID;

/// Returns the object as a wrapped `CFType`. The reference count is incremented by one.
#[inline]
Expand Down Expand Up @@ -152,8 +150,7 @@ pub trait TCFType<ConcreteTypeRef> {
/// Returns true if this value is an instance of another type.
#[inline]
fn instance_of<OtherConcreteTypeRef,OtherCFType:TCFType<OtherConcreteTypeRef>>(&self) -> bool {
let dummy: Option<OtherCFType> = None;
self.type_of() == TCFType::type_id(dummy)
self.type_of() == <OtherCFType as TCFType<_>>::type_id()
}
}

Expand Down Expand Up @@ -182,7 +179,7 @@ impl TCFType<CFTypeRef> for CFType {
}

#[inline]
fn type_id(_: Option<CFType>) -> CFTypeID {
fn type_id() -> CFTypeID {
// FIXME(pcwalton): Is this right?
0
}
Expand Down
2 changes: 1 addition & 1 deletion src/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl TCFType<CFBooleanRef> for CFBoolean {
}

#[inline]
fn type_id(_: Option<CFBoolean>) -> CFTypeID {
fn type_id() -> CFTypeID {
unsafe {
CFBooleanGetTypeID()
}
Expand Down
2 changes: 1 addition & 1 deletion src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl TCFType<CFBundleRef> for CFBundle {
}

#[inline]
fn type_id(_: Option<CFBundle>) -> CFTypeID {
fn type_id() -> CFTypeID {
unsafe {
CFBundleGetTypeID()
}
Expand Down
2 changes: 1 addition & 1 deletion src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl TCFType<CFDataRef> for CFData {
}

#[inline]
fn type_id(_: Option<CFData>) -> CFTypeID {
fn type_id() -> CFTypeID {
unsafe {
CFDataGetTypeID()
}
Expand Down
2 changes: 1 addition & 1 deletion src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl TCFType<CFDictionaryRef> for CFDictionary {
}

#[inline]
fn type_id(_: Option<CFDictionary>) -> CFTypeID {
fn type_id() -> CFTypeID {
unsafe {
CFDictionaryGetTypeID()
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#![allow(non_snake_case)]

#![feature(core, libc)]
#![feature(core)]

extern crate libc;

Expand Down
2 changes: 1 addition & 1 deletion src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl TCFType<CFNumberRef> for CFNumber {
}

#[inline]
fn type_id(_: Option<CFNumber>) -> CFTypeID {
fn type_id() -> CFTypeID {
unsafe {
CFNumberGetTypeID()
}
Expand Down
42 changes: 24 additions & 18 deletions src/runloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ use base::{kCFAllocatorDefault};
use base::{Boolean};
use array::{CFArrayRef};
use string::{CFString, CFStringRef};
use date::{CFAbsoluteTime, CFTimeInterval, CFAbsoluteTimeGetCurrent};
use date::{CFAbsoluteTime, CFTimeInterval};
use libc::c_void;
use std::mem;
use std::ptr;

/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug.
pub struct CFRunLoop {
Expand Down Expand Up @@ -60,7 +59,7 @@ impl TCFType<CFRunLoopRef> for CFRunLoop {
}

#[inline]
fn type_id(_: Option<CFRunLoop>) -> CFTypeID {
fn type_id() -> CFTypeID {
unsafe {
CFRunLoopGetTypeID()
}
Expand Down Expand Up @@ -117,7 +116,7 @@ impl CFRunLoop {
CFRunLoopAddTimer(self.obj, timer.obj, mode);
}
}

}

#[repr(C)]
Expand Down Expand Up @@ -154,7 +153,7 @@ pub const kCFRunLoopAllActivities: CFOptionFlags = 0x0FFFFFFF;

#[repr(C)]
pub struct CFRunLoopSourceContext {
version: CFIndex,
version: CFIndex,
info: *mut c_void,
retain: extern "C" fn (info: *const c_void) -> *const c_void,
release: extern "C" fn (info: *const c_void),
Expand All @@ -168,7 +167,7 @@ pub struct CFRunLoopSourceContext {

#[repr(C)]
pub struct CFRunLoopSourceContext1 {
version: CFIndex,
version: CFIndex,
info: *mut c_void,
retain: extern "C" fn (info: *const c_void) -> *const c_void,
release: extern "C" fn (info: *const c_void),
Expand All @@ -182,7 +181,7 @@ pub struct CFRunLoopSourceContext1 {

#[repr(C)]
pub struct CFRunLoopObserverContext {
version: CFIndex,
version: CFIndex,
info: *mut c_void,
retain: extern "C" fn (info: *const c_void) -> *const c_void,
release: extern "C" fn (info: *const c_void),
Expand All @@ -193,7 +192,7 @@ pub type CFRunLoopObserverCallBack = extern "C" fn (observer: CFRunLoopObserverR

#[repr(C)]
pub struct CFRunLoopTimerContext {
version: CFIndex,
version: CFIndex,
info: *mut c_void,
retain: extern "C" fn (info: *const c_void) -> *const c_void,
release: extern "C" fn (info: *const c_void),
Expand Down Expand Up @@ -247,7 +246,7 @@ impl TCFType<CFRunLoopTimerRef> for CFRunLoopTimer {
}

#[inline]
fn type_id(_: Option<CFRunLoopTimer>) -> CFTypeID {
fn type_id() -> CFTypeID {
unsafe {
CFRunLoopTimerGetTypeID()
}
Expand Down Expand Up @@ -328,9 +327,15 @@ extern {
fn CFRunLoopTimerSetTolerance(timer: CFRunLoopTimerRef, tolerance: CFTimeInterval);
}

#[cfg(test)]
mod test {
use super::*;
use date::{CFAbsoluteTime, CFAbsoluteTimeGetCurrent};
use std::mem;
use libc::c_void;

#[test]
fn wait_200_milliseconds() {
#[test]
fn wait_200_milliseconds() {
let run_loop = CFRunLoop::get_current();
let mut now = unsafe { CFAbsoluteTimeGetCurrent() };
let mut context = unsafe { CFRunLoopTimerContext {
Expand All @@ -346,12 +351,13 @@ fn wait_200_milliseconds() {
run_loop.add_timer(&run_loop_timer, kCFRunLoopDefaultMode);

CFRunLoop::run_current();
}
}

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 = unsafe { CFAbsoluteTimeGetCurrent() };
assert!(now - previous_now > 0.19 && now - previous_now < 0.21);
CFRunLoop::get_current().stop();
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 = unsafe { CFAbsoluteTimeGetCurrent() };
assert!(now - previous_now > 0.19 && now - previous_now < 0.21);
CFRunLoop::get_current().stop();
}
}
2 changes: 1 addition & 1 deletion src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl TCFType<CFSetRef> for CFSet {
}

#[inline]
fn type_id(_: Option<CFSet>) -> CFTypeID {
fn type_id() -> CFTypeID {
unsafe {
CFSetGetTypeID()
}
Expand Down
2 changes: 1 addition & 1 deletion src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl TCFType<CFStringRef> for CFString {
}

#[inline]
fn type_id(_: Option<CFString>) -> CFTypeID {
fn type_id() -> CFTypeID {
unsafe {
CFStringGetTypeID()
}
Expand Down
2 changes: 1 addition & 1 deletion src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl TCFType<CFURLRef> for CFURL {
}

#[inline]
fn type_id(_: Option<CFURL>) -> CFTypeID {
fn type_id() -> CFTypeID {
unsafe {
CFURLGetTypeID()
}
Expand Down