From c9afa566cc0122885bf68054534deb87dad93030 Mon Sep 17 00:00:00 2001 From: Sam Ward Date: Fri, 9 Jan 2015 22:34:21 -0800 Subject: [PATCH 1/2] Update for 1.0.0-alpha compatibility --- src/array.rs | 7 +++++-- src/base.rs | 5 ++--- src/data.rs | 6 +++--- src/dictionary.rs | 16 ++++++++-------- src/lib.rs | 2 +- src/number.rs | 1 - src/string.rs | 25 ++++++++++++++++--------- 7 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/array.rs b/src/array.rs index 2fdee58..e23227a 100644 --- a/src/array.rs +++ b/src/array.rs @@ -14,6 +14,7 @@ use base::{CFType, CFTypeID, CFTypeRef, TCFType}; use base::{kCFAllocatorDefault}; use libc::c_void; use std::mem; +use std::num::ToPrimitive; /// FIXME(pcwalton): This is wrong. pub type CFArrayRetainCallBack = *const u8; @@ -63,7 +64,9 @@ pub struct CFArrayIterator<'a> { index: CFIndex, } -impl<'a> Iterator<*const c_void> for CFArrayIterator<'a> { +impl<'a> Iterator for CFArrayIterator<'a> { + type Item = *const c_void; + fn next(&mut self) -> Option<*const c_void> { if self.index >= self.array.len() { None @@ -177,7 +180,7 @@ extern { fn should_box_and_unbox() { use number::{CFNumber, number}; - let arr = CFArray::from_CFTypes([ + let arr = CFArray::from_CFTypes(&[ number(1).as_CFType(), number(2).as_CFType(), number(3).as_CFType(), diff --git a/src/base.rs b/src/base.rs index bcd2df1..68cbd69 100644 --- a/src/base.rs +++ b/src/base.rs @@ -20,11 +20,11 @@ pub trait CFIndexConvertible { fn to_CFIndex(self) -> CFIndex; } -impl CFIndexConvertible for uint { +impl CFIndexConvertible for usize { #[inline] fn to_CFIndex(self) -> CFIndex { let max_CFIndex: CFIndex = Int::max_value(); - if self > (max_CFIndex as uint) { + if self > (max_CFIndex as usize) { panic!("value out of range") } self as CFIndex @@ -228,4 +228,3 @@ extern { /* Base Utilities Reference */ // N.B. Some things missing here. } - diff --git a/src/data.rs b/src/data.rs index b889d38..0deb88f 100644 --- a/src/data.rs +++ b/src/data.rs @@ -70,7 +70,7 @@ impl TCFType for CFData { impl CFData { pub fn from_buffer(buffer: &[u8]) -> CFData { unsafe { - let data_ref = CFDataCreate(kCFAllocatorDefault, + let data_ref = CFDataCreate(kCFAllocatorDefault, buffer.as_ptr(), buffer.len().to_CFIndex()); TCFType::wrap_under_create_rule(data_ref) @@ -82,7 +82,7 @@ impl CFData { #[inline] pub fn bytes<'a>(&'a self) -> &'a [u8] { unsafe { - mem::transmute((CFDataGetBytePtr(self.obj), self.len() as uint)) + mem::transmute((CFDataGetBytePtr(self.obj), self.len() as usize)) } } @@ -101,7 +101,7 @@ extern { * CFData.h */ - fn CFDataCreate(allocator: CFAllocatorRef, + fn CFDataCreate(allocator: CFAllocatorRef, bytes: *const u8, length: CFIndex) -> CFDataRef; //fn CFDataFind fn CFDataGetBytePtr(theData: CFDataRef) -> *const u8; diff --git a/src/dictionary.rs b/src/dictionary.rs index 2d8200b..29a98dc 100644 --- a/src/dictionary.rs +++ b/src/dictionary.rs @@ -102,10 +102,11 @@ impl TCFType for CFDictionary { impl CFDictionary { pub fn from_CFType_pairs(pairs: &[(CFType, CFType)]) -> CFDictionary { - let (keys, values) = - vec::unzip(pairs.iter() - .map(|&(ref key, ref value)| (key.as_CFTypeRef(), - value.as_CFTypeRef()))); + let (keys, values): (Vec,Vec) = + pairs.iter() + .map(|&(ref key, ref value)| (key.as_CFTypeRef(), value.as_CFTypeRef())) + .unzip(); + unsafe { let dictionary_ref = CFDictionaryCreate(kCFAllocatorDefault, mem::transmute(keys.as_ptr()), @@ -118,9 +119,9 @@ impl CFDictionary { } #[inline] - pub fn len(&self) -> uint { + pub fn len(&self) -> usize { unsafe { - CFDictionaryGetCount(self.obj) as uint + CFDictionaryGetCount(self.obj) as usize } } @@ -152,7 +153,7 @@ impl CFDictionary { pub fn get(&self, key: *const c_void) -> *const c_void { let value = self.find(key); if value.is_none() { - panic!("No entry found for key: {}", key); + panic!("No entry found for key [FIXME]"); // FIXME Not sure how to convert key to be printed } value.unwrap() } @@ -184,4 +185,3 @@ extern { fn CFDictionaryGetValueIfPresent(theDict: CFDictionaryRef, key: *const c_void, value: *mut *const c_void) -> Boolean; } - diff --git a/src/lib.rs b/src/lib.rs index d06c9eb..6337eda 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,7 +56,7 @@ pub mod test { let tru = CFBoolean::true_value(); let n42 = number(42); - let _d = CFDictionary::from_CFType_pairs([ + let _d = CFDictionary::from_CFType_pairs(&[ (bar.as_CFType(), boo.as_CFType()), (baz.as_CFType(), tru.as_CFType()), (foo.as_CFType(), n42.as_CFType()), diff --git a/src/number.rs b/src/number.rs index 1a29a99..99127ec 100644 --- a/src/number.rs +++ b/src/number.rs @@ -169,4 +169,3 @@ extern { //fn CFNumberCompare fn CFNumberGetTypeID() -> CFTypeID; } - diff --git a/src/string.rs b/src/string.rs index aececd0..ac76f39 100644 --- a/src/string.rs +++ b/src/string.rs @@ -18,6 +18,7 @@ use base::{kCFAllocatorDefault, kCFAllocatorNull}; use libc; use std::fmt; use std::str::FromStr; +use std::string::ToString; use std::mem; use std::ptr; use std::vec::Vec; @@ -277,16 +278,15 @@ impl FromStr for CFString { } } -impl fmt::Show for CFString { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { +impl ToString for CFString { + fn to_string(&self) -> String { unsafe { let char_len = self.char_len(); - let range = CFRange::init(0, char_len); // First, ask how big the buffer ought to be. let mut bytes_required: CFIndex = 0; CFStringGetBytes(self.obj, - range, + CFRange::init(0, char_len), kCFStringEncodingUTF8, 0, false as Boolean, @@ -295,26 +295,34 @@ impl fmt::Show for CFString { &mut bytes_required); // Then, allocate the buffer and actually copy. - let mut buffer: Vec = Vec::from_elem(bytes_required as uint, '\x00' as u8); + let mut buffer = Vec::with_capacity(bytes_required as usize); + for _ in (0..bytes_required) { buffer.push('\x00' as u8) } + let mut bytes_used: CFIndex = 0; let chars_written = CFStringGetBytes(self.obj, - range, + CFRange::init(0, char_len), kCFStringEncodingUTF8, 0, false as Boolean, buffer.as_mut_ptr(), buffer.len().to_CFIndex(), - &mut bytes_used) as uint; + &mut bytes_used) as usize; assert!(chars_written.to_CFIndex() == char_len); // This is dangerous; we over-allocate and null-terminate the string (during // initialization). assert!(bytes_used == buffer.len().to_CFIndex()); - String::from_utf8(buffer).unwrap().fmt(f) + String::from_utf8(buffer).unwrap() } } } +impl fmt::Show for CFString { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.to_string().fmt(f) + } +} + impl CFString { /// Like `CFString::from_string`, but references a string that can be used as a backing store @@ -467,4 +475,3 @@ fn string_and_back() { let converted = cfstr.to_string(); assert!(original == converted.as_slice()); } - From 14ac9f2a8cf72cce76762a8ecd134db57dfcba3c Mon Sep 17 00:00:00 2001 From: Sam Ward Date: Sat, 10 Jan 2015 01:08:08 -0800 Subject: [PATCH 2/2] Show key during panic --- src/dictionary.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dictionary.rs b/src/dictionary.rs index 29a98dc..57b8822 100644 --- a/src/dictionary.rs +++ b/src/dictionary.rs @@ -153,7 +153,7 @@ impl CFDictionary { pub fn get(&self, key: *const c_void) -> *const c_void { let value = self.find(key); if value.is_none() { - panic!("No entry found for key [FIXME]"); // FIXME Not sure how to convert key to be printed + panic!("No entry found for key {:p}", key); } value.unwrap() }