Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use once_cell #61

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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.49] - 2022-09-22
### Changed
- `once_cell` dependency is not needed ([#61](https://github.com/strawlab/iana-time-zone/pull/58))

## [0.1.48] - 2022-09-12
### Changed
- Downgrade requirements for WASM dependencies ([#58](https://github.com/strawlab/iana-time-zone/pull/58))
Expand Down Expand Up @@ -199,6 +203,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Implement for Linux, Windows, MacOS

[0.1.49]: https://github.com/strawlab/iana-time-zone/releases/tag/v0.1.49
[0.1.48]: https://github.com/strawlab/iana-time-zone/releases/tag/v0.1.48
[0.1.47]: https://github.com/strawlab/iana-time-zone/releases/tag/v0.1.47
[0.1.46]: https://github.com/strawlab/iana-time-zone/releases/tag/v0.1.46
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 17 additions & 6 deletions src/tz_android.rs
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") };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CStr::from_bytes_with_nul_unchecked is only usable in const contexts since Rust 1.59.0, so this PR actually raises the MSRV from where once_cell would have bumped it to.

https://doc.rust-lang.org/nightly/std/ffi/struct.CStr.html#method.from_bytes_with_nul_unchecked

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, that would be in line with our MSRV policy not to care about tier 2 targets. :) The MSRV for tier 1 targets was still reduced.

But yes, rising it for Android was an accident. Do you want to open a PR to fix that?

Copy link
Collaborator

@lopopolo lopopolo Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather open a PR to revert this change and have 0.1.49 yanked on crates.io


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)
}