summaryrefslogtreecommitdiffstats
path: root/vendor/core-foundation/src/dictionary.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/core-foundation/src/dictionary.rs')
-rw-r--r--vendor/core-foundation/src/dictionary.rs204
1 files changed, 138 insertions, 66 deletions
diff --git a/vendor/core-foundation/src/dictionary.rs b/vendor/core-foundation/src/dictionary.rs
index efcbba117..aad37f3c8 100644
--- a/vendor/core-foundation/src/dictionary.rs
+++ b/vendor/core-foundation/src/dictionary.rs
@@ -11,18 +11,22 @@
pub use core_foundation_sys::dictionary::*;
-use core_foundation_sys::base::{CFTypeRef, CFRelease, kCFAllocatorDefault};
+use core_foundation_sys::base::{kCFAllocatorDefault, CFRelease, CFTypeRef};
+use std::marker::PhantomData;
use std::mem;
use std::os::raw::c_void;
use std::ptr;
-use std::marker::PhantomData;
-use base::{ItemRef, FromVoid, ToVoid};
-use base::{CFIndexConvertible, TCFType};
-use ConcreteCFType;
+use crate::base::{CFIndexConvertible, TCFType};
+use crate::base::{FromVoid, ItemRef, ToVoid};
+use crate::ConcreteCFType;
// consume the type parameters with PhantomDatas
-pub struct CFDictionary<K = *const c_void, V = *const c_void>(CFDictionaryRef, PhantomData<K>, PhantomData<V>);
+pub struct CFDictionary<K = *const c_void, V = *const c_void>(
+ CFDictionaryRef,
+ PhantomData<K>,
+ PhantomData<V>,
+);
impl<K, V> Drop for CFDictionary<K, V> {
fn drop(&mut self) {
@@ -36,19 +40,25 @@ impl_CFTypeDescription!(CFDictionary<K, V>);
unsafe impl ConcreteCFType for CFDictionary<*const c_void, *const c_void> {}
impl<K, V> CFDictionary<K, V> {
- pub fn from_CFType_pairs(pairs: &[(K, V)]) -> CFDictionary<K, V> where K: TCFType, V: TCFType {
+ pub fn from_CFType_pairs(pairs: &[(K, V)]) -> CFDictionary<K, V>
+ where
+ K: TCFType,
+ V: TCFType,
+ {
let (keys, values): (Vec<CFTypeRef>, Vec<CFTypeRef>) = pairs
.iter()
- .map(|&(ref key, ref value)| (key.as_CFTypeRef(), value.as_CFTypeRef()))
+ .map(|(key, value)| (key.as_CFTypeRef(), value.as_CFTypeRef()))
.unzip();
unsafe {
- let dictionary_ref = CFDictionaryCreate(kCFAllocatorDefault,
- keys.as_ptr(),
- values.as_ptr(),
- keys.len().to_CFIndex(),
- &kCFTypeDictionaryKeyCallBacks,
- &kCFTypeDictionaryValueCallBacks);
+ let dictionary_ref = CFDictionaryCreate(
+ kCFAllocatorDefault,
+ keys.as_ptr(),
+ values.as_ptr(),
+ keys.len().to_CFIndex(),
+ &kCFTypeDictionaryKeyCallBacks,
+ &kCFTypeDictionaryValueCallBacks,
+ );
TCFType::wrap_under_create_rule(dictionary_ref)
}
}
@@ -76,9 +86,7 @@ impl<K, V> CFDictionary<K, V> {
#[inline]
pub fn len(&self) -> usize {
- unsafe {
- CFDictionaryGetCount(self.0) as usize
- }
+ unsafe { CFDictionaryGetCount(self.0) as usize }
}
#[inline]
@@ -87,12 +95,19 @@ impl<K, V> CFDictionary<K, V> {
}
#[inline]
- pub fn contains_key(&self, key: &K) -> bool where K: ToVoid<K> {
+ pub fn contains_key(&self, key: &K) -> bool
+ where
+ K: ToVoid<K>,
+ {
unsafe { CFDictionaryContainsKey(self.0, key.to_void()) != 0 }
}
#[inline]
- pub fn find<'a, T: ToVoid<K>>(&'a self, key: T) -> Option<ItemRef<'a, V>> where V: FromVoid, K: ToVoid<K> {
+ pub fn find<T: ToVoid<K>>(&self, key: T) -> Option<ItemRef<'_, V>>
+ where
+ V: FromVoid,
+ K: ToVoid<K>,
+ {
unsafe {
let mut value: *const c_void = ptr::null();
if CFDictionaryGetValueIfPresent(self.0, key.to_void(), &mut value) != 0 {
@@ -108,9 +123,14 @@ impl<K, V> CFDictionary<K, V> {
/// Panics if the key is not present in the dictionary. Use `find` to get an `Option` instead
/// of panicking.
#[inline]
- pub fn get<'a, T: ToVoid<K>>(&'a self, key: T) -> ItemRef<'a, V> where V: FromVoid, K: ToVoid<K> {
+ pub fn get<T: ToVoid<K>>(&self, key: T) -> ItemRef<'_, V>
+ where
+ V: FromVoid,
+ K: ToVoid<K>,
+ {
let ptr = key.to_void();
- self.find(key).unwrap_or_else(|| panic!("No entry found for key {:p}", ptr))
+ self.find(key)
+ .unwrap_or_else(|| panic!("No entry found for key {:p}", ptr))
}
pub fn get_keys_and_values(&self) -> (Vec<*const c_void>, Vec<*const c_void>) {
@@ -129,7 +149,11 @@ impl<K, V> CFDictionary<K, V> {
}
// consume the type parameters with PhantomDatas
-pub struct CFMutableDictionary<K = *const c_void, V = *const c_void>(CFMutableDictionaryRef, PhantomData<K>, PhantomData<V>);
+pub struct CFMutableDictionary<K = *const c_void, V = *const c_void>(
+ CFMutableDictionaryRef,
+ PhantomData<K>,
+ PhantomData<V>,
+);
impl<K, V> Drop for CFMutableDictionary<K, V> {
fn drop(&mut self) {
@@ -147,24 +171,31 @@ impl<K, V> CFMutableDictionary<K, V> {
pub fn with_capacity(capacity: isize) -> Self {
unsafe {
- let dictionary_ref = CFDictionaryCreateMutable(kCFAllocatorDefault,
- capacity as _,
- &kCFTypeDictionaryKeyCallBacks,
- &kCFTypeDictionaryValueCallBacks);
+ let dictionary_ref = CFDictionaryCreateMutable(
+ kCFAllocatorDefault,
+ capacity as _,
+ &kCFTypeDictionaryKeyCallBacks,
+ &kCFTypeDictionaryValueCallBacks,
+ );
TCFType::wrap_under_create_rule(dictionary_ref)
}
}
pub fn copy_with_capacity(&self, capacity: isize) -> Self {
unsafe {
- let dictionary_ref = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, capacity as _, self.0);
+ let dictionary_ref =
+ CFDictionaryCreateMutableCopy(kCFAllocatorDefault, capacity as _, self.0);
TCFType::wrap_under_get_rule(dictionary_ref)
}
}
- pub fn from_CFType_pairs(pairs: &[(K, V)]) -> CFMutableDictionary<K, V> where K: ToVoid<K>, V: ToVoid<V> {
+ pub fn from_CFType_pairs(pairs: &[(K, V)]) -> CFMutableDictionary<K, V>
+ where
+ K: ToVoid<K>,
+ V: ToVoid<V>,
+ {
let mut result = Self::with_capacity(pairs.len() as _);
- for &(ref key, ref value) in pairs {
+ for (key, value) in pairs {
result.add(key, value);
}
result
@@ -194,9 +225,7 @@ impl<K, V> CFMutableDictionary<K, V> {
#[inline]
pub fn len(&self) -> usize {
- unsafe {
- CFDictionaryGetCount(self.0) as usize
- }
+ unsafe { CFDictionaryGetCount(self.0) as usize }
}
#[inline]
@@ -206,13 +235,15 @@ impl<K, V> CFMutableDictionary<K, V> {
#[inline]
pub fn contains_key(&self, key: *const c_void) -> bool {
- unsafe {
- CFDictionaryContainsKey(self.0, key) != 0
- }
+ unsafe { CFDictionaryContainsKey(self.0, key) != 0 }
}
#[inline]
- pub fn find<'a>(&'a self, key: &K) -> Option<ItemRef<'a, V>> where V: FromVoid, K: ToVoid<K> {
+ pub fn find<'a>(&'a self, key: &K) -> Option<ItemRef<'a, V>>
+ where
+ V: FromVoid,
+ K: ToVoid<K>,
+ {
unsafe {
let mut value: *const c_void = ptr::null();
if CFDictionaryGetValueIfPresent(self.0, key.to_void(), &mut value) != 0 {
@@ -228,9 +259,14 @@ impl<K, V> CFMutableDictionary<K, V> {
/// Panics if the key is not present in the dictionary. Use `find` to get an `Option` instead
/// of panicking.
#[inline]
- pub fn get<'a>(&'a self, key: &K) -> ItemRef<'a, V> where V: FromVoid, K: ToVoid<K> {
+ pub fn get<'a>(&'a self, key: &K) -> ItemRef<'a, V>
+ where
+ V: FromVoid,
+ K: ToVoid<K>,
+ {
let ptr = key.to_void();
- self.find(&key).unwrap_or_else(|| panic!("No entry found for key {:p}", ptr))
+ self.find(key)
+ .unwrap_or_else(|| panic!("No entry found for key {:p}", ptr))
}
pub fn get_keys_and_values(&self) -> (Vec<*const c_void>, Vec<*const c_void>) {
@@ -251,25 +287,40 @@ impl<K, V> CFMutableDictionary<K, V> {
/// Adds the key-value pair to the dictionary if no such key already exist.
#[inline]
- pub fn add(&mut self, key: &K, value: &V) where K: ToVoid<K>, V: ToVoid<V> {
+ pub fn add(&mut self, key: &K, value: &V)
+ where
+ K: ToVoid<K>,
+ V: ToVoid<V>,
+ {
unsafe { CFDictionaryAddValue(self.0, key.to_void(), value.to_void()) }
}
/// Sets the value of the key in the dictionary.
#[inline]
- pub fn set(&mut self, key: K, value: V) where K: ToVoid<K>, V: ToVoid<V> {
+ pub fn set(&mut self, key: K, value: V)
+ where
+ K: ToVoid<K>,
+ V: ToVoid<V>,
+ {
unsafe { CFDictionarySetValue(self.0, key.to_void(), value.to_void()) }
}
/// Replaces the value of the key in the dictionary.
#[inline]
- pub fn replace(&mut self, key: K, value: V) where K: ToVoid<K>, V: ToVoid<V> {
+ pub fn replace(&mut self, key: K, value: V)
+ where
+ K: ToVoid<K>,
+ V: ToVoid<V>,
+ {
unsafe { CFDictionaryReplaceValue(self.0, key.to_void(), value.to_void()) }
}
/// Removes the value of the key from the dictionary.
#[inline]
- pub fn remove(&mut self, key: K) where K: ToVoid<K> {
+ pub fn remove(&mut self, key: K)
+ where
+ K: ToVoid<K>,
+ {
unsafe { CFDictionaryRemoveValue(self.0, key.to_void()) }
}
@@ -296,15 +347,13 @@ impl<'a, K, V> From<&'a CFDictionary<K, V>> for CFMutableDictionary<K, V> {
}
}
-
#[cfg(test)]
pub mod test {
use super::*;
- use base::{CFType, TCFType};
- use boolean::CFBoolean;
- use number::CFNumber;
- use string::CFString;
-
+ use crate::base::{CFType, TCFType};
+ use crate::boolean::CFBoolean;
+ use crate::number::CFNumber;
+ use crate::string::CFString;
#[test]
fn dictionary() {
@@ -322,8 +371,14 @@ pub mod test {
]);
let (v1, v2) = d.get_keys_and_values();
- assert_eq!(v1, &[bar.as_CFTypeRef(), baz.as_CFTypeRef(), foo.as_CFTypeRef()]);
- assert_eq!(v2, &[boo.as_CFTypeRef(), tru.as_CFTypeRef(), n42.as_CFTypeRef()]);
+ assert_eq!(
+ v1,
+ &[bar.as_CFTypeRef(), baz.as_CFTypeRef(), foo.as_CFTypeRef()]
+ );
+ assert_eq!(
+ v2,
+ &[boo.as_CFTypeRef(), tru.as_CFTypeRef(), n42.as_CFTypeRef()]
+ );
}
#[test]
@@ -342,8 +397,14 @@ pub mod test {
assert_eq!(d.len(), 3);
let (v1, v2) = d.get_keys_and_values();
- assert_eq!(v1, &[bar.as_CFTypeRef(), baz.as_CFTypeRef(), foo.as_CFTypeRef()]);
- assert_eq!(v2, &[boo.as_CFTypeRef(), tru.as_CFTypeRef(), n42.as_CFTypeRef()]);
+ assert_eq!(
+ v1,
+ &[bar.as_CFTypeRef(), baz.as_CFTypeRef(), foo.as_CFTypeRef()]
+ );
+ assert_eq!(
+ v2,
+ &[boo.as_CFTypeRef(), tru.as_CFTypeRef(), n42.as_CFTypeRef()]
+ );
d.remove(baz);
assert_eq!(d.len(), 2);
@@ -358,12 +419,10 @@ pub mod test {
#[test]
fn dict_find_and_contains_key() {
- let dict = CFDictionary::from_CFType_pairs(&[
- (
- CFString::from_static_string("hello"),
- CFBoolean::true_value(),
- ),
- ]);
+ let dict = CFDictionary::from_CFType_pairs(&[(
+ CFString::from_static_string("hello"),
+ CFBoolean::true_value(),
+ )]);
let key = CFString::from_static_string("hello");
let invalid_key = CFString::from_static_string("foobar");
@@ -377,17 +436,24 @@ pub mod test {
#[test]
fn convert_immutable_to_mutable_dict() {
- let dict: CFDictionary<CFString, CFBoolean> = CFDictionary::from_CFType_pairs(&[
- (CFString::from_static_string("Foo"), CFBoolean::true_value()),
- ]);
+ let dict: CFDictionary<CFString, CFBoolean> = CFDictionary::from_CFType_pairs(&[(
+ CFString::from_static_string("Foo"),
+ CFBoolean::true_value(),
+ )]);
let mut mut_dict = CFMutableDictionary::from(&dict);
assert_eq!(dict.retain_count(), 1);
assert_eq!(mut_dict.retain_count(), 1);
assert_eq!(mut_dict.len(), 1);
- assert_eq!(*mut_dict.get(&CFString::from_static_string("Foo")), CFBoolean::true_value());
-
- mut_dict.add(&CFString::from_static_string("Bar"), &CFBoolean::false_value());
+ assert_eq!(
+ *mut_dict.get(&CFString::from_static_string("Foo")),
+ CFBoolean::true_value()
+ );
+
+ mut_dict.add(
+ &CFString::from_static_string("Bar"),
+ &CFBoolean::false_value(),
+ );
assert_eq!(dict.len(), 1);
assert_eq!(mut_dict.len(), 2);
}
@@ -395,13 +461,19 @@ pub mod test {
#[test]
fn mutable_dictionary_as_immutable() {
let mut mut_dict: CFMutableDictionary<CFString, CFBoolean> = CFMutableDictionary::new();
- mut_dict.add(&CFString::from_static_string("Bar"), &CFBoolean::false_value());
+ mut_dict.add(
+ &CFString::from_static_string("Bar"),
+ &CFBoolean::false_value(),
+ );
assert_eq!(mut_dict.retain_count(), 1);
let dict = mut_dict.to_immutable();
assert_eq!(mut_dict.retain_count(), 2);
assert_eq!(dict.retain_count(), 2);
- assert_eq!(*dict.get(&CFString::from_static_string("Bar")), CFBoolean::false_value());
+ assert_eq!(
+ *dict.get(&CFString::from_static_string("Bar")),
+ CFBoolean::false_value()
+ );
mem::drop(dict);
assert_eq!(mut_dict.retain_count(), 1);