Skip to content

Commit f9e40ce

Browse files
committed
Auto merge of rust-lang#120852 - matthiaskrgr:rollup-01pr8gj, r=matthiaskrgr
Rollup of 11 pull requests Successful merges: - rust-lang#120351 (Implement SystemTime for UEFI) - rust-lang#120354 (improve normalization of `Pointee::Metadata`) - rust-lang#120776 (Move path implementations into `sys`) - rust-lang#120790 (better error message on download CI LLVM failure) - rust-lang#120806 (Clippy subtree update) - rust-lang#120815 (Improve `Option::inspect` docs) - rust-lang#120822 (Emit more specific diagnostics when enums fail to cast with `as`) - rust-lang#120827 (Print image input file and checksum in CI only) - rust-lang#120836 (hide impls if trait bound is proven from env) - rust-lang#120844 (Build DebugInfo for async closures) - rust-lang#120851 (Remove duplicate release note) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 04889f6 + 85a5f3a commit f9e40ce

File tree

26 files changed

+173
-59
lines changed

26 files changed

+173
-59
lines changed

core/src/option.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1073,18 +1073,23 @@ impl<T> Option<T> {
10731073
}
10741074
}
10751075

1076-
/// Calls the provided closure with a reference to the contained value (if [`Some`]).
1076+
/// Calls a function with a reference to the contained value if [`Some`].
1077+
///
1078+
/// Returns the original option.
10771079
///
10781080
/// # Examples
10791081
///
10801082
/// ```
1081-
/// let v = vec![1, 2, 3, 4, 5];
1083+
/// let list = vec![1, 2, 3];
10821084
///
1083-
/// // prints "got: 4"
1084-
/// let x: Option<&usize> = v.get(3).inspect(|x| println!("got: {x}"));
1085+
/// // prints "got: 2"
1086+
/// let x = list
1087+
/// .get(1)
1088+
/// .inspect(|x| println!("got: {x}"))
1089+
/// .expect("list should be long enough");
10851090
///
10861091
/// // prints nothing
1087-
/// let x: Option<&usize> = v.get(5).inspect(|x| println!("got: {x}"));
1092+
/// list.get(5).inspect(|x| println!("got: {x}"));
10881093
/// ```
10891094
#[inline]
10901095
#[stable(feature = "result_option_inspect", since = "1.76.0")]

core/src/result.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,9 @@ impl<T, E> Result<T, E> {
830830
}
831831
}
832832

833-
/// Calls the provided closure with a reference to the contained value (if [`Ok`]).
833+
/// Calls a function with a reference to the contained value if [`Ok`].
834+
///
835+
/// Returns the original result.
834836
///
835837
/// # Examples
836838
///
@@ -851,7 +853,9 @@ impl<T, E> Result<T, E> {
851853
self
852854
}
853855

854-
/// Calls the provided closure with a reference to the contained error (if [`Err`]).
856+
/// Calls a function with a reference to the contained value if [`Err`].
857+
///
858+
/// Returns the original result.
855859
///
856860
/// # Examples
857861
///

std/src/sys/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod personality;
77

88
pub mod cmath;
99
pub mod os_str;
10+
pub mod path;
1011

1112
// FIXME(117276): remove this, move feature implementations into individual
1213
// submodules.

std/src/sys/pal/hermit/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ pub mod io;
2828
pub mod memchr;
2929
pub mod net;
3030
pub mod os;
31-
#[path = "../unix/path.rs"]
32-
pub mod path;
3331
#[path = "../unsupported/pipe.rs"]
3432
pub mod pipe;
3533
#[path = "../unsupported/process.rs"]

std/src/sys/pal/sgx/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ pub mod io;
2222
pub mod memchr;
2323
pub mod net;
2424
pub mod os;
25-
pub mod path;
2625
#[path = "../unsupported/pipe.rs"]
2726
pub mod pipe;
2827
#[path = "../unsupported/process.rs"]

std/src/sys/pal/solid/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ pub mod fs;
2929
pub mod io;
3030
pub mod net;
3131
pub mod os;
32-
pub mod path;
3332
#[path = "../unsupported/pipe.rs"]
3433
pub mod pipe;
3534
#[path = "../unsupported/process.rs"]

std/src/sys/pal/teeos/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ pub mod net;
2525
#[path = "../unsupported/once.rs"]
2626
pub mod once;
2727
pub mod os;
28-
#[path = "../unix/path.rs"]
29-
pub mod path;
3028
#[path = "../unsupported/pipe.rs"]
3129
pub mod pipe;
3230
#[path = "../unsupported/process.rs"]

std/src/sys/pal/uefi/helpers.rs

+8
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,11 @@ pub(crate) fn image_handle_protocol<T>(protocol_guid: Guid) -> Option<NonNull<T>
146146
let system_handle = uefi::env::try_image_handle()?;
147147
open_protocol(system_handle, protocol_guid).ok()
148148
}
149+
150+
/// Get RuntimeServices
151+
pub(crate) fn runtime_services() -> Option<NonNull<r_efi::efi::RuntimeServices>> {
152+
let system_table: NonNull<r_efi::efi::SystemTable> =
153+
crate::os::uefi::env::try_system_table()?.cast();
154+
let runtime_services = unsafe { (*system_table.as_ptr()).runtime_services };
155+
NonNull::new(runtime_services)
156+
}

