Skip to content

Commit

Permalink
Merge pull request #50 from Kijewski/pr-core-foundation-sys
Browse files Browse the repository at this point in the history
Use core-foundation-sys instead of core-foundation
  • Loading branch information
astraw committed Aug 11, 2022
2 parents 175791e + bf6177d commit aa7f46f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -18,7 +18,7 @@ fallback = []
android_system_properties = "0.1.4"

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
core-foundation = "0.9"
core-foundation-sys = "0.8.3"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3.9", features = ["activation", "combaseapi", "objbase", "roapi", "winerror", "winstring"] }
Expand Down
42 changes: 39 additions & 3 deletions src/tz_macos.rs
@@ -1,5 +1,41 @@
use core_foundation_sys::base::{CFRelease, CFTypeRef};
use core_foundation_sys::string::{kCFStringEncodingUTF8, CFStringGetCStringPtr};
use core_foundation_sys::timezone::{CFTimeZoneCopySystem, CFTimeZoneGetName};

pub(crate) fn get_timezone_inner() -> Result<String, crate::GetTimezoneError> {
Ok(core_foundation::timezone::CFTimeZone::system()
.name()
.to_string())
unsafe {
Dropping::new(CFTimeZoneCopySystem())
.and_then(|tz| Dropping::new(CFTimeZoneGetName(tz.0)))
.and_then(|name| {
let name = CFStringGetCStringPtr(name.0, kCFStringEncodingUTF8);
if name.is_null() {
None
} else {
Some(name)
}
})
.and_then(|name| std::ffi::CStr::from_ptr(name).to_str().ok())
.map(|name| name.to_owned())
.ok_or(crate::GetTimezoneError::OsError)
}
}

struct Dropping<T>(*const T);

impl<T> Drop for Dropping<T> {
#[inline]
fn drop(&mut self) {
unsafe { CFRelease(self.0 as CFTypeRef) };
}
}

impl<T> Dropping<T> {
#[inline]
unsafe fn new(v: *const T) -> Option<Self> {
if v.is_null() {
None
} else {
Some(Self(v))
}
}
}

0 comments on commit aa7f46f

Please sign in to comment.