forked from strawlab/iana-time-zone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`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. <matklad/once_cell#201>
- Loading branch information
Showing
2 changed files
with
17 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<AndroidSystemProperties> = 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<AndroidSystemProperties> = 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<String, crate::GetTimezoneError> { | ||
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) | ||
} |