Skip to content

Commit ffdb0a2

Browse files
committed
Change CFDynamicStore to use CFPropertyList
1 parent 2347bba commit ffdb0a2

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

system-configuration/src/bin/set_dns.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ fn main() {
1818

1919
let ipv4_dict = store
2020
.get("State:/Network/Global/IPv4")
21-
.expect("Unable to find global settings");
21+
.expect("Unable to find global settings")
22+
.downcast::<_, CFDictionary>()
23+
.expect("Global IPv4 settings not a dictionary");
2224
println!("Got IPv4 global property list");
2325

2426
let pri_service_id_ptr = ipv4_dict

system-configuration/src/dynamic_store.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
use core_foundation::base::TCFType;
1010
use core_foundation::boolean::CFBoolean;
11-
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef};
11+
use core_foundation::dictionary::CFDictionary;
12+
use core_foundation::propertylist::{CFPropertyList, CFPropertyListSubClass};
1213
use core_foundation::string::CFString;
1314
use core_foundation_sys::base::{CFRelease, kCFAllocatorDefault};
14-
use core_foundation_sys::propertylist::CFPropertyListRef;
1515

1616
use system_configuration_sys::dynamic_store::*;
1717

@@ -70,15 +70,15 @@ impl SCDynamicStore {
7070
}
7171

7272
/// If the given key exists in the store, the associated value is returned.
73-
pub fn get<S: Into<CFString>>(&self, key: S) -> Option<CFDictionary> {
73+
///
74+
/// Use `CFPropertyList::downcast` to cast the result into the correct type.
75+
pub fn get<S: Into<CFString>>(&self, key: S) -> Option<CFPropertyList> {
7476
let cf_key = key.into();
7577
unsafe {
7678
let dict_ref =
7779
SCDynamicStoreCopyValue(self.as_concrete_TypeRef(), cf_key.as_concrete_TypeRef());
7880
if dict_ref != ptr::null() {
79-
Some(CFDictionary::wrap_under_create_rule(
80-
dict_ref as *const _ as CFDictionaryRef,
81-
))
81+
Some(CFPropertyList::wrap_under_create_rule(dict_ref))
8282
} else {
8383
None
8484
}
@@ -87,13 +87,23 @@ impl SCDynamicStore {
8787

8888
/// Sets the value of the given key. Overwrites existing values.
8989
/// Returns `true` on success, false on failure.
90-
pub fn set<S: Into<CFString>>(&self, key: S, value: &CFDictionary) -> bool {
90+
pub fn set<S: Into<CFString>, R, V: CFPropertyListSubClass<R>>(
91+
&self,
92+
key: S,
93+
value: &V,
94+
) -> bool {
95+
self.set_raw(key, &value.to_CFPropertyList())
96+
}
97+
98+
/// Sets the value of the given key. Overwrites existing values.
99+
/// Returns `true` on success, false on failure.
100+
pub fn set_raw<S: Into<CFString>>(&self, key: S, value: &CFPropertyList) -> bool {
91101
let cf_key = key.into();
92102
let success = unsafe {
93103
SCDynamicStoreSetValue(
94104
self.as_concrete_TypeRef(),
95105
cf_key.as_concrete_TypeRef(),
96-
value.as_concrete_TypeRef() as CFPropertyListRef,
106+
value.as_concrete_TypeRef(),
97107
)
98108
};
99109
success != 0

0 commit comments

Comments
 (0)