Skip to content

Commit e88ebce

Browse files
committed
Add simplifying constructors to SCPreferences
1 parent addb8b0 commit e88ebce

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

system-configuration/src/preferences.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
//! [`SCPreferences`]: https://developer.apple.com/documentation/systemconfiguration/scpreferences
1414
1515

16-
use core_foundation::base::CFAllocatorRef;
17-
use core_foundation::base::TCFType;
16+
use core_foundation::base::{CFAllocatorRef, TCFType};
1817
use core_foundation::string::CFString;
1918

2019
pub use system_configuration_sys::preferences::*;
@@ -31,18 +30,37 @@ impl_TCFType!(SCPreferences, SCPreferencesRef, SCPreferencesGetTypeID);
3130

3231

3332
impl SCPreferences {
34-
/// Initiates access to the per-system set of configuration preferences.
35-
pub fn new(allocator: CFAllocatorRef, name: &str, prefs_id: Option<&str>) -> Self {
36-
let prefs_id = match prefs_id {
37-
Some(prefs_id) => CFString::new(prefs_id).as_concrete_TypeRef(),
33+
/// Initiates access to the default system preferences using the default allocator.
34+
pub fn default(calling_process_name: &CFString) -> Self {
35+
Self::with_allocator(ptr::null(), calling_process_name, None)
36+
}
37+
38+
/// Initiates access to the given (`prefs_id`) group of configuration preferences using the
39+
/// default allocator. To access the default system preferences, use the [`default`]
40+
/// constructor.
41+
///
42+
/// [`default`]: #method.default
43+
pub fn group(calling_process_name: &CFString, prefs_id: &CFString) -> Self {
44+
Self::with_allocator(ptr::null(), calling_process_name, Some(prefs_id))
45+
}
46+
47+
/// Initiates access to the per-system set of configuration preferences with a given
48+
/// allocator and preference group to access.
49+
pub fn with_allocator(
50+
allocator: CFAllocatorRef,
51+
calling_process_name: &CFString,
52+
prefs_id: Option<&CFString>,
53+
) -> Self {
54+
let prefs_id_ptr = match prefs_id {
55+
Some(prefs_id) => prefs_id.as_concrete_TypeRef(),
3856
None => ptr::null(),
3957
};
4058

4159
unsafe {
4260
SCPreferences::wrap_under_get_rule(SCPreferencesCreate(
4361
allocator,
44-
CFString::new(name).as_concrete_TypeRef(),
45-
prefs_id,
62+
calling_process_name.as_concrete_TypeRef(),
63+
prefs_id_ptr,
4664
))
4765
}
4866
}

0 commit comments

Comments
 (0)