std/src/sys/pal/uefi/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub mod net;
2626
#[path = "../unsupported/once.rs"]
2727
pub mod once;
2828
pub mod os;
29-
pub mod path;
3029
#[path = "../unsupported/pipe.rs"]
3130
pub mod pipe;
3231
#[path = "../unsupported/process.rs"]
@@ -38,7 +37,6 @@ pub mod thread;
3837
pub mod thread_local_key;
3938
#[path = "../unsupported/thread_parking.rs"]
4039
pub mod thread_parking;
41-
#[path = "../unsupported/time.rs"]
4240
pub mod time;
4341

4442
mod helpers;

std/src/sys/pal/uefi/path.rs

-25
This file was deleted.

std/src/sys/pal/uefi/tests.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use super::alloc::*;
2+
use super::time::*;
3+
use crate::time::Duration;
24

35
#[test]
46
fn align() {
@@ -19,3 +21,21 @@ fn align() {
1921
}
2022
}
2123
}
24+
25+
#[test]
26+
fn epoch() {
27+
let t = r_efi::system::Time {
28+
year: 1970,
29+
month: 1,
30+
day: 1,
31+
hour: 0,
32+
minute: 0,
33+
second: 0,
34+
nanosecond: 0,
35+
timezone: r_efi::efi::UNSPECIFIED_TIMEZONE,
36+
daylight: 0,
37+
pad1: 0,
38+
pad2: 0,
39+
};
40+
assert_eq!(system_time_internal::uefi_time_to_duration(t), Duration::new(0, 0));
41+
}

std/src/sys/pal/uefi/time.rs

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
use crate::time::Duration;
2+
3+
const SECS_IN_MINUTE: u64 = 60;
4+
const SECS_IN_HOUR: u64 = SECS_IN_MINUTE * 60;
5+
const SECS_IN_DAY: u64 = SECS_IN_HOUR * 24;
6+
7+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
8+
pub struct Instant(Duration);
9+
10+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
11+
pub struct SystemTime(Duration);
12+
13+
pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0));
14+
15+
impl Instant {
16+
pub fn now() -> Instant {
17+
panic!("time not implemented on this platform")
18+
}
19+
20+
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
21+
self.0.checked_sub(other.0)
22+
}
23+
24+
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
25+
Some(Instant(self.0.checked_add(*other)?))
26+
}
27+
28+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
29+
Some(Instant(self.0.checked_sub(*other)?))
30+
}
31+
}
32+
33+
impl SystemTime {
34+
pub fn now() -> SystemTime {
35+
system_time_internal::now()
36+
.unwrap_or_else(|| panic!("time not implemented on this platform"))
37+
}
38+
39+
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
40+
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
41+
}
42+
43+
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
44+
Some(SystemTime(self.0.checked_add(*other)?))
45+
}
46+
47+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
48+
Some(SystemTime(self.0.checked_sub(*other)?))
49+
}
50+
}
51+
52+
pub(crate) mod system_time_internal {
53+
use super::super::helpers;
54+
use super::*;
55+
use crate::mem::MaybeUninit;
56+
use crate::ptr::NonNull;
57+
use r_efi::efi::{RuntimeServices, Time};
58+
59+
pub fn now() -> Option<SystemTime> {
60+
let runtime_services: NonNull<RuntimeServices> = helpers::runtime_services()?;
61+
let mut t: MaybeUninit<Time> = MaybeUninit::uninit();
62+
let r = unsafe {
63+
((*runtime_services.as_ptr()).get_time)(t.as_mut_ptr(), crate::ptr::null_mut())
64+
};
65+
66+
if r.is_error() {
67+
return None;
68+
}
69+
70+
let t = unsafe { t.assume_init() };
71+
72+
Some(SystemTime(uefi_time_to_duration(t)))
73+
}
74+
75+
// This algorithm is based on the one described in the post
76+
// https://blog.reverberate.org/2020/05/12/optimizing-date-algorithms.html
77+
pub const fn uefi_time_to_duration(t: r_efi::system::Time) -> Duration {
78+
assert!(t.month <= 12);
79+
assert!(t.month != 0);
80+
81+
const YEAR_BASE: u32 = 4800; /* Before min year, multiple of 400. */
82+
83+
// Calculate the number of days since 1/1/1970
84+
// Use 1 March as the start
85+
let (m_adj, overflow): (u32, bool) = (t.month as u32).overflowing_sub(3);
86+
let (carry, adjust): (u32, u32) = if overflow { (1, 12) } else { (0, 0) };
87+
let y_adj: u32 = (t.year as u32) + YEAR_BASE - carry;
88+
let month_days: u32 = (m_adj.wrapping_add(adjust) * 62719 + 769) / 2048;
89+
let leap_days: u32 = y_adj / 4 - y_adj / 100 + y_adj / 400;
90+
let days: u32 = y_adj * 365 + leap_days + month_days + (t.day as u32 - 1) - 2472632;
91+
92+
let localtime_epoch: u64 = (days as u64) * SECS_IN_DAY
93+
+ (t.second as u64)
94+
+ (t.minute as u64) * SECS_IN_MINUTE
95+
+ (t.hour as u64) * SECS_IN_HOUR;
96+
97+
let utc_epoch: u64 = if t.timezone == r_efi::efi::UNSPECIFIED_TIMEZONE {
98+
localtime_epoch
99+
} else {
100+
(localtime_epoch as i64 + (t.timezone as i64) * SECS_IN_MINUTE as i64) as u64
101+
};
102+
103+
Duration::new(utc_epoch, t.nanosecond)
104+
}
105+
}

