Skip to content

Commit e694eff

Browse files
committed
unix time module now return result
1 parent f475098 commit e694eff

File tree

2 files changed

+39
-61
lines changed

2 files changed

+39
-61
lines changed

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

+15-15
Original file line numberDiff line numberDiff line change
@@ -448,15 +448,15 @@ impl FileAttr {
448448
#[cfg(target_os = "netbsd")]
449449
impl FileAttr {
450450
pub fn modified(&self) -> io::Result<SystemTime> {
451-
Ok(SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtimensec as i64))
451+
SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtimensec as i64)
452452
}
453453

454454
pub fn accessed(&self) -> io::Result<SystemTime> {
455-
Ok(SystemTime::new(self.stat.st_atime as i64, self.stat.st_atimensec as i64))
455+
SystemTime::new(self.stat.st_atime as i64, self.stat.st_atimensec as i64)
456456
}
457457

458458
pub fn created(&self) -> io::Result<SystemTime> {
459-
Ok(SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtimensec as i64))
459+
SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtimensec as i64)
460460
}
461461
}
462462

@@ -472,16 +472,16 @@ impl FileAttr {
472472
#[cfg(target_pointer_width = "32")]
473473
cfg_has_statx! {
474474
if let Some(mtime) = self.stx_mtime() {
475-
return Ok(SystemTime::new(mtime.tv_sec, mtime.tv_nsec as i64));
475+
return SystemTime::new(mtime.tv_sec, mtime.tv_nsec as i64);
476476
}
477477
}
478478

479-
Ok(SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtime_nsec as i64))
479+
SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtime_nsec as i64)
480480
}
481481

482482
#[cfg(any(target_os = "vxworks", target_os = "espidf", target_os = "vita"))]
483483
pub fn modified(&self) -> io::Result<SystemTime> {
484-
Ok(SystemTime::new(self.stat.st_mtime as i64, 0))
484+
SystemTime::new(self.stat.st_mtime as i64, 0)
485485
}
486486

487487
#[cfg(target_os = "horizon")]
@@ -499,16 +499,16 @@ impl FileAttr {
499499
#[cfg(target_pointer_width = "32")]
500500
cfg_has_statx! {
501501
if let Some(atime) = self.stx_atime() {
502-
return Ok(SystemTime::new(atime.tv_sec, atime.tv_nsec as i64));
502+
return SystemTime::new(atime.tv_sec, atime.tv_nsec as i64);
503503
}
504504
}
505505

506-
Ok(SystemTime::new(self.stat.st_atime as i64, self.stat.st_atime_nsec as i64))
506+
SystemTime::new(self.stat.st_atime as i64, self.stat.st_atime_nsec as i64)
507507
}
508508

509509
#[cfg(any(target_os = "vxworks", target_os = "espidf", target_os = "vita"))]
510510
pub fn accessed(&self) -> io::Result<SystemTime> {
511-
Ok(SystemTime::new(self.stat.st_atime as i64, 0))
511+
SystemTime::new(self.stat.st_atime as i64, 0)
512512
}
513513

514514
#[cfg(target_os = "horizon")]
@@ -525,7 +525,7 @@ impl FileAttr {
525525
target_os = "watchos",
526526
))]
527527
pub fn created(&self) -> io::Result<SystemTime> {
528-
Ok(SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtime_nsec as i64))
528+
SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtime_nsec as i64)
529529
}
530530

