From ea8d2bd026cd91d3fb5fd753677da3fd7d21dbab Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 25 Jun 2013 19:45:15 -0700 Subject: [PATCH] Update for language changes --- array.rs | 16 ++++++++++------ base.rs | 32 +++++++++++++++++--------------- boolean.rs | 6 +++--- data.rs | 16 ++++++++-------- dictionary.rs | 26 +++++++++++++++----------- number.rs | 20 +++++++++++--------- set.rs | 8 +++++--- string.rs | 16 +++++++++++----- 8 files changed, 80 insertions(+), 60 deletions(-) diff --git a/array.rs b/array.rs index 4640edd..8d36136 100644 --- a/array.rs +++ b/array.rs @@ -16,7 +16,11 @@ use base::{ CFWrapper, kCFAllocatorDefault, }; -use core::libc::c_void; +use std::cast; +use std::libc::c_void; +use std::ptr; +use std::uint; +use std::vec; pub type CFArrayRetainCallBack = *u8; pub type CFArrayReleaseCallBack = *u8; @@ -44,22 +48,22 @@ pub struct CFArray { contents: CFWrapper } -pub impl CFArray { - fn wrap_shared(array: CFArrayRef) -> CFArray { +impl CFArray { + pub fn wrap_shared(array: CFArrayRef) -> CFArray { CFArray { contents: CFWrapper::wrap_shared(array) } } - fn wrap_owned(array: CFArrayRef) -> CFArray { + pub fn wrap_owned(array: CFArrayRef) -> CFArray { CFArray { contents: CFWrapper::wrap_owned(array) } } - fn new(elems: &[ElemRefType]) -> CFArray { + pub fn new(elems: &[ElemRefType]) -> CFArray { let array_ref: CFArrayRef; - let elems_refs = do vec::map(elems) |e: &ElemRefType| { e.as_type_ref() }; + let elems_refs = do elems.map |e: &ElemRefType| { e.as_type_ref() }; unsafe { array_ref = CFArrayCreate(kCFAllocatorDefault, diff --git a/base.rs b/base.rs index f7241f2..a18211a 100644 --- a/base.rs +++ b/base.rs @@ -7,7 +7,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use core::libc::c_long; +use std::cast; +use std::libc; +use std::libc::c_long; // a raw Core Foundation reference. It may or may not have been // CFRetain'ed, depending on whether it was obtained via ownership or @@ -80,12 +82,12 @@ impl Drop for CFWrapper { pub type CFType = CFWrapper; -pub impl<'self, T:Copy + AbstractCFTypeRef, E1, E2> CFWrapper { - fn borrow_ref(&'self self) -> &'self T { +impl<'self, T:Copy + AbstractCFTypeRef, E1, E2> CFWrapper { + pub fn borrow_ref(&'self self) -> &'self T { &self.obj } - fn borrow_type_ref(&self) -> &'self CFTypeRef { + pub fn borrow_type_ref(&self) -> &'self CFTypeRef { unsafe { cast::transmute::<&T, &CFTypeRef>(&self.obj) } @@ -93,7 +95,7 @@ pub impl<'self, T:Copy + AbstractCFTypeRef, E1, E2> CFWrapper { // Use this when following Core Foundation's "Create" rule; i.e., the wrapper assumes ownership. // The object has already been retained, so we need not increment the retain count ourself. - fn wrap_owned(some_ref: T) -> CFWrapper { + pub fn wrap_owned(some_ref: T) -> CFWrapper { // N.B. we can't make any assertions about retain count here, // because returned things are only guaranteed to be already // retained. Strings, for example, could be interned. @@ -102,46 +104,46 @@ pub impl<'self, T:Copy + AbstractCFTypeRef, E1, E2> CFWrapper { // Use this when following Core Foundation's "Get" rule. The wrapper does not have ownership. // Twe need to increment object's the retain count so it isn't freed out from under our noses. - fn wrap_shared(some_ref: T) -> CFWrapper { + pub fn wrap_shared(some_ref: T) -> CFWrapper { unsafe { CFRetain(some_ref.as_type_ref()); } CFWrapper { obj: some_ref } } // Unwraps the wrapper, returning the underlying AbstractCFType. - fn unwrap(wrapper: CFWrapper) -> T { - wrapper.obj + pub fn unwrap(wrapper: CFWrapper) -> T { + copy wrapper.obj } - fn to_CFType(wrapper: CFWrapper) -> CFType { + pub fn to_CFType(wrapper: CFWrapper) -> CFType { unsafe { cast::transmute::, CFType>(wrapper) } } - fn from_CFType(wrapper: CFType) -> CFWrapper { + pub fn from_CFType(wrapper: CFType) -> CFWrapper { unsafe { assert!(wrapper.type_id() == AbstractCFTypeRef::type_id::()); cast::transmute::>(wrapper) } } - fn clone(wrapper: &CFWrapper) -> CFWrapper { - CFWrapper::wrap_shared(*wrapper.borrow_ref()) + pub fn clone(wrapper: &CFWrapper) -> CFWrapper { + CFWrapper::wrap_shared(copy *wrapper.borrow_ref()) } - fn retain_count(&self) -> CFIndex { + pub fn retain_count(&self) -> CFIndex { unsafe { CFGetRetainCount(*self.borrow_type_ref()) } } - fn type_id(&self) -> CFTypeID { + pub fn type_id(&self) -> CFTypeID { unsafe { CFGetTypeID(*self.borrow_type_ref()) } } - fn show(&self) { + pub fn show(&self) { unsafe { CFShow(*self.borrow_type_ref()); } diff --git a/boolean.rs b/boolean.rs index d2d7fbc..dbfc089 100644 --- a/boolean.rs +++ b/boolean.rs @@ -33,14 +33,14 @@ pub struct CFBoolean { contents: CFWrapper } -pub impl CFBoolean { - fn true_value() -> CFBoolean { +impl CFBoolean { + pub fn true_value() -> CFBoolean { CFBoolean { contents: CFWrapper::wrap_shared(kCFBooleanTrue) } } - fn false_value() -> CFBoolean { + pub fn false_value() -> CFBoolean { CFBoolean { contents: CFWrapper::wrap_shared(kCFBooleanFalse) } diff --git a/data.rs b/data.rs index 781fe5d..3c906aa 100644 --- a/data.rs +++ b/data.rs @@ -18,7 +18,7 @@ use base::{ kCFAllocatorDefault, }; -use core::vec; +use std::vec; struct __CFData { private: () } pub type CFDataRef = *__CFData; @@ -39,14 +39,14 @@ pub struct CFData { contents: CFWrapper } -pub impl CFData { - fn wrap_owned(data: CFDataRef) -> CFData { +impl CFData { + pub fn wrap_owned(data: CFDataRef) -> CFData { CFData { contents: CFWrapper::wrap_owned(data) } } - fn new_from_buf(buf: &[u8]) -> CFData { + pub fn new_from_buf(buf: &[u8]) -> CFData { let result; unsafe { result = CFDataCreate(kCFAllocatorDefault, @@ -60,25 +60,25 @@ pub impl CFData { } // tread with caution; read-only - fn bytes(&self) -> *u8 { + pub fn bytes(&self) -> *u8 { unsafe { CFDataGetBytePtr(self.contents.obj) } } - fn len(&self) -> uint { + pub fn len(&self) -> uint { unsafe { CFDataGetLength(self.contents.obj) as uint } } - fn copy_to_buf(&self) -> ~[u8] { + pub fn copy_to_buf(&self) -> ~[u8] { unsafe { vec::from_buf(self.bytes(), self.len()) } } - fn with_buf(&self, blk: &fn(v: &[u8]) -> U) -> U { + pub fn with_buf(&self, blk: &fn(v: &[u8]) -> U) -> U { unsafe { vec::raw::buf_as_slice(self.bytes(), self.len(), blk) } diff --git a/dictionary.rs b/dictionary.rs index 3d86b8c..1be8ff2 100644 --- a/dictionary.rs +++ b/dictionary.rs @@ -19,7 +19,11 @@ use base::{ }; use string::CFStringRef; -use core::libc::c_void; +use std::cast; +use std::libc::c_void; +use std::ptr; +use std::uint; +use std::vec; pub type CFDictionaryApplierFunction = *u8; pub type CFDictionaryCopyDescriptionCallBack = *u8; @@ -66,21 +70,21 @@ pub struct CFDictionary { pub type UntypedCFDictionary = CFDictionary; -pub impl +impl CFDictionary { - fn wrap_owned(dictionary: CFDictionaryRef) -> CFDictionary { + pub fn wrap_owned(dictionary: CFDictionaryRef) -> CFDictionary { CFDictionary { contents: CFWrapper::wrap_owned(dictionary) } } - fn new(pairs: &[(KeyRefType,ValueRefType)]) -> CFDictionary { + pub fn new(pairs: &[(KeyRefType,ValueRefType)]) -> CFDictionary { let mut keys : ~[CFTypeRef] = ~[]; let mut values : ~[CFTypeRef] = ~[]; for pairs.each |pair| { // FIXME: "let" would be much nicer here, but that doesn't work yet. match *pair { - (key, value) => { + (ref key, ref value) => { keys.push(key.as_type_ref()); values.push(value.as_type_ref()); } @@ -106,17 +110,17 @@ pub impl uint { + pub fn len(&self) -> uint { unsafe { CFDictionaryGetCount(self.contents.obj) as uint } } - fn is_empty(&self) -> bool { + pub fn is_empty(&self) -> bool { self.len() == 0 } - fn contains_key(&self, key: &KeyRefType) -> bool { + pub fn contains_key(&self, key: &KeyRefType) -> bool { unsafe { CFDictionaryContainsKey(self.contents.obj, cast::transmute::(key.as_type_ref())) @@ -124,7 +128,7 @@ pub impl Option { + pub fn find(&self, key: &KeyRefType) -> Option { unsafe { let value : *c_void = ptr::null(); let did_find_value = CFDictionaryGetValueIfPresent( @@ -142,7 +146,7 @@ pub impl ValueRefType { + pub fn get(&self, key: &KeyRefType) -> ValueRefType { let value = self.find(key); if value.is_none() { fail!(fmt!("No entry found for key: %?", key)); @@ -150,7 +154,7 @@ pub impl bool) -> bool { + pub fn each(&self, blk: &fn(&KeyRefType, &ValueRefType) -> bool) -> bool { unsafe { let len = self.len(); let null_keys = cast::transmute::<*c_void,KeyRefType>(ptr::null()); diff --git a/number.rs b/number.rs index 03a3062..44a6101 100644 --- a/number.rs +++ b/number.rs @@ -10,7 +10,9 @@ use base::{AbstractCFTypeRef, Boolean, CFAllocatorRef, CFTypeID, CFTypeRef, CFWrapper}; use base::{kCFAllocatorDefault}; -use core::libc::c_void; +use std::cast; +use std::libc; +use std::libc::c_void; pub type CFNumberType = u32; @@ -50,20 +52,20 @@ pub struct CFNumber { contents: CFWrapper } -pub impl CFNumber { - fn wrap_owned(number: CFNumberRef) -> CFNumber { +impl CFNumber { + pub fn wrap_owned(number: CFNumberRef) -> CFNumber { CFNumber { contents: CFWrapper::wrap_owned(number) } } - fn wrap_shared(number: CFNumberRef) -> CFNumber { + pub fn wrap_shared(number: CFNumberRef) -> CFNumber { CFNumber { contents: CFWrapper::wrap_shared(number) } } - fn new(n: T) -> CFNumber { + pub fn new(n: T) -> CFNumber { unsafe { let objref = CFNumberCreate(kCFAllocatorDefault, n.cf_number_type(), @@ -74,7 +76,7 @@ pub impl CFNumber { } } - fn to_i8(&self) -> i8 { + pub fn to_i8(&self) -> i8 { let ty = kCFNumberSInt8Type; assert!(self.has_number_type(ty)); unsafe { @@ -86,7 +88,7 @@ pub impl CFNumber { } } - fn to_i16(&self) -> i16 { + pub fn to_i16(&self) -> i16 { let ty = kCFNumberSInt16Type; assert!(self.has_number_type(ty)); unsafe { @@ -98,7 +100,7 @@ pub impl CFNumber { } } - fn to_i32(&self) -> i32 { + pub fn to_i32(&self) -> i32 { let ty = kCFNumberSInt32Type; assert!(self.has_number_type(ty)); unsafe { @@ -110,7 +112,7 @@ pub impl CFNumber { } } - fn to_float(&self) -> float { + pub fn to_float(&self) -> float { unsafe { assert!(self.has_float_type()); let ty = CFNumberGetType(self.contents.obj); diff --git a/set.rs b/set.rs index 8bf0404..3057a34 100644 --- a/set.rs +++ b/set.rs @@ -17,7 +17,9 @@ use base::{ CFWrapper, kCFAllocatorDefault, }; -use core::libc::c_void; +use std::cast; +use std::libc::c_void; +use std::vec; pub type CFSetRetainCallBack = *u8; pub type CFSetReleaseCallBack = *u8; @@ -53,8 +55,8 @@ pub struct CFSet { contents: CFWrapper } -pub impl CFSet { - fn new(elems: &[ElemRefType]) -> CFSet { +impl CFSet { + pub fn new(elems: &[ElemRefType]) -> CFSet { let result: CFSetRef; let elems_refs = do vec::map(elems) |e: &ElemRefType| { e.as_type_ref() diff --git a/string.rs b/string.rs index b3daddf..a1aeaa7 100644 --- a/string.rs +++ b/string.rs @@ -10,6 +10,12 @@ use base::{AbstractCFTypeRef, Boolean, CFAllocatorRef, CFIndex, CFOptionFlags, CFRange}; use base::{CFRangeMake, CFTypeRef, CFTypeID, CFWrapper, kCFAllocatorDefault, kCFAllocatorNull}; +use std::cast; +use std::libc; +use std::ptr; +use std::str; +use std::vec; + pub type UniChar = libc::c_ushort; /* @@ -207,10 +213,10 @@ pub struct CFString { contents: CFWrapper } -pub impl CFString { +impl CFString { // convenience method to make it easier to wrap extern // CFStringRefs without providing explicit typarams to base::wrap() - fn wrap_owned(string: CFStringRef) -> CFString { + pub fn wrap_owned(string: CFStringRef) -> CFString { CFString { contents: CFWrapper::wrap_owned(string) } @@ -218,7 +224,7 @@ pub impl CFString { // convenience method to make it easier to wrap extern // CFStringRefs without providing explicit typarams to base::wrap() - fn wrap_shared(string: CFStringRef) -> CFString { + pub fn wrap_shared(string: CFStringRef) -> CFString { CFString { contents: CFWrapper::wrap_shared(string) } @@ -226,7 +232,7 @@ pub impl CFString { // like CFString::new, but references a string that can be used as // a backing store by virtue of being statically allocated. - fn new_static(string: &'static str) -> CFString { + pub fn new_static(string: &'static str) -> CFString { let string_ref = do str::as_buf(string) |bytes, len| { unsafe { CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, @@ -243,7 +249,7 @@ pub impl CFString { } } - fn new(string: &str) -> CFString { + pub fn new(string: &str) -> CFString { let string_ref = do str::as_buf(string) |bytes, len| { unsafe { CFStringCreateWithBytes(kCFAllocatorDefault,