From d7e718f5f99493755d047576bdcee19839b66c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Thu, 22 Sep 2022 19:23:16 +0200 Subject: [PATCH] Don't use once_cell `once_cell` upgraded the MSRV to 1.56. This breaks the use of `iana-time-zone` transitively even though we only use it for Android targets. This PR replaces `once_cell` by using `static mut` + `std::sync::Once`. `once_cell` is more or less only a safe wrapper around both, but not actually needed. We already do the same in our Windows targets. Cf. --- Cargo.toml | 1 - src/tz_android.rs | 23 +++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 275a0e5..430628d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,6 @@ fallback = [] [target.'cfg(target_os = "android")'.dependencies] android_system_properties = "0.1.5" -once_cell = "1.13.1" [target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies] core-foundation-sys = "0.8.3" diff --git a/src/tz_android.rs b/src/tz_android.rs index 5eedefd..89fd669 100644 --- a/src/tz_android.rs +++ b/src/tz_android.rs @@ -1,16 +1,27 @@ use std::ffi::CStr; +use std::sync::Once; use android_system_properties::AndroidSystemProperties; -use once_cell::sync::OnceCell; + +static INITALIZED: Once = Once::new(); +static mut PROPERTIES: Option = None; // From https://android.googlesource.com/platform/ndk/+/android-4.2.2_r1.2/docs/system/libc/OVERVIEW.html // The system property named 'persist.sys.timezone' contains the name of the current timezone. - -static PROPERTIES: OnceCell = OnceCell::new(); +// SAFETY: the key is NUL-terminated and there are no other NULs +const KEY: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"persist.sys.timezone\0") }; pub(crate) fn get_timezone_inner() -> Result { - PROPERTIES - .get_or_init(AndroidSystemProperties::new) - .get_from_cstr(unsafe { CStr::from_bytes_with_nul_unchecked(b"persist.sys.timezone\0") }) + INITALIZED.call_once(|| { + let properties = AndroidSystemProperties::new(); + // SAFETY: `INITALIZED` is synchronizing. The variable is only assigned to once. + unsafe { PROPERTIES = Some(properties) }; + }); + + // SAFETY: `INITALIZED` is synchronizing. The variable is only assigned to once. + let properties = unsafe { PROPERTIES.as_ref() }; + + properties + .and_then(|properties| properties.get_from_cstr(KEY)) .ok_or(crate::GetTimezoneError::OsError) }