std/src/sys/pal/unix/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub mod net;
2727
#[cfg(target_os = "l4re")]
2828
pub use self::l4re::net;
2929
pub mod os;
30-
pub mod path;
3130
pub mod pipe;
3231
pub mod process;
3332
pub mod rand;

std/src/sys/pal/unsupported/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ pub mod locks;
99
pub mod net;
1010
pub mod once;
1111
pub mod os;
12-
#[path = "../unix/path.rs"]
13-
pub mod path;
1412
pub mod pipe;
1513
pub mod process;
1614
pub mod stdio;

std/src/sys/pal/wasi/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ pub mod io;
3030

3131
pub mod net;
3232
pub mod os;
33-
#[path = "../unix/path.rs"]
34-
pub mod path;
3533
#[path = "../unsupported/pipe.rs"]
3634
pub mod pipe;
3735
#[path = "../unsupported/process.rs"]

std/src/sys/pal/wasm/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ pub mod io;
2828
pub mod net;
2929
#[path = "../unsupported/os.rs"]
3030
pub mod os;
31-
#[path = "../unix/path.rs"]
32-
pub mod path;
3331
#[path = "../unsupported/pipe.rs"]
3432
pub mod pipe;
3533
#[path = "../unsupported/process.rs"]

std/src/sys/pal/windows/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use crate::sys::{c, cvt, Align8};
1616
use crate::sys_common::{AsInner, FromInner, IntoInner};
1717
use crate::thread;
1818

19-
use super::path::maybe_verbatim;
2019
use super::{api, to_u16s, IoResult};
20+
use crate::sys::path::maybe_verbatim;
2121

2222
pub struct File {
2323
handle: Handle,

std/src/sys/pal/windows/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub mod locks;
2323
pub mod memchr;
2424
pub mod net;
2525
pub mod os;
26-
pub mod path;
2726
pub mod pipe;
2827
pub mod process;
2928
pub mod rand;
@@ -210,7 +209,7 @@ pub fn to_u16s<S: AsRef<OsStr>>(s: S) -> crate::io::Result<Vec<u16>> {
210209
// Once the syscall has completed (errors bail out early) the second closure is
211210
// yielded the data which has been read from the syscall. The return value
212211
// from this closure is then the return value of the function.
213-
fn fill_utf16_buf<F1, F2, T>(mut f1: F1, f2: F2) -> crate::io::Result<T>
212+
pub fn fill_utf16_buf<F1, F2, T>(mut f1: F1, f2: F2) -> crate::io::Result<T>
214213
where
215214
F1: FnMut(*mut u16, c::DWORD) -> c::DWORD,
216215
F2: FnOnce(&[u16]) -> T,
@@ -274,7 +273,7 @@ where
274273
}
275274
}
276275

277-
fn os2path(s: &[u16]) -> PathBuf {
276+
pub fn os2path(s: &[u16]) -> PathBuf {
278277
PathBuf::from(OsString::from_wide(s))
279278
}
280279

std/src/sys/pal/xous/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ pub mod io;
1212
pub mod locks;
1313
pub mod net;
1414
pub mod os;
15-
#[path = "../unix/path.rs"]
16-
pub mod path;
1715
#[path = "../unsupported/pipe.rs"]
1816
pub mod pipe;
1917
#[path = "../unsupported/process.rs"]

std/src/sys/pal/zkvm/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ pub mod net;
2424
#[path = "../unsupported/once.rs"]
2525
pub mod once;
2626
pub mod os;
27-
#[path = "../unix/os_str.rs"]
28-
pub mod os_str;
29-
#[path = "../unix/path.rs"]
30-
pub mod path;
3127
#[path = "../unsupported/pipe.rs"]
3228
pub mod pipe;
3329
#[path = "../unsupported/process.rs"]

std/src/sys/path/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cfg_if::cfg_if! {
2+
if #[cfg(target_os = "windows")] {
3+
mod windows;
4+
pub use windows::*;
5+
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
6+
mod sgx;
7+
pub use sgx::*;
8+
} else if #[cfg(any(
9+
target_os = "uefi",
10+
target_os = "solid_asp3",
11+
))] {
12+
mod unsupported_backslash;
13+
pub use unsupported_backslash::*;
14+
} else {
15+
mod unix;
16+
pub use unix::*;
17+
}
18+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)