Skip to content

Commit

Permalink
Removed lots of type annotations.
Browse files Browse the repository at this point in the history
Now possible thanks to rust-lang/rust#22172
  • Loading branch information
SSheldon committed Feb 27, 2015
1 parent e86d26a commit 6069aed
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 82 deletions.
14 changes: 7 additions & 7 deletions core/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,27 @@ pub trait EncodePtr : MarkerTrait {
}

impl<'a, T> Encode for &'a T where T: EncodePtr {
fn code() -> &'static str { <T as EncodePtr>::ptr_code() }
fn code() -> &'static str { T::ptr_code() }
}

impl<'a, T> Encode for &'a mut T where T: EncodePtr {
fn code() -> &'static str { <T as EncodePtr>::ptr_code() }
fn code() -> &'static str { T::ptr_code() }
}

impl<'a, T> Encode for Option<&'a T> where T: EncodePtr {
fn code() -> &'static str { <T as EncodePtr>::ptr_code() }
fn code() -> &'static str { T::ptr_code() }
}

impl<'a, T> Encode for Option<&'a mut T> where T: EncodePtr {
fn code() -> &'static str { <T as EncodePtr>::ptr_code() }
fn code() -> &'static str { T::ptr_code() }
}

impl<T> Encode for *const T where T: EncodePtr {
fn code() -> &'static str { <T as EncodePtr>::ptr_code() }
fn code() -> &'static str { T::ptr_code() }
}

impl<T> Encode for *mut T where T: EncodePtr {
fn code() -> &'static str { <T as EncodePtr>::ptr_code() }
fn code() -> &'static str { T::ptr_code() }
}

