From 0c296b55f5197840ad341576bab877b807d1bd42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Fri, 23 Sep 2022 13:09:44 +0200 Subject: [PATCH 1/2] Follow up to #61 * [`CStr::from_bytes_with_nul_unchecked()`] is only `const` since rustc 1.59.0. Just don't use it `const`. * Moving the `static mut` variable into a getter function makes it more obvious that it cannot be changed after the initial assignment. [`CStr::from_bytes_with_nul_unchecked()`]: https://doc.rust-lang.org/1.64.0/std/ffi/struct.CStr.html#method.from_bytes_with_nul_unchecked --- CHANGELOG.md | 6 +++++- Cargo.toml | 2 +- src/tz_android.rs | 26 ++++++++++++++------------ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1505611..64c86d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.1.50] - 2022-09-23 +### Fixed +- Reduce MSRV for Android again + ## [0.1.49] - 2022-09-22 ### Changed -- `once_cell` dependency is not needed ([#61](https://github.com/strawlab/iana-time-zone/pull/58)) +- `once_cell` dependency is not needed ([#61](https://github.com/strawlab/iana-time-zone/pull/61)) ## [0.1.48] - 2022-09-12 ### Changed diff --git a/Cargo.toml b/Cargo.toml index 48b48d9..1e0b8ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "iana-time-zone" description = "get the IANA time zone for the current system" -version = "0.1.49" +version = "0.1.50" authors = ["Andrew Straw "] repository = "https://github.com/strawlab/iana-time-zone" license = "MIT OR Apache-2.0" diff --git a/src/tz_android.rs b/src/tz_android.rs index 89fd669..ba5d593 100644 --- a/src/tz_android.rs +++ b/src/tz_android.rs @@ -3,15 +3,21 @@ use std::sync::Once; use android_system_properties::AndroidSystemProperties; -static INITALIZED: Once = Once::new(); -static mut PROPERTIES: Option = None; +pub(crate) fn get_timezone_inner() -> Result { + // 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. + // SAFETY: the key is NUL-terminated and there are no other NULs + let key = unsafe { CStr::from_bytes_with_nul_unchecked(b"persist.sys.timezone\0") }; -// 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. -// 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") }; + get_properties() + .and_then(|properties| properties.get_from_cstr(key)) + .ok_or(crate::GetTimezoneError::OsError) +} + +fn get_properties() -> Option<&'static AndroidSystemProperties> { + static INITALIZED: Once = Once::new(); + static mut PROPERTIES: Option = None; -pub(crate) fn get_timezone_inner() -> Result { INITALIZED.call_once(|| { let properties = AndroidSystemProperties::new(); // SAFETY: `INITALIZED` is synchronizing. The variable is only assigned to once. @@ -19,9 +25,5 @@ pub(crate) fn get_timezone_inner() -> Result { }); // 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) + unsafe { PROPERTIES.as_ref() } } From ecacfc09b4e7a96f097bf8aa6824903be2cea91e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Fri, 23 Sep 2022 13:17:25 +0200 Subject: [PATCH 2/2] Add issue link to changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64c86d7..c8cc031 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.1.50] - 2022-09-23 ### Fixed -- Reduce MSRV for Android again +- Reduce MSRV for Android again ([#62](https://github.com/strawlab/iana-time-zone/pull/62)) ## [0.1.49] - 2022-09-22 ### Changed