Skip to content

Various fixes and rearrangements #145

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 5 commits into from
Jan 24, 2018
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
1 change: 0 additions & 1 deletion core-foundation/src/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

//! A Boolean type.

use core_foundation_sys::base::{CFRelease};
pub use core_foundation_sys::number::{CFBooleanRef, CFBooleanGetTypeID, kCFBooleanTrue, kCFBooleanFalse};

use base::TCFType;
Expand Down
2 changes: 1 addition & 1 deletion core-foundation/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! Core Foundation Bundle Type

pub use core_foundation_sys::bundle::*;
use core_foundation_sys::base::{CFRelease, kCFAllocatorDefault};
use core_foundation_sys::base::kCFAllocatorDefault;

use base::TCFType;
use url::CFURL;
Expand Down
2 changes: 1 addition & 1 deletion core-foundation/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! Core Foundation byte buffers.

pub use core_foundation_sys::data::*;
use core_foundation_sys::base::{CFIndex, CFRelease};
use core_foundation_sys::base::CFIndex;
use core_foundation_sys::base::{kCFAllocatorDefault};
use std::ops::Deref;
use std::slice;
Expand Down
2 changes: 1 addition & 1 deletion core-foundation/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! Core Foundation date objects.

pub use core_foundation_sys::date::*;
use core_foundation_sys::base::{CFRelease, kCFAllocatorDefault};
use core_foundation_sys::base::kCFAllocatorDefault;

use base::TCFType;

Expand Down
62 changes: 61 additions & 1 deletion core-foundation/src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! Dictionaries of key-value pairs.

pub use core_foundation_sys::dictionary::*;
use core_foundation_sys::base::CFRelease;

use core_foundation_sys::base::{CFTypeRef, kCFAllocatorDefault};
use libc::c_void;
use std::mem;
Expand Down Expand Up @@ -316,3 +316,63 @@ impl CFMutableDictionary {
unsafe { CFDictionaryRemoveAllValues(self.0) }
}
}


#[cfg(test)]
pub mod test {
use super::*;
use base::TCFType;
use boolean::CFBoolean;
use number::CFNumber;
use string::CFString;

#[test]
fn dictionary() {
let bar = CFString::from_static_string("Bar");
let baz = CFString::from_static_string("Baz");
let boo = CFString::from_static_string("Boo");
let foo = CFString::from_static_string("Foo");
let tru = CFBoolean::true_value();
let n42 = CFNumber::from(42);

let d = CFDictionary::from_CFType_pairs(&[
(bar.as_CFType(), boo.as_CFType()),
(baz.as_CFType(), tru.as_CFType()),
(foo.as_CFType(), n42.as_CFType()),
]);

let (v1, v2) = d.get_keys_and_values();
assert!(v1 == &[bar.as_CFTypeRef(), baz.as_CFTypeRef(), foo.as_CFTypeRef()]);
assert!(v2 == &[boo.as_CFTypeRef(), tru.as_CFTypeRef(), n42.as_CFTypeRef()]);
}

#[test]
fn mutable_dictionary() {
let bar = CFString::from_static_string("Bar");
let baz = CFString::from_static_string("Baz");
let boo = CFString::from_static_string("Boo");
let foo = CFString::from_static_string("Foo");
let tru = CFBoolean::true_value();
let n42 = CFNumber::from(42);

let d = CFMutableDictionary::new();
d.add2(&bar, &boo);
d.add2(&baz, &tru);
d.add2(&foo, &n42);
assert_eq!(d.len(), 3);

let (v1, v2) = d.get_keys_and_values();
assert!(v1 == &[bar.as_CFTypeRef(), baz.as_CFTypeRef(), foo.as_CFTypeRef()]);
assert!(v2 == &[boo.as_CFTypeRef(), tru.as_CFTypeRef(), n42.as_CFTypeRef()]);

d.remove2(&baz);
assert_eq!(d.len(), 2);

let (v1, v2) = d.get_keys_and_values();
assert!(v1 == &[bar.as_CFTypeRef(), foo.as_CFTypeRef()]);
assert!(v2 == &[boo.as_CFTypeRef(), n42.as_CFTypeRef()]);

d.remove_all();
assert_eq!(d.len(), 0)
}
}
4 changes: 2 additions & 2 deletions core-foundation/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

//! Core Foundation errors.

use core_foundation_sys::error::*;
use core_foundation_sys::base::CFRelease;
pub use core_foundation_sys::error::*;

use std::error::Error;
use std::fmt;

Expand Down
49 changes: 22 additions & 27 deletions core-foundation/src/filedescriptor.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
pub use core_foundation_sys::filedescriptor::*;

use core_foundation_sys::base::{Boolean, CFIndex, CFRelease};
use core_foundation_sys::base::{Boolean, CFIndex};
use core_foundation_sys::base::{kCFAllocatorDefault, CFOptionFlags};

use base::{TCFType};
use base::TCFType;
use runloop::CFRunLoopSource;

use std::mem;
use std::os::unix::io::{AsRawFd, RawFd};
use std::ptr;

pub struct CFFileDescriptor(CFFileDescriptorRef);

