From a07639761413b9574e73a900e1b6990091e6689e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Wed, 22 Nov 2017 16:26:50 +0100 Subject: [PATCH 1/3] Add higher level methods to CFDictionary --- core-foundation/src/dictionary.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core-foundation/src/dictionary.rs b/core-foundation/src/dictionary.rs index 3e48802..65276a0 100644 --- a/core-foundation/src/dictionary.rs +++ b/core-foundation/src/dictionary.rs @@ -70,6 +70,13 @@ impl CFDictionary { } } + /// Similar to `contains_key` but acts on a higher level, automatically converting from any + /// `TCFType` to the raw pointer of its concrete TypeRef. + #[inline] + pub fn contains_key2>(&self, key: &K) -> bool { + self.contains_key(key.as_concrete_TypeRef() as *const c_void) + } + #[inline] pub fn find(&self, key: *const c_void) -> Option<*const c_void> { unsafe { @@ -82,6 +89,13 @@ impl CFDictionary { } } + /// Similar to `find` but acts on a higher level, automatically converting from any `TCFType` + /// to the raw pointer of its concrete TypeRef. + #[inline] + pub fn find2>(&self, key: &K) -> Option<*const c_void> { + self.find(key.as_concrete_TypeRef() as *const c_void) + } + #[inline] pub fn get(&self, key: *const c_void) -> *const c_void { let value = self.find(key); From 64d944780aaa4d2dd261fe679a98bcfd1917bcb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Wed, 22 Nov 2017 16:27:32 +0100 Subject: [PATCH 2/3] Simplify and document CFDictionary::get --- core-foundation/src/dictionary.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/core-foundation/src/dictionary.rs b/core-foundation/src/dictionary.rs index 65276a0..05486e9 100644 --- a/core-foundation/src/dictionary.rs +++ b/core-foundation/src/dictionary.rs @@ -96,13 +96,16 @@ impl CFDictionary { self.find(key.as_concrete_TypeRef() as *const c_void) } + /// # Panics + /// + /// Panics if the key is not present in the dictionary. Use `find` to get an `Option` instead + /// of panicking. #[inline] 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 {:p}", key); + match self.find(key) { + None => panic!("No entry found for key {:p}", key), + Some(value) => value } - value.unwrap() } /// A convenience function to retrieve `CFType` instances. From c445b6e9386a3d896aa9d485662f28a1865607a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Wed, 22 Nov 2017 20:21:25 +0100 Subject: [PATCH 3/3] Simplify CFDictionary::get with expect() --- core-foundation/src/dictionary.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/core-foundation/src/dictionary.rs b/core-foundation/src/dictionary.rs index 05486e9..822eb9c 100644 --- a/core-foundation/src/dictionary.rs +++ b/core-foundation/src/dictionary.rs @@ -102,10 +102,7 @@ impl CFDictionary { /// of panicking. #[inline] pub fn get(&self, key: *const c_void) -> *const c_void { - match self.find(key) { - None => panic!("No entry found for key {:p}", key), - Some(value) => value - } + self.find(key).expect(&format!("No entry found for key {:p}", key)) } /// A convenience function to retrieve `CFType` instances.