impl EncodePtr for Object {
Expand All @@ -140,7 +140,7 @@ impl EncodePtr for Class {

/// Returns the Objective-C type encoding for a type.
pub fn encode<T>() -> &'static str where T: Encode {
<T as Encode>::code()
T::code()
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion core/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<T> Id<T, Owned> where T: Message {

impl<T, O> Encode for Id<T, O> where T: EncodePtr {
fn code() -> &'static str {
<T as EncodePtr>::ptr_code()
T::ptr_code()
}
}

Expand Down
37 changes: 18 additions & 19 deletions foundation/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub trait INSArray : INSObject {
}

unsafe fn from_refs(refs: &[&Self::Item]) -> Id<Self> {
let cls = <Self as INSObject>::class();
let cls = Self::class();
let obj: *mut Self = msg_send![cls, alloc];
let obj: *mut Self = msg_send![obj, initWithObjects:refs.as_ptr()
count:refs.len()];
Expand All @@ -106,7 +106,7 @@ pub trait INSArray : INSObject {

fn objects_in_range(&self, range: Range<usize>) -> Vec<&Self::Item> {
let range = NSRange::from_range(range);
let mut vec: Vec<&Self::Item> = Vec::with_capacity(range.length);
let mut vec = Vec::with_capacity(range.length);
unsafe {
let _: () = msg_send![self, getObjects:vec.as_ptr() range:range];
vec.set_len(range.length);
Expand Down Expand Up @@ -326,16 +326,16 @@ mod tests {
use super::{INSArray, INSMutableArray, NSArray, NSMutableArray};

fn sample_array(len: usize) -> Id<NSArray<NSObject>> {
let mut vec: Vec<Id<NSObject>> = Vec::with_capacity(len);
let mut vec = Vec::with_capacity(len);
for _ in 0..len {
vec.push(INSObject::new());
vec.push(NSObject::new());
}
INSArray::from_vec(vec)
NSArray::from_vec(vec)
}

#[test]
fn test_count() {
let empty_array: Id<NSArray<NSObject>> = INSObject::new();
let empty_array = NSArray::<NSObject>::new();
assert!(empty_array.count() == 0);

let array = sample_array(4);
Expand Down Expand Up @@ -390,35 +390,34 @@ mod tests {

#[test]
fn test_add_object() {
let mut array: Id<NSMutableArray<NSObject>> = INSObject::new();
let obj: Id<NSObject> = INSObject::new();
let mut array = NSMutableArray::new();
let obj = NSObject::new();
array.add_object(obj);

assert!(array.count() == 1);
assert!(array.object_at(0) == array.object_at(0));

let obj: Id<NSObject> = INSObject::new();
let obj = NSObject::new();
array.insert_object_at(0, obj);
assert!(array.count() == 2);
}

#[test]
fn test_replace_object() {
let mut array: Id<NSMutableArray<NSObject>> = INSObject::new();
let obj: Id<NSObject> = INSObject::new();
let mut array = NSMutableArray::new();
let obj = NSObject::new();
array.add_object(obj);

let obj: Id<NSObject> = INSObject::new();
let obj = NSObject::new();
let old_obj = array.replace_object_at(0, obj);
assert!(&*old_obj != array.object_at(0));
}

#[test]
fn test_remove_object() {
let mut array: Id<NSMutableArray<NSObject>> = INSObject::new();
let mut array = NSMutableArray::new();
for _ in 0..4 {
let obj: Id<NSObject> = INSObject::new();
array.add_object(obj);
array.add_object(NSObject::new());
}

array.remove_object_at(1);
Expand All @@ -433,11 +432,11 @@ mod tests {

#[test]
fn test_sort() {
let strings: Vec<Id<NSString>> = vec![
INSString::from_str("hello"),
INSString::from_str("hi"),
let strings = vec![
NSString::from_str("hello"),
NSString::from_str("hi"),
];
let mut strings: Id<NSMutableArray<_>> = INSArray::from_vec(strings);
let mut strings = NSMutableArray::from_vec(strings);

strings.sort_by(|s1, s2| s1.as_str().len().cmp(&s2.as_str().len()));
assert!(strings[0].as_str() == "hi");
Expand Down
19 changes: 9 additions & 10 deletions foundation/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait INSData : INSObject {
}

fn with_bytes(bytes: &[u8]) -> Id<Self> {
let cls = <Self as INSObject>::class();
let cls = Self::class();
unsafe {
let obj: *mut Self = msg_send![cls, alloc];
let obj: *mut Self = msg_send![obj, initWithBytes:bytes.as_ptr()
Expand All @@ -47,7 +47,7 @@ pub trait INSData : INSObject {
let dealloc: &mut Block<(*mut c_void, usize), ()> = &mut dealloc;

let mut bytes = bytes;
let cls = <Self as INSObject>::class();
let cls = Self::class();
unsafe {
let obj: *mut Self = msg_send![cls, alloc];
let obj: *mut Self = msg_send![obj, initWithBytesNoCopy:bytes.as_mut_ptr()
Expand Down Expand Up @@ -129,34 +129,33 @@ impl INSMutableCopying for NSMutableData {

#[cfg(test)]
mod tests {
use objc::Id;
use INSObject;
use super::{INSData, INSMutableData, NSData, NSMutableData};

#[test]
fn test_bytes() {
let bytes = [3, 7, 16, 52, 112, 19];
let data: Id<NSData> = INSData::with_bytes(&bytes);
let data = NSData::with_bytes(&bytes);
assert!(data.len() == bytes.len());
assert!(data.bytes() == bytes);
}

#[test]
fn test_no_bytes() {
let data: Id<NSData> = INSObject::new();
let data = NSData::new();
assert!(Some(data.bytes()).is_some());
}

#[test]
fn test_bytes_mut() {
let mut data: Id<NSMutableData> = INSData::with_bytes(&[7, 16]);
let mut data = NSMutableData::with_bytes(&[7, 16]);
data.bytes_mut()[0] = 3;
assert!(data.bytes() == [3, 16]);
}

#[test]
fn test_set_len() {
let mut data: Id<NSMutableData> = INSData::with_bytes(&[7, 16]);
let mut data = NSMutableData::with_bytes(&[7, 16]);
data.set_len(4);
assert!(data.len() == 4);
assert!(data.bytes() == [7, 16, 0, 0]);
Expand All @@ -168,15 +167,15 @@ mod tests {

#[test]
fn test_append() {
let mut data: Id<NSMutableData> = INSData::with_bytes(&[7, 16]);
let mut data = NSMutableData::with_bytes(&[7, 16]);
data.append(&[3, 52]);
assert!(data.len() == 4);
assert!(data.bytes() == [7, 16, 3, 52]);
}

#[test]
fn test_replace() {
let mut data: Id<NSMutableData> = INSData::with_bytes(&[7, 16]);
let mut data = NSMutableData::with_bytes(&[7, 16]);
data.replace_range(0..0, &[3]);
assert!(data.bytes() == [3, 7, 16]);

Expand All @@ -195,7 +194,7 @@ mod tests {
let bytes = vec![3, 7, 16];
let bytes_ptr = bytes.as_ptr();

let data: Id<NSData> = INSData::from_vec(bytes);
let data = NSData::from_vec(bytes);
assert!(data.bytes().as_ptr() == bytes_ptr);
}
}
20 changes: 10 additions & 10 deletions foundation/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub trait INSDictionary : INSObject {

fn keys(&self) -> Vec<&Self::Key> {
let len = self.count();
let mut keys: Vec<&Self::Key> = Vec::with_capacity(len);
let mut keys = Vec::with_capacity(len);
unsafe {
let _: () = msg_send![self, getObjects:ptr::null_mut::<Self::Value>()
andKeys:keys.as_mut_ptr()];
Expand All @@ -42,7 +42,7 @@ pub trait INSDictionary : INSObject {

fn values(&self) -> Vec<&Self::Value> {
let len = self.count();
let mut vals: Vec<&Self::Value> = Vec::with_capacity(len);
let mut vals = Vec::with_capacity(len);
unsafe {
let _: () = msg_send![self, getObjects:vals.as_mut_ptr()
andKeys:ptr::null_mut::<Self::Key>()];
Expand All @@ -53,8 +53,8 @@ pub trait INSDictionary : INSObject {

fn keys_and_objects(&self) -> (Vec<&Self::Key>, Vec<&Self::Value>) {
let len = self.count();
let mut keys: Vec<&Self::Key> = Vec::with_capacity(len);
let mut objs: Vec<&Self::Value> = Vec::with_capacity(len);
let mut keys = Vec::with_capacity(len);
let mut objs = Vec::with_capacity(len);
unsafe {
let _: () = msg_send![self, getObjects:objs.as_mut_ptr()
andKeys:keys.as_mut_ptr()];
Expand Down Expand Up @@ -87,7 +87,7 @@ pub trait INSDictionary : INSObject {

unsafe fn from_refs<T>(keys: &[&T], vals: &[&Self::Value]) -> Id<Self>
where T: INSCopying<Output=Self::Key> {
let cls = <Self as INSObject>::class();
let cls = Self::class();
let count = min(keys.len(), vals.len());
let obj: *mut Self = msg_send![cls, alloc];
let obj: *mut Self = msg_send![obj, initWithObjects:vals.as_ptr()
Expand Down Expand Up @@ -153,9 +153,9 @@ mod tests {
use super::{INSDictionary, NSDictionary};

fn sample_dict(key: &str) -> Id<NSDictionary<NSString, NSObject>> {
let string: Id<NSString> = INSString::from_str(key);
let obj: Id<NSObject> = INSObject::new();
INSDictionary::from_keys_and_objects(&[&*string], vec![obj])
let string = NSString::from_str(key);
let obj = NSObject::new();
NSDictionary::from_keys_and_objects(&[&*string], vec![obj])
}

#[test]
Expand All @@ -168,10 +168,10 @@ mod tests {
fn test_object_for() {
let dict = sample_dict("abcd");

let string: Id<NSString> = INSString::from_str("abcd");
let string = NSString::from_str("abcd");
assert!(dict.object_for(&string).is_some());

let string: Id<NSString> = INSString::from_str("abcde");
let string = NSString::from_str("abcde");
assert!(dict.object_for(&string).is_none());
}

Expand Down
9 changes: 4 additions & 5 deletions foundation/enumerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,13 @@ impl<'a, C: INSFastEnumeration> Iterator for NSFastEnumerator<'a, C> {

#[cfg(test)]
mod tests {
use objc::Id;
use {INSArray, INSValue, NSArray, NSValue};
use super::INSFastEnumeration;

#[test]
fn test_enumerator() {
let vec: Vec<Id<NSValue<u32>>> = (0..4).map(INSValue::from_value).collect();
let array: Id<NSArray<_>> = INSArray::from_vec(vec);
let vec = (0u32..4).map(NSValue::from_value).collect();
let array = NSArray::from_vec(vec);

let enumerator = array.object_enumerator();
assert!(enumerator.count() == 4);
Expand All @@ -153,8 +152,8 @@ mod tests {

#[test]
fn test_fast_enumerator() {
let vec: Vec<Id<NSValue<u32>>> = (0..4).map(INSValue::from_value).collect();
let array: Id<NSArray<_>> = INSArray::from_vec(vec);
let vec = (0u32..4).map(NSValue::from_value).collect();
let array = NSArray::from_vec(vec);

let enumerator = array.enumerator();
assert!(enumerator.count() == 4);
Expand Down
8 changes: 4 additions & 4 deletions foundation/examples/custom_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate objc_foundation;

use std::sync::{Once, ONCE_INIT};

use objc::{EncodePtr, Id, Message};
use objc::{EncodePtr, Message};
use objc::declare::{ClassDecl, MethodDecl};
use objc::runtime::{Class, Object, Sel};
use objc_foundation::{INSObject, NSObject};
Expand All @@ -17,7 +17,7 @@ impl MYObject {
&*(self as *const _ as *const Object)
};
unsafe {
*obj.get_ivar::<u32>("_number")
*obj.get_ivar("_number")
}
}

Expand All @@ -42,7 +42,7 @@ static MYOBJECT_REGISTER_CLASS: Once = ONCE_INIT;
impl INSObject for MYObject {
fn class() -> &'static Class {
MYOBJECT_REGISTER_CLASS.call_once(|| {
let superclass = <NSObject as INSObject>::class();
let superclass = NSObject::class();
let mut decl = ClassDecl::new(superclass, "MYObject").unwrap();
decl.add_ivar::<u32>("_number");

Expand All @@ -69,7 +69,7 @@ impl INSObject for MYObject {
}

fn main() {
let mut obj: Id<MYObject> = INSObject::new();
let mut obj = MYObject::new();

obj.set_number(7);
println!("Number: {}", unsafe {
Expand Down
Loading

0 comments on commit 6069aed

Please sign in to comment.