impl Drop for CFFileDescriptor {
fn drop(&mut self) {
unsafe {
CFRelease(self.as_CFTypeRef())
}
}
declare_TCFType!{
CFFileDescriptor, CFFileDescriptorRef
}

impl_TCFType!(CFFileDescriptor, CFFileDescriptorRef, CFFileDescriptorGetTypeID);

impl CFFileDescriptor {
Expand Down Expand Up @@ -72,22 +66,14 @@ impl CFFileDescriptor {
CFFileDescriptorInvalidate(self.0)
}
}
}

impl AsRawFd for CFFileDescriptor {
fn as_raw_fd(&self) -> RawFd {
unsafe {
CFFileDescriptorGetNativeDescriptor(self.0)
}
}
}

use runloop::{CFRunLoopSource};

impl CFRunLoopSource {
pub fn from_file_descriptor(fd: &CFFileDescriptor, order: CFIndex) -> Option<CFRunLoopSource> {
pub fn to_run_loop_source(&self, order: CFIndex) -> Option<CFRunLoopSource> {
unsafe {
let source_ref = CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, fd.0, order);
let source_ref = CFFileDescriptorCreateRunLoopSource(
kCFAllocatorDefault,
self.0,
order
);
if source_ref.is_null() {
None
} else {
Expand All @@ -97,6 +83,15 @@ impl CFRunLoopSource {
}
}

impl AsRawFd for CFFileDescriptor {
fn as_raw_fd(&self) -> RawFd {
unsafe {
CFFileDescriptorGetNativeDescriptor(self.0)
}
}
}


#[cfg(test)]
mod test {
extern crate libc;
Expand Down Expand Up @@ -169,11 +164,11 @@ mod test {

assert!(cf_fd.valid());

let runloop = CFRunLoop::get_current();
let run_loop = CFRunLoop::get_current();
let source = CFRunLoopSource::from_file_descriptor(&cf_fd, 0);
assert!(source.is_some());
unsafe {
runloop.add_source(&source.unwrap(), kCFRunLoopDefaultMode);
run_loop.add_source(&source.unwrap(), kCFRunLoopDefaultMode);
}

info.value = 0;
Expand Down
82 changes: 7 additions & 75 deletions core-foundation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ macro_rules! declare_TCFType {

impl Drop for $ty {
fn drop(&mut self) {
unsafe { CFRelease(self.as_CFTypeRef()) }
unsafe { $crate::base::CFRelease(self.as_CFTypeRef()) }
}
}
}
Expand All @@ -42,12 +42,13 @@ macro_rules! impl_TCFType {

#[inline]
unsafe fn wrap_under_get_rule(reference: $raw) -> $ty {
let reference = ::std::mem::transmute(::core_foundation_sys::base::CFRetain(::std::mem::transmute(reference)));
use std::mem;
let reference = mem::transmute($crate::base::CFRetain(mem::transmute(reference)));
$crate::base::TCFType::wrap_under_create_rule(reference)
}

#[inline]
fn as_CFTypeRef(&self) -> ::core_foundation_sys::base::CFTypeRef {
fn as_CFTypeRef(&self) -> $crate::base::CFTypeRef {
unsafe {
::std::mem::transmute(self.as_concrete_TypeRef())
}
Expand All @@ -59,7 +60,7 @@ macro_rules! impl_TCFType {
}

#[inline]
fn type_id() -> ::core_foundation_sys::base::CFTypeID {
fn type_id() -> $crate::base::CFTypeID {
unsafe {
$ty_id()
}
Expand Down Expand Up @@ -99,7 +100,8 @@ macro_rules! impl_TCFTypeGeneric {

#[inline]
unsafe fn wrap_under_get_rule(reference: $raw) -> $ty<T> {
let reference = ::std::mem::transmute(::core_foundation_sys::base::CFRetain(::std::mem::transmute(reference)));
use std::mem;
let reference = mem::transmute($crate::base::CFRetain(mem::transmute(reference)));
$crate::base::TCFType::wrap_under_create_rule(reference)
}

Expand Down Expand Up @@ -204,73 +206,3 @@ pub mod propertylist;
pub mod runloop;
pub mod timezone;
pub mod uuid;

#[cfg(test)]
pub mod test {
#[test]
fn test_stuff() {
use base::TCFType;
use boolean::CFBoolean;
use number::CFNumber;
use dictionary::CFDictionary;
use string::CFString;

/*let n = CFNumber::new_number(42 as i32);
io::println(format!("%d", (&n).retain_count() as int));
(&n).show();*/

let bar = CFString::from_static_string("Bar");
let baz = CFString::from_static_string("Baz");
let boo = CFString::from_static_string("Boo");
let foo = CFString::from_static_string("Foo");
let tru = CFBoolean::true_value();
let n42 = CFNumber::from(42);

let d = CFDictionary::from_CFType_pairs(&[
(bar.as_CFType(), boo.as_CFType()),
(baz.as_CFType(), tru.as_CFType()),
(foo.as_CFType(), n42.as_CFType()),
]);

let (v1, v2) = d.get_keys_and_values();

assert!(v1 == &[bar.as_CFTypeRef(), baz.as_CFTypeRef(), foo.as_CFTypeRef()]);
assert!(v2 == &[boo.as_CFTypeRef(), tru.as_CFTypeRef(), n42.as_CFTypeRef()]);
}

#[test]
fn test_mutable_dictionary() {
use base::TCFType;
use boolean::CFBoolean;
use dictionary::CFMutableDictionary;
use number::CFNumber;
use string::CFString;

let bar = CFString::from_static_string("Bar");
let baz = CFString::from_static_string("Baz");
let boo = CFString::from_static_string("Boo");
let foo = CFString::from_static_string("Foo");
let tru = CFBoolean::true_value();
let n42 = CFNumber::from(42);

let d = CFMutableDictionary::new();
d.add2(&bar, &boo);
d.add2(&baz, &tru);
d.add2(&foo, &n42);
assert_eq!(d.len(), 3);

let (v1, v2) = d.get_keys_and_values();
assert!(v1 == &[bar.as_CFTypeRef(), baz.as_CFTypeRef(), foo.as_CFTypeRef()]);
assert!(v2 == &[boo.as_CFTypeRef(), tru.as_CFTypeRef(), n42.as_CFTypeRef()]);

d.remove2(&baz);
assert_eq!(d.len(), 2);

let (v1, v2) = d.get_keys_and_values();
assert!(v1 == &[bar.as_CFTypeRef(), foo.as_CFTypeRef()]);
assert!(v2 == &[boo.as_CFTypeRef(), n42.as_CFTypeRef()]);

d.remove_all();
assert_eq!(d.len(), 0)
}
}
2 changes: 1 addition & 1 deletion core-foundation/src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

//! Immutable numbers.

use core_foundation_sys::base::{CFRelease, kCFAllocatorDefault};
use core_foundation_sys::base::kCFAllocatorDefault;
pub use core_foundation_sys::number::*;
use std::mem;

Expand Down
2 changes: 1 addition & 1 deletion core-foundation/src/propertylist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use base::{CFType, TCFType};

pub use core_foundation_sys::propertylist::*;
use core_foundation_sys::error::CFErrorRef;
use core_foundation_sys::base::{CFGetRetainCount, CFGetTypeID, CFIndex, CFRelease, CFRetain,
use core_foundation_sys::base::{CFGetRetainCount, CFGetTypeID, CFIndex, CFRetain,
CFShow, CFTypeID, kCFAllocatorDefault};

pub fn create_with_data(data: CFData,
Expand Down
9 changes: 8 additions & 1 deletion core-foundation/src/runloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
#![allow(non_upper_case_globals)]

pub use core_foundation_sys::runloop::*;
use core_foundation_sys::base::{CFIndex, CFRelease};
use core_foundation_sys::base::CFIndex;
use core_foundation_sys::base::{kCFAllocatorDefault, CFOptionFlags};
use core_foundation_sys::string::CFStringRef;

use base::{TCFType};
use date::{CFAbsoluteTime, CFTimeInterval};
use filedescriptor::CFFileDescriptor;
use string::{CFString};

pub type CFRunLoopMode = CFStringRef;
Expand Down Expand Up @@ -137,6 +138,12 @@ impl CFRunLoopTimer {
declare_TCFType!(CFRunLoopSource, CFRunLoopSourceRef);
impl_TCFType!(CFRunLoopSource, CFRunLoopSourceRef, CFRunLoopSourceGetTypeID);

impl CFRunLoopSource {
pub fn from_file_descriptor(fd: &CFFileDescriptor, order: CFIndex) -> Option<CFRunLoopSource> {
fd.to_run_loop_source(order)
}
}

declare_TCFType!(CFRunLoopObserver, CFRunLoopObserverRef);
impl_TCFType!(CFRunLoopObserver, CFRunLoopObserverRef, CFRunLoopObserverGetTypeID);

Expand Down
1 change: 0 additions & 1 deletion core-foundation/src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//! An immutable bag of elements.

pub use core_foundation_sys::set::*;
use core_foundation_sys::base::CFRelease;
use core_foundation_sys::base::{CFTypeRef, kCFAllocatorDefault};

use base::{CFIndexConvertible, TCFType};
Expand Down
2 changes: 1 addition & 1 deletion core-foundation/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use core_foundation_sys::string::*;

use base::{CFIndexConvertible, TCFType};

use core_foundation_sys::base::{Boolean, CFIndex, CFRange, CFRelease};
use core_foundation_sys::base::{Boolean, CFIndex, CFRange};
use core_foundation_sys::base::{kCFAllocatorDefault, kCFAllocatorNull};
use std::fmt;
use std::str::{self, FromStr};
Expand Down
2 changes: 1 addition & 1 deletion core-foundation/src/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! Core Foundation time zone objects.

pub use core_foundation_sys::timezone::*;
use core_foundation_sys::base::{CFRelease, kCFAllocatorDefault};
use core_foundation_sys::base::kCFAllocatorDefault;

use base::TCFType;
use date::{CFDate, CFTimeInterval};
Expand Down
Loading