531531
#[cfg(not(any(
@@ -541,7 +541,7 @@ impl FileAttr {
541541
cfg_has_statx! {
542542
if let Some(ext) = &self.statx_extra_fields {
543543
return if (ext.stx_mask & libc::STATX_BTIME) != 0 {
544-
Ok(SystemTime::new(ext.stx_btime.tv_sec, ext.stx_btime.tv_nsec as i64))
544+
SystemTime::new(ext.stx_btime.tv_sec, ext.stx_btime.tv_nsec as i64)
545545
} else {
546546
Err(io::const_io_error!(
547547
io::ErrorKind::Uncategorized,
@@ -560,22 +560,22 @@ impl FileAttr {
560560

561561
#[cfg(target_os = "vita")]
562562
pub fn created(&self) -> io::Result<SystemTime> {
563-
Ok(SystemTime::new(self.stat.st_ctime as i64, 0))
563+
SystemTime::new(self.stat.st_ctime as i64, 0)
564564
}
565565
}
566566

567567
#[cfg(target_os = "nto")]
568568
impl FileAttr {
569569
pub fn modified(&self) -> io::Result<SystemTime> {
570-
Ok(SystemTime::new(self.stat.st_mtim.tv_sec, self.stat.st_mtim.tv_nsec))
570+
SystemTime::new(self.stat.st_mtim.tv_sec, self.stat.st_mtim.tv_nsec)
571571
}
572572

573573
pub fn accessed(&self) -> io::Result<SystemTime> {
574-
Ok(SystemTime::new(self.stat.st_atim.tv_sec, self.stat.st_atim.tv_nsec))
574+
SystemTime::new(self.stat.st_atim.tv_sec, self.stat.st_atim.tv_nsec)
575575
}
576576

577577
pub fn created(&self) -> io::Result<SystemTime> {
578-
Ok(SystemTime::new(self.stat.st_ctim.tv_sec, self.stat.st_ctim.tv_nsec))
578+
SystemTime::new(self.stat.st_ctim.tv_sec, self.stat.st_ctim.tv_nsec)
579579
}
580580
}
581581

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

+24-46
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
pub use self::inner::Instant;
55

@@ -36,8 +36,8 @@ pub(in crate::sys::unix) struct Timespec {
3636

3737
impl SystemTime {
3838
#[cfg_attr(target_os = "horizon", allow(unused))]
39-
pub fn new(tv_sec: i64, tv_nsec: i64) -> SystemTime {
40-
SystemTime { t: Timespec::new(tv_sec, tv_nsec) }
39+
pub fn new(tv_sec: i64, tv_nsec: i64) -> Result<SystemTime, io::Error> {
40+
Ok(SystemTime { t: Timespec::new(tv_sec, tv_nsec)? })
4141
}
4242

4343
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
@@ -53,12 +53,6 @@ impl SystemTime {
5353
}
5454
}
5555

56-
impl From<libc::timespec> for SystemTime {
57-
fn from(t: libc::timespec) -> SystemTime {
58-
SystemTime { t: Timespec::from(t) }
59-
}
60-
}
61-
6256
impl fmt::Debug for SystemTime {
6357
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6458
f.debug_struct("SystemTime")
@@ -70,12 +64,24 @@ impl fmt::Debug for SystemTime {
7064

7165
impl Timespec {
7266
pub const fn zero() -> Timespec {
73-
Timespec::new(0, 0)
67+
unsafe { Self::new_unchecked(0, 0) }
7468
}
7569

76-
const fn new(tv_sec: i64, tv_nsec: i64) -> Timespec {
70+
const fn new_assert(tv_sec: i64, tv_nsec: i64) -> Self {
7771
assert!(tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC as i64);
7872
// SAFETY: The assert above checks tv_nsec is within the valid range
73+
unsafe { Timespec::new_unchecked(tv_sec as i64, tv_nsec as i64) }
74+
}
75+
76+
const fn new(tv_sec: i64, tv_nsec: i64) -> Result<Timespec, io::Error> {
77+
if tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC as i64 {
78+
Ok(unsafe { Self::new_unchecked(tv_sec, tv_nsec) })
79+
} else {
80+
Err(io::const_io_error!(io::ErrorKind::Other, "Invalid time for nanosecond"))
81+
}
82+
}
83+
84+
const unsafe fn new_unchecked(tv_sec: i64, tv_nsec: i64) -> Timespec {
7985
Timespec { tv_sec, tv_nsec: unsafe { Nanoseconds(tv_nsec as u32) } }
8086
}
8187

@@ -122,7 +128,7 @@ impl Timespec {
122128
nsec -= NSEC_PER_SEC as u32;
123129
secs = secs.checked_add(1)?;
124130
}
125-
Some(Timespec::new(secs, nsec.into()))
131+
Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
126132
}
127133

128134
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
@@ -134,7 +140,7 @@ impl Timespec {
134140
nsec += NSEC_PER_SEC as i32;
135141
secs = secs.checked_sub(1)?;
136142
}
137-
Some(Timespec::new(secs, nsec.into()))
143+
Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
138144
}
139145

140146
#[allow(dead_code)]
@@ -170,12 +176,6 @@ impl Timespec {
170176
}
171177
}
172178

173-
impl From<libc::timespec> for Timespec {
174-
fn from(t: libc::timespec) -> Timespec {
175-
Timespec::new(t.tv_sec as i64, t.tv_nsec as i64)
176-
}
177-
}
178-
179179
#[cfg(all(
180180
target_os = "linux",
181181
target_env = "gnu",
@@ -204,18 +204,6 @@ impl __timespec64 {
204204
}
205205
}
206206

207-
#[cfg(all(
208-
target_os = "linux",
209-
target_env = "gnu",
210-
target_pointer_width = "32",
211-
not(target_arch = "riscv32")
212-
))]
213-
impl From<__timespec64> for Timespec {
214-
fn from(t: __timespec64) -> Timespec {
215-
Timespec::new(t.tv_sec, t.tv_nsec.into())
216-
}
217-
}
218-
219207
#[cfg(any(
220208
all(target_os = "macos", any(not(target_arch = "aarch64"))),
221209
target_os = "ios",
@@ -274,19 +262,7 @@ mod inner {
274262

275263
let mut s = libc::timeval { tv_sec: 0, tv_usec: 0 };
276264
cvt(unsafe { libc::gettimeofday(&mut s, ptr::null_mut()) }).unwrap();
277-
return SystemTime::from(s);
278-
}
279-
}
280-
281-
impl From<libc::timeval> for Timespec {
282-
fn from(t: libc::timeval) -> Timespec {
283-
Timespec::new(t.tv_sec as i64, 1000 * t.tv_usec as i64)
284-
}
285-
}
286-
287-
impl From<libc::timeval> for SystemTime {
288-
fn from(t: libc::timeval) -> SystemTime {
289-
SystemTime { t: Timespec::from(t) }
265+
SystemTime { t: Timespec::new_assert(s.tv_sec as i64, s.tv_usec as i64 * 1000) }
290266
}
291267
}
292268

@@ -412,13 +388,15 @@ mod inner {
412388
if let Some(clock_gettime64) = __clock_gettime64.get() {
413389
let mut t = MaybeUninit::uninit();
414390
cvt(unsafe { clock_gettime64(clock, t.as_mut_ptr()) }).unwrap();
415-
return Timespec::from(unsafe { t.assume_init() });
391+
let t = unsafe { t.assume_init() };
392+
return Timespec::new_assert(t.tv_sec as i64, t.tv_nsec as i64);
416393
}
417394
}
418395

419396
let mut t = MaybeUninit::uninit();
420397
cvt(unsafe { libc::clock_gettime(clock, t.as_mut_ptr()) }).unwrap();
421-
Timespec::from(unsafe { t.assume_init() })
398+
let t = unsafe { t.assume_init() };
399+
Timespec::new_assert(t.tv_sec as i64, t.tv_nsec as i64)
422400
}
423401
}
424402
}

0 commit comments

Comments
 (0)