Skip to content
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

Modernization and cleanup #69

Merged
merged 8 commits into from Sep 5, 2015
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -9,8 +9,8 @@

//! Heterogeneous immutable arrays.

use base::{CFAllocatorRef, CFIndex, CFIndexConvertible, CFRelease, CFRetain};
use base::{CFType, CFTypeID, CFTypeRef, TCFType};
use base::{CFAllocatorRef, CFIndex, CFIndexConvertible, CFRelease};
use base::{CFTypeID, CFTypeRef, TCFType};
use base::{kCFAllocatorDefault};
use libc::c_void;
use std::mem;
@@ -44,11 +44,7 @@ struct __CFArray;
pub type CFArrayRef = *const __CFArray;

/// A heterogeneous immutable array.
///
/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug.
pub struct CFArray {
obj: CFArrayRef,
}
pub struct CFArray(CFArrayRef);

impl Drop for CFArray {
fn drop(&mut self) {
@@ -77,43 +73,11 @@ impl<'a> Iterator for CFArrayIterator<'a> {
}
}

impl TCFType<CFArrayRef> for CFArray {
#[inline]
fn as_concrete_TypeRef(&self) -> CFArrayRef {
self.obj
}

#[inline]
unsafe fn wrap_under_get_rule(reference: CFArrayRef) -> CFArray {
let reference: CFArrayRef = mem::transmute(CFRetain(mem::transmute(reference)));
TCFType::wrap_under_create_rule(reference)
}

#[inline]
fn as_CFTypeRef(&self) -> CFTypeRef {
unsafe {
mem::transmute(self.as_concrete_TypeRef())
}
}

#[inline]
unsafe fn wrap_under_create_rule(obj: CFArrayRef) -> CFArray {
CFArray {
obj: obj,
}
}

#[inline]
fn type_id() -> CFTypeID {
unsafe {
CFArrayGetTypeID()
}
}
}
impl_TCFType!(CFArray, CFArrayRef, CFArrayGetTypeID);

impl CFArray {
/// Creates a new `CFArray` with the given elements, which must be `CFType` objects.
pub fn from_CFTypes(elems: &[CFType]) -> CFArray {
pub fn from_CFTypes<R, T>(elems: &[T]) -> CFArray where T: TCFType<R> {
unsafe {
let elems: Vec<CFTypeRef> = elems.iter().map(|elem| elem.as_CFTypeRef()).collect();
let array_ref = CFArrayCreate(kCFAllocatorDefault,
@@ -128,7 +92,7 @@ impl CFArray {
///
/// Careful; the loop body must wrap the reference properly. Generally, when array elements are
/// Core Foundation objects (not always true), they need to be wrapped with
/// `TCFType::wrap_under_get_rule()`. The safer `iter_CFTypes` method will do this for you.
/// `TCFType::wrap_under_get_rule()`.
#[inline]
pub fn iter<'a>(&'a self) -> CFArrayIterator<'a> {
CFArrayIterator {
@@ -140,19 +104,28 @@ impl CFArray {
#[inline]
pub fn len(&self) -> CFIndex {
unsafe {
CFArrayGetCount(self.obj)
CFArrayGetCount(self.0)
}
}

#[inline]
pub fn get(&self, index: CFIndex) -> *const c_void {
assert!(index < self.len());
unsafe {
CFArrayGetValueAtIndex(self.obj, index)
CFArrayGetValueAtIndex(self.0, index)
}
}
}

impl<'a> IntoIterator for &'a CFArray {
type Item = *const c_void;
type IntoIter = CFArrayIterator<'a>;

fn into_iter(self) -> CFArrayIterator<'a> {
self.iter()
}
}

#[link(name = "CoreFoundation", kind = "framework")]
extern {
/*
@@ -72,23 +72,21 @@ struct __CFType;
pub type CFTypeRef = *const __CFType;

/// Superclass of all Core Foundation objects.
pub struct CFType {
obj: CFTypeRef,
}
pub struct CFType(CFTypeRef);

impl Clone for CFType {
#[inline]
fn clone(&self) -> CFType {
unsafe {
TCFType::wrap_under_get_rule(self.obj)
TCFType::wrap_under_get_rule(self.0)
}
}
}

impl Drop for CFType {
fn drop(&mut self) {
unsafe {
CFRelease(self.obj)
CFRelease(self.0)
}
}
}
@@ -156,7 +154,7 @@ pub trait TCFType<ConcreteTypeRef> {
impl TCFType<CFTypeRef> for CFType {
#[inline]
fn as_concrete_TypeRef(&self) -> CFTypeRef {
self.obj
self.0
}

#[inline]
@@ -172,9 +170,7 @@ impl TCFType<CFTypeRef> for CFType {

#[inline]
unsafe fn wrap_under_create_rule(obj: CFTypeRef) -> CFType {
CFType {
obj: obj,
}
CFType(obj)
}

#[inline]
@@ -9,7 +9,7 @@

//! A Boolean type.

use base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType};
use base::{CFRelease, CFTypeID, TCFType};
use std::mem;

pub type Boolean = u32;
@@ -22,9 +22,7 @@ pub type CFBooleanRef = *const __CFBoolean;
/// A Boolean type.
///
/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug.
pub struct CFBoolean {
obj: CFBooleanRef,
}
pub struct CFBoolean(CFBooleanRef);

impl Drop for CFBoolean {
fn drop(&mut self) {
@@ -34,38 +32,7 @@ impl Drop for CFBoolean {
}
}

impl TCFType<CFBooleanRef> for CFBoolean {
#[inline]
fn as_concrete_TypeRef(&self) -> CFBooleanRef {
self.obj
}

#[inline]
unsafe fn wrap_under_get_rule(reference: CFBooleanRef) -> CFBoolean {
let reference: CFBooleanRef = mem::transmute(CFRetain(mem::transmute(reference)));
TCFType::wrap_under_create_rule(reference)
}

#[inline]
fn as_CFTypeRef(&self) -> CFTypeRef {
unsafe {
mem::transmute(self.as_concrete_TypeRef())
}
}

unsafe fn wrap_under_create_rule(obj: CFBooleanRef) -> CFBoolean {
CFBoolean {
obj: obj,
}
}

#[inline]
fn type_id() -> CFTypeID {
unsafe {
CFBooleanGetTypeID()
}
}
}
impl_TCFType!(CFBoolean, CFBooleanRef, CFBooleanGetTypeID);

impl CFBoolean {
pub fn true_value() -> CFBoolean {
@@ -9,7 +9,7 @@

//! Core Foundation Bundle Type

use base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType};
use base::{CFRelease, CFTypeID, TCFType};
use std::mem;

use string::CFStringRef;
@@ -21,11 +21,7 @@ struct __CFBundle;
pub type CFBundleRef = *const __CFBundle;

/// A Bundle type.
///
/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug.
pub struct CFBundle {
obj: CFBundleRef,
}
pub struct CFBundle(CFBundleRef);

impl Drop for CFBundle {
fn drop(&mut self) {
@@ -35,38 +31,7 @@ impl Drop for CFBundle {
}
}

impl TCFType<CFBundleRef> for CFBundle {
#[inline]
fn as_concrete_TypeRef(&self) -> CFBundleRef {
self.obj
}

#[inline]
unsafe fn wrap_under_get_rule(reference: CFBundleRef) -> CFBundle {
let reference: CFBundleRef = mem::transmute(CFRetain(mem::transmute(reference)));
TCFType::wrap_under_create_rule(reference)
}

#[inline]
fn as_CFTypeRef(&self) -> CFTypeRef {
unsafe {
mem::transmute(self.as_concrete_TypeRef())
}
}

unsafe fn wrap_under_create_rule(obj: CFBundleRef) -> CFBundle {
CFBundle {
obj: obj,
}
}

#[inline]
fn type_id() -> CFTypeID {
unsafe {
CFBundleGetTypeID()
}
}
}
impl_TCFType!(CFBundle, CFBundleRef, CFBundleGetTypeID);

#[link(name = "CoreFoundation", kind = "framework")]
extern {
@@ -9,22 +9,20 @@

//! Core Foundation byte buffers.

use base::{CFAllocatorRef, CFIndex, CFIndexConvertible, CFRelease, CFRetain};
use base::{CFTypeID, CFTypeRef, TCFType, kCFAllocatorDefault};
use base::{CFAllocatorRef, CFIndex, CFIndexConvertible, CFRelease};
use base::{CFTypeID, TCFType, kCFAllocatorDefault};

use std::mem;
use std::ops::Deref;
use std::slice;

#[repr(C)]
struct __CFData;

pub type CFDataRef = *const __CFData;

/// A byte buffer.
///
/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug.
pub struct CFData {
obj: CFDataRef,
}
pub struct CFData(CFDataRef);

impl Drop for CFData {
fn drop(&mut self) {
@@ -34,38 +32,7 @@ impl Drop for CFData {
}
}

impl TCFType<CFDataRef> for CFData {
#[inline]
fn as_concrete_TypeRef(&self) -> CFDataRef {
self.obj
}

#[inline]
unsafe fn wrap_under_get_rule(reference: CFDataRef) -> CFData {
let reference: CFDataRef = mem::transmute(CFRetain(mem::transmute(reference)));
TCFType::wrap_under_create_rule(reference)
}

#[inline]
fn as_CFTypeRef(&self) -> CFTypeRef {
unsafe {
mem::transmute(self.as_concrete_TypeRef())
}
}

unsafe fn wrap_under_create_rule(obj: CFDataRef) -> CFData {
CFData {
obj: obj,
}
}

#[inline]
fn type_id() -> CFTypeID {
unsafe {
CFDataGetTypeID()
}
}
}
impl_TCFType!(CFData, CFDataRef, CFDataGetTypeID);

impl CFData {
pub fn from_buffer(buffer: &[u8]) -> CFData {
@@ -82,19 +49,28 @@ impl CFData {
#[inline]
pub fn bytes<'a>(&'a self) -> &'a [u8] {
unsafe {
mem::transmute((CFDataGetBytePtr(self.obj), self.len() as usize))
slice::from_raw_parts(CFDataGetBytePtr(self.0), self.len() as usize)
}
}

/// Returns the length of this byte buffer.
#[inline]
pub fn len(&self) -> CFIndex {
unsafe {
CFDataGetLength(self.obj)
CFDataGetLength(self.0)
}
}
}

impl Deref for CFData {
type Target = [u8];

#[inline]
fn deref(&self) -> &[u8] {
self.bytes()
}
}

#[link(name = "CoreFoundation", kind = "framework")]
extern {
/*
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.