Skip to content

Commit e69f14b

Browse files
committed
Auto merge of rust-lang#114038 - Stargateur:108277, r=ChrisDenton
unix time module now return result First try to fix rust-lang#108277 without break anything. if anyone who read this know tips to be able to check compilation for different target I could use some help. So far I installed many target with rustup but `./x check --all-targets` doesn't seem to use them. TODO: - [x] better error - [ ] test, how ? `@rustbot` label -S-waiting-on-author +S-waiting-on-review
2 parents cb580ff + 408c0ea commit e69f14b

File tree

2 files changed

+35
-51
lines changed

2 files changed

+35
-51
lines changed

library/std/src/sys/pal/unix/fs.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -463,15 +463,15 @@ impl FileAttr {
463463
#[cfg(target_os = "netbsd")]
464464
impl FileAttr {
465465
pub fn modified(&self) -> io::Result<SystemTime> {
466-
Ok(SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtimensec as i64))
466+
SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtimensec as i64)
467467
}
468468

469469
pub fn accessed(&self) -> io::Result<SystemTime> {
470-
Ok(SystemTime::new(self.stat.st_atime as i64, self.stat.st_atimensec as i64))
470+
SystemTime::new(self.stat.st_atime as i64, self.stat.st_atimensec as i64)
471471
}
472472

473473
pub fn created(&self) -> io::Result<SystemTime> {
474-
Ok(SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtimensec as i64))
474+
SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtimensec as i64)
475475
}
476476
}
477477

@@ -503,16 +503,16 @@ impl FileAttr {
503503
#[cfg(target_pointer_width = "32")]
504504
cfg_has_statx! {
505505
if let Some(mtime) = self.stx_mtime() {
506-
return Ok(SystemTime::new(mtime.tv_sec, mtime.tv_nsec as i64));
506+
return SystemTime::new(mtime.tv_sec, mtime.tv_nsec as i64);
507507
}
508508
}
509509

510-
Ok(SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtime_nsec as i64))
510+
SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtime_nsec as i64)
511511
}
512512

513513
#[cfg(any(target_os = "vxworks", target_os = "espidf", target_os = "vita"))]
514514
pub fn modified(&self) -> io::Result<SystemTime> {
515-
Ok(SystemTime::new(self.stat.st_mtime as i64, 0))
515+
SystemTime::new(self.stat.st_mtime as i64, 0)
516516
}
517517

518518
#[cfg(any(target_os = "horizon", target_os = "hurd"))]
@@ -531,16 +531,16 @@ impl FileAttr {
531531
#[cfg(target_pointer_width = "32")]
532532
cfg_has_statx! {
533533
if let Some(atime) = self.stx_atime() {
534-
return Ok(SystemTime::new(atime.tv_sec, atime.tv_nsec as i64));
534+
return SystemTime::new(atime.tv_sec, atime.tv_nsec as i64);
535535
}
536536
}
537537

538-
Ok(SystemTime::new(self.stat.st_atime as i64, self.stat.st_atime_nsec as i64))
538+
SystemTime::new(self.stat.st_atime as i64, self.stat.st_atime_nsec as i64)
539539
}
540540

541541
#[cfg(any(target_os = "vxworks", target_os = "espidf", target_os = "vita"))]
542542
pub fn accessed(&self) -> io::Result<SystemTime> {
543-
Ok(SystemTime::new(self.stat.st_atime as i64, 0))
543+
SystemTime::new(self.stat.st_atime as i64, 0)
544544
}
545545

546546
#[cfg(any(target_os = "horizon", target_os = "hurd"))]
@@ -557,7 +557,7 @@ impl FileAttr {
557557
target_os = "watchos",
558558
))]
559559
pub fn created(&self) -> io::Result<SystemTime> {
560-
Ok(SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtime_nsec as i64))
560+
SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtime_nsec as i64)
561561
}
562562

