diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:42 +0000 |
commit | 837b550238aa671a591ccf282dddeab29cadb206 (patch) | |
tree | 914b6b8862bace72bd3245ca184d374b08d8a672 /vendor/time/src/util.rs | |
parent | Adding debian version 1.70.0+dfsg2-1. (diff) | |
download | rustc-837b550238aa671a591ccf282dddeab29cadb206.tar.xz rustc-837b550238aa671a591ccf282dddeab29cadb206.zip |
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/time/src/util.rs')
-rw-r--r-- | vendor/time/src/util.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/vendor/time/src/util.rs b/vendor/time/src/util.rs index 11330501c..b41068384 100644 --- a/vendor/time/src/util.rs +++ b/vendor/time/src/util.rs @@ -29,3 +29,61 @@ pub const fn days_in_year_month(year: i32, month: Month) -> u8 { February => 28, } } + +#[cfg(feature = "local-offset")] +/// Utility functions relating to the local UTC offset. +pub mod local_offset { + use core::sync::atomic::{AtomicBool, Ordering}; + + /// Whether obtaining the local UTC offset is required to be sound. + static LOCAL_OFFSET_IS_SOUND: AtomicBool = AtomicBool::new(true); + + /// The soundness of obtaining the local UTC offset. + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + pub enum Soundness { + /// Obtaining the local UTC offset is required to be sound. Undefined behavior will never + /// occur. This is the default. + Sound, + /// Obtaining the local UTC offset is allowed to invoke undefined behavior. Setting this + /// value is strongly discouraged. To do so, you must comply with the safety requirements + /// of [`time::local_offset::set_soundness`](set_soundness). + Unsound, + } + + /// Set whether obtaining the local UTC offset is allowed to invoke undefined behavior. + /// + /// # Safety + /// + /// If this method is called with [`Soundness::Sound`], the call is always sound. If this method + /// is called with [`Soundness::Unsound`], the following conditions apply. + /// + /// - If the operating system is not Unix-like, the call is sound. + /// - If the process is single-threaded, the call is sound. + /// - If the process is multi-threaded, no other thread may mutate the environment in any way at + /// the same time a call to a method that obtains the local UTC offset. This includes adding, + /// removing, or modifying an environment variable. + /// + /// Note that you must not only verify this safety condition for your code, but for **all** code + /// that will be included in the final binary. Notably, it applies to both direct and transitive + /// dependencies and to both Rust and non-Rust code. For this reason it is not possible to + /// soundly pass [`Soundness::Unsound`] to this method if you are writing a library that may + /// used by others. + /// + /// The following methods currently obtain the local UTC offset: + /// + /// - [`OffsetDateTime::now_local`](crate::OffsetDateTime::now_local) + /// - [`UtcOffset::local_offset_at`](crate::UtcOffset::local_offset_at) + /// - [`UtcOffset::current_local_offset`](crate::UtcOffset::current_local_offset) + pub unsafe fn set_soundness(soundness: Soundness) { + LOCAL_OFFSET_IS_SOUND.store(soundness == Soundness::Sound, Ordering::SeqCst); + } + + /// Obtains the soundness of obtaining the local UTC offset. If it is [`Soundness::Unsound`], + /// it is allowed to invoke undefined behavior when obtaining the local UTC offset. + pub fn get_soundness() -> Soundness { + match LOCAL_OFFSET_IS_SOUND.load(Ordering::SeqCst) { + false => Soundness::Unsound, + true => Soundness::Sound, + } + } +} |