1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use std::cmp::min;
use std::marker::PhantomData;
use std::ops::Index;
use std::ptr;

use objc::runtime::Class;
use objc_id::{Id, Owned, Ownership, ShareId};

use {
    INSArray, INSFastEnumeration, INSCopying, INSObject,
    NSArray, NSSharedArray, NSEnumerator,
};

unsafe fn from_refs<D, T>(keys: &[&T], vals: &[&D::Value]) -> Id<D>
        where D: INSDictionary, T: INSCopying<Output=D::Key> {
    let cls = D::class();
    let count = min(keys.len(), vals.len());
    let obj: *mut D = msg_send![cls, alloc];
    let obj: *mut D = msg_send![obj, initWithObjects:vals.as_ptr()
                                             forKeys:keys.as_ptr()
                                               count:count];
    Id::from_retained_ptr(obj)
}

pub trait INSDictionary : INSObject {
    type Key: INSObject;
    type Value: INSObject;
    type Own: Ownership;

    fn count(&self) -> usize {
        unsafe {
            msg_send![self, count]
        }
    }

    fn object_for(&self, key: &Self::Key) -> Option<&Self::Value> {
        unsafe {
            let obj: *mut Self::Value = msg_send![self, objectForKey:key];
            if obj.is_null() { None } else { Some(&*obj) }
        }
    }

    fn keys(&self) -> Vec<&Self::Key> {
        let len = self.count();
        let mut keys = Vec::with_capacity(len);
        unsafe {
            let _: () = msg_send![self, getObjects:ptr::null_mut::<Self::Value>()
                                           andKeys:keys.as_mut_ptr()];
            keys.set_len(len);
        }
        keys
    }

    fn values(&self) -> Vec<&Self::Value> {
        let len = self.count();
        let mut vals = Vec::with_capacity(len);
        unsafe {
            let _: () = msg_send![self, getObjects:vals.as_mut_ptr()
                                           andKeys:ptr::null_mut::<Self::Key>()];
            vals.set_len(len);
        }
        vals
    }

    fn keys_and_objects(&self) -> (Vec<&Self::Key>, Vec<&Self::Value>) {
        let len = self.count();
        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()];
            keys.set_len(len);
            objs.set_len(len);
        }
        (keys, objs)
    }

    fn key_enumerator(&self) -> NSEnumerator<Self::Key> {
        unsafe {
            let result = msg_send![self, keyEnumerator];
            NSEnumerator::from_ptr(result)
        }
    }

    fn object_enumerator(&self) -> NSEnumerator<Self::Value> {
        unsafe {
            let result = msg_send![self, objectEnumerator];
            NSEnumerator::from_ptr(result)
        }
    }

    fn keys_array(&self) -> Id<NSSharedArray<Self::Key>> {
        unsafe {
            let keys: *mut NSSharedArray<Self::Key> = msg_send![self, allKeys];
            Id::from_ptr(keys)
        }
    }

    fn from_keys_and_objects<T>(keys: &[&T],
            vals: Vec<Id<Self::Value, Self::Own>>) -> Id<Self>
            where T: INSCopying<Output=Self::Key> {
        let vals_refs: Vec<&Self::Value> = vals.iter().map(|obj| &**obj).collect();
        unsafe {
            from_refs(keys, &vals_refs)
        }
    }

    fn into_values_array(dict: Id<Self>) -> Id<NSArray<Self::Value, Self::Own>> {
        unsafe {
            let vals = msg_send![dict, allValues];
            Id::from_ptr(vals)
        }
    }
}

pub struct NSDictionary<K, V> {
    key: PhantomData<ShareId<K>>,
    obj: PhantomData<Id<V>>,
}

object_impl!(NSDictionary<K, V>);

impl<K, V> INSObject for NSDictionary<K, V> where K: INSObject, V: INSObject {
    fn class() -> &'static Class {
        Class::get("NSDictionary").unwrap()
    }
}

impl<K, V> INSDictionary for NSDictionary<K, V>
        where K: INSObject, V: INSObject {
    type Key = K;
    type Value = V;
    type Own = Owned;
}

impl<K, V> INSFastEnumeration for NSDictionary<K, V>
        where K: INSObject, V: INSObject {
    type Item = K;
}

impl<'a, K, V> Index<&'a K> for NSDictionary<K, V> where K: INSObject, V: INSObject {
    type Output = V;

    fn index(&self, index: &K) -> &V {
        self.object_for(index).unwrap()
    }
}

#[cfg(test)]
mod tests {
    use objc_id::Id;
    use {INSArray, INSObject, INSString, NSObject, NSString};
    use super::{INSDictionary, NSDictionary};

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

    #[test]
    fn test_count() {
        let dict = sample_dict("abcd");
        assert!(dict.count() == 1);
    }

    #[test]
    fn test_object_for() {
        let dict = sample_dict("abcd");

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

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

    #[test]
    fn test_keys() {
        let dict = sample_dict("abcd");
        let keys = dict.keys();

        assert!(keys.len() == 1);
        assert!(keys[0].as_str() == "abcd");
    }

    #[test]
    fn test_values() {
        let dict = sample_dict("abcd");
        let vals = dict.values();

        assert!(vals.len() == 1);
    }

    #[test]
    fn test_keys_and_objects() {
        let dict = sample_dict("abcd");
        let (keys, objs) = dict.keys_and_objects();

        assert!(keys.len() == 1);
        assert!(objs.len() == 1);
        assert!(keys[0].as_str() == "abcd");
        assert!(objs[0] == dict.object_for(keys[0]).unwrap());
    }

    #[test]
    fn test_key_enumerator() {
        let dict = sample_dict("abcd");
        assert!(dict.key_enumerator().count() == 1);
        assert!(dict.key_enumerator().next().unwrap().as_str() == "abcd");
    }

    #[test]
    fn test_object_enumerator() {
        let dict = sample_dict("abcd");
        assert!(dict.object_enumerator().count() == 1);
    }

    #[test]
    fn test_arrays() {
        let dict = sample_dict("abcd");

        let keys = dict.keys_array();
        assert!(keys.count() == 1);
        assert!(keys.object_at(0).as_str() == "abcd");

        let objs = INSDictionary::into_values_array(dict);
        assert!(objs.count() == 1);
    }
}