563563
#[cfg(not(any(
@@ -573,7 +573,7 @@ impl FileAttr {
573573
cfg_has_statx! {
574574
if let Some(ext) = &self.statx_extra_fields {
575575
return if (ext.stx_mask & libc::STATX_BTIME) != 0 {
576-
Ok(SystemTime::new(ext.stx_btime.tv_sec, ext.stx_btime.tv_nsec as i64))
576+
SystemTime::new(ext.stx_btime.tv_sec, ext.stx_btime.tv_nsec as i64)
577577
} else {
578578
Err(io::const_io_error!(
579579
io::ErrorKind::Unsupported,
@@ -592,22 +592,22 @@ impl FileAttr {
592592

593593
#[cfg(target_os = "vita")]
594594
pub fn created(&self) -> io::Result<SystemTime> {
595-
Ok(SystemTime::new(self.stat.st_ctime as i64, 0))
595+
SystemTime::new(self.stat.st_ctime as i64, 0)
596596
}
597597
}
598598

599599
#[cfg(target_os = "nto")]
600600
impl FileAttr {
601601
pub fn modified(&self) -> io::Result<SystemTime> {
602-
Ok(SystemTime::new(self.stat.st_mtim.tv_sec, self.stat.st_mtim.tv_nsec))
602+
SystemTime::new(self.stat.st_mtim.tv_sec, self.stat.st_mtim.tv_nsec)
603603
}
604604

605605
pub fn accessed(&self) -> io::Result<SystemTime> {
606-
Ok(SystemTime::new(self.stat.st_atim.tv_sec, self.stat.st_atim.tv_nsec))
606+
SystemTime::new(self.stat.st_atim.tv_sec, self.stat.st_atim.tv_nsec)
607607
}
608608

609609
pub fn created(&self) -> io::Result<SystemTime> {
610-
Ok(SystemTime::new(self.stat.st_ctim.tv_sec, self.stat.st_ctim.tv_nsec))
610+
SystemTime::new(self.stat.st_ctim.tv_sec, self.stat.st_ctim.tv_nsec)
611611
}
612612
}
613613

library/std/src/sys/pal/unix/time.rs

+20-36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::fmt;
21
use crate::time::Duration;
2+
use crate::{fmt, io};
33

44
const NSEC_PER_SEC: u64 = 1_000_000_000;
55
pub const UNIX_EPOCH: SystemTime = SystemTime { t: Timespec::zero() };
@@ -34,8 +34,8 @@ pub(crate) struct Timespec {
3434

3535
impl SystemTime {
3636
#[cfg_attr(any(target_os = "horizon", target_os = "hurd"), allow(unused))]
37-
pub fn new(tv_sec: i64, tv_nsec: i64) -> SystemTime {
38-
SystemTime { t: Timespec::new(tv_sec, tv_nsec) }
37+
pub fn new(tv_sec: i64, tv_nsec: i64) -> Result<SystemTime, io::Error> {
38+
Ok(SystemTime { t: Timespec::new(tv_sec, tv_nsec)? })
3939
}
4040

4141
pub fn now() -> SystemTime {
@@ -55,12 +55,6 @@ impl SystemTime {
5555
}
5656
}
5757

58-
impl From<libc::timespec> for SystemTime {
59-
fn from(t: libc::timespec) -> SystemTime {
60-
SystemTime { t: Timespec::from(t) }
61-
}
62-
}
63-
6458
impl fmt::Debug for SystemTime {
6559
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6660
f.debug_struct("SystemTime")
@@ -71,11 +65,15 @@ impl fmt::Debug for SystemTime {
7165
}
7266

7367
impl Timespec {
68+
const unsafe fn new_unchecked(tv_sec: i64, tv_nsec: i64) -> Timespec {
69+
Timespec { tv_sec, tv_nsec: unsafe { Nanoseconds(tv_nsec as u32) } }
70+
}
71+
7472
pub const fn zero() -> Timespec {
75-
Timespec::new(0, 0)
73+
unsafe { Self::new_unchecked(0, 0) }
7674
}
7775

78-
const fn new(tv_sec: i64, tv_nsec: i64) -> Timespec {
76+
const fn new(tv_sec: i64, tv_nsec: i64) -> Result<Timespec, io::Error> {
7977
// On Apple OS, dates before epoch are represented differently than on other
8078
// Unix platforms: e.g. 1/10th of a second before epoch is represented as `seconds=-1`
8179
// and `nanoseconds=100_000_000` on other platforms, but is `seconds=0` and
@@ -100,9 +98,11 @@ impl Timespec {
10098
} else {
10199
(tv_sec, tv_nsec)
102100
};
103-
assert!(tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC as i64);
104-
// SAFETY: The assert above checks tv_nsec is within the valid range
105-
Timespec { tv_sec, tv_nsec: unsafe { Nanoseconds(tv_nsec as u32) } }
101+
if tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC as i64 {
102+
Ok(unsafe { Self::new_unchecked(tv_sec, tv_nsec) })
103+
} else {
104+
Err(io::const_io_error!(io::ErrorKind::InvalidData, "Invalid timestamp"))
105+
}
106106
}
107107

108108
pub fn now(clock: libc::clockid_t) -> Timespec {
@@ -126,13 +126,15 @@ impl Timespec {
126126
if let Some(clock_gettime64) = __clock_gettime64.get() {
127127
let mut t = MaybeUninit::uninit();
128128
cvt(unsafe { clock_gettime64(clock, t.as_mut_ptr()) }).unwrap();
129-
return Timespec::from(unsafe { t.assume_init() });
129+
let t = unsafe { t.assume_init() };
130+
return Timespec::new(t.tv_sec as i64, t.tv_nsec as i64).unwrap();
130131
}
131132
}
132133

133134
let mut t = MaybeUninit::uninit();
134135
cvt(unsafe { libc::clock_gettime(clock, t.as_mut_ptr()) }).unwrap();
135-
Timespec::from(unsafe { t.assume_init() })
136+
let t = unsafe { t.assume_init() };
137+
Timespec::new(t.tv_sec as i64, t.tv_nsec as i64).unwrap()
136138
}
137139

138140
pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
@@ -178,7 +180,7 @@ impl Timespec {
178180
nsec -= NSEC_PER_SEC as u32;
179181
secs = secs.checked_add(1)?;
180182
}
181-
Some(Timespec::new(secs, nsec.into()))
183+
Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
182184
}
183185

184186
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
@@ -190,7 +192,7 @@ impl Timespec {
190192
nsec += NSEC_PER_SEC as i32;
191193
secs = secs.checked_sub(1)?;
192194
}
193-
Some(Timespec::new(secs, nsec.into()))
195+
Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
194196
}
195197

196198
#[allow(dead_code)]
@@ -226,12 +228,6 @@ impl Timespec {
226228
}
227229
}
228230

229-
impl From<libc::timespec> for Timespec {
230-
fn from(t: libc::timespec) -> Timespec {
231-
Timespec::new(t.tv_sec as i64, t.tv_nsec as i64)
232-
}
233-
}
234-
235231
#[cfg(all(
236232
target_os = "linux",
237233
target_env = "gnu",
@@ -260,18 +256,6 @@ impl __timespec64 {
260256
}
261257
}
262258

263-
#[cfg(all(
264-
target_os = "linux",
265-
target_env = "gnu",
266-
target_pointer_width = "32",
267-
not(target_arch = "riscv32")
268-
))]
269-
impl From<__timespec64> for Timespec {
270-
fn from(t: __timespec64) -> Timespec {
271-
Timespec::new(t.tv_sec, t.tv_nsec.into())
272-
}
273-
}
274-
275259
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
276260
pub struct Instant {
277261
t: Timespec,

0 commit comments

Comments
 (0)