Skip to content

Commit

Permalink
unix time module now return result
Browse files Browse the repository at this point in the history
  • Loading branch information
Stargateur committed Sep 22, 2023
1 parent 99b63d0 commit be78042
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 63 deletions.
30 changes: 15 additions & 15 deletions library/std/src/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,15 +452,15 @@ impl FileAttr {
#[cfg(target_os = "netbsd")]
impl FileAttr {
pub fn modified(&self) -> io::Result<SystemTime> {
Ok(SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtimensec as i64))
SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtimensec as i64)
}

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

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

Expand All @@ -477,16 +477,16 @@ impl FileAttr {
#[cfg(target_pointer_width = "32")]
cfg_has_statx! {
if let Some(mtime) = self.stx_mtime() {
return Ok(SystemTime::new(mtime.tv_sec, mtime.tv_nsec as i64));
return SystemTime::new(mtime.tv_sec, mtime.tv_nsec as i64);
}
}

Ok(SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtime_nsec as i64))
SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtime_nsec as i64)
}

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

#[cfg(any(target_os = "horizon", target_os = "hurd"))]
Expand All @@ -505,16 +505,16 @@ impl FileAttr {
#[cfg(target_pointer_width = "32")]
cfg_has_statx! {
if let Some(atime) = self.stx_atime() {
return Ok(SystemTime::new(atime.tv_sec, atime.tv_nsec as i64));
return SystemTime::new(atime.tv_sec, atime.tv_nsec as i64);
}
}

Ok(SystemTime::new(self.stat.st_atime as i64, self.stat.st_atime_nsec as i64))
SystemTime::new(self.stat.st_atime as i64, self.stat.st_atime_nsec as i64)
}

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

#[cfg(any(target_os = "horizon", target_os = "hurd"))]
Expand All @@ -531,7 +531,7 @@ impl FileAttr {
target_os = "watchos",
))]
pub fn created(&self) -> io::Result<SystemTime> {
Ok(SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtime_nsec as i64))
SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtime_nsec as i64)
}

#[cfg(not(any(
Expand All @@ -547,7 +547,7 @@ impl FileAttr {
cfg_has_statx! {
if let Some(ext) = &self.statx_extra_fields {
return if (ext.stx_mask & libc::STATX_BTIME) != 0 {
Ok(SystemTime::new(ext.stx_btime.tv_sec, ext.stx_btime.tv_nsec as i64))
SystemTime::new(ext.stx_btime.tv_sec, ext.stx_btime.tv_nsec as i64)
} else {
Err(io::const_io_error!(
io::ErrorKind::Uncategorized,
Expand All @@ -566,22 +566,22 @@ impl FileAttr {

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

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

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

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

Expand Down
70 changes: 24 additions & 46 deletions library/std/src/sys/unix/time.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::fmt;
use crate::time::Duration;
use crate::{fmt, io};

pub use self::inner::Instant;

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

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

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

impl From<libc::timespec> for SystemTime {
fn from(t: libc::timespec) -> SystemTime {
SystemTime { t: Timespec::from(t) }
}
}

impl fmt::Debug for SystemTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SystemTime")
Expand All @@ -70,12 +64,24 @@ impl fmt::Debug for SystemTime {

impl Timespec {
pub const fn zero() -> Timespec {
Timespec::new(0, 0)
unsafe { Self::new_unchecked(0, 0) }
}

const fn new(tv_sec: i64, tv_nsec: i64) -> Timespec {
const fn new_assert(tv_sec: i64, tv_nsec: i64) -> Self {
assert!(tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC as i64);
// SAFETY: The assert above checks tv_nsec is within the valid range
unsafe { Timespec::new_unchecked(tv_sec as i64, tv_nsec as i64) }
}

const fn new(tv_sec: i64, tv_nsec: i64) -> Result<Timespec, io::Error> {
if tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC as i64 {
Ok(unsafe { Self::new_unchecked(tv_sec, tv_nsec) })
} else {
Err(io::const_io_error!(io::ErrorKind::Other, "Invalid time for nanosecond"))
}
}

const unsafe fn new_unchecked(tv_sec: i64, tv_nsec: i64) -> Timespec {
Timespec { tv_sec, tv_nsec: unsafe { Nanoseconds(tv_nsec as u32) } }
}

Expand Down Expand Up @@ -122,7 +128,7 @@ impl Timespec {
nsec -= NSEC_PER_SEC as u32;
secs = secs.checked_add(1)?;
}
Some(Timespec::new(secs, nsec.into()))
Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
}

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

#[allow(dead_code)]
Expand Down Expand Up @@ -170,12 +176,6 @@ impl Timespec {
}
}

impl From<libc::timespec> for Timespec {
fn from(t: libc::timespec) -> Timespec {
Timespec::new(t.tv_sec as i64, t.tv_nsec as i64)
}
}

#[cfg(all(
target_os = "linux",
target_env = "gnu",
Expand Down Expand Up @@ -204,18 +204,6 @@ impl __timespec64 {
}
}

#[cfg(all(
target_os = "linux",
target_env = "gnu",
target_pointer_width = "32",
not(target_arch = "riscv32")
))]
impl From<__timespec64> for Timespec {
fn from(t: __timespec64) -> Timespec {
Timespec::new(t.tv_sec, t.tv_nsec.into())
}
}

#[cfg(any(
all(target_os = "macos", any(not(target_arch = "aarch64"))),
target_os = "ios",
Expand Down Expand Up @@ -274,19 +262,7 @@ mod inner {

let mut s = libc::timeval { tv_sec: 0, tv_usec: 0 };
cvt(unsafe { libc::gettimeofday(&mut s, ptr::null_mut()) }).unwrap();
return SystemTime::from(s);
}
}

impl From<libc::timeval> for Timespec {
fn from(t: libc::timeval) -> Timespec {
Timespec::new(t.tv_sec as i64, 1000 * t.tv_usec as i64)
}
}

impl From<libc::timeval> for SystemTime {
fn from(t: libc::timeval) -> SystemTime {
SystemTime { t: Timespec::from(t) }
SystemTime { t: Timespec::new_assert(s.tv_sec as i64, s.tv_usec as i64 * 1000) }
}
}

Expand Down Expand Up @@ -412,13 +388,15 @@ mod inner {
if let Some(clock_gettime64) = __clock_gettime64.get() {
let mut t = MaybeUninit::uninit();
cvt(unsafe { clock_gettime64(clock, t.as_mut_ptr()) }).unwrap();
return Timespec::from(unsafe { t.assume_init() });
let t = unsafe { t.assume_init() };
return Timespec::new_assert(t.tv_sec as i64, t.tv_nsec as i64);
}
}

let mut t = MaybeUninit::uninit();
cvt(unsafe { libc::clock_gettime(clock, t.as_mut_ptr()) }).unwrap();
Timespec::from(unsafe { t.assume_init() })
let t = unsafe { t.assume_init() };
Timespec::new_assert(t.tv_sec as i64, t.tv_nsec as i64)
}
}
}
2 changes: 1 addition & 1 deletion library/stdarch
Submodule stdarch updated 54 files
+16 −20 .github/workflows/main.yml
+4 −3 ci/docker/riscv64gc-unknown-linux-gnu/Dockerfile
+2 −4 ci/dox.sh
+0 −5 ci/run-docker.sh
+0 −6 ci/run.sh
+23 −0 crates/core_arch/src/aarch64/armclang.rs
+14 −0 crates/core_arch/src/aarch64/mod.rs
+233 −525 crates/core_arch/src/aarch64/neon/generated.rs
+12 −12 crates/core_arch/src/aarch64/neon/mod.rs
+2 −2 crates/core_arch/src/aarch64/tme.rs
+104 −0 crates/core_arch/src/aarch64/v8.rs
+35 −0 crates/core_arch/src/arm/armclang.rs
+125 −0 crates/core_arch/src/arm/ex.rs
+14 −0 crates/core_arch/src/arm/mod.rs
+28 −0 crates/core_arch/src/arm/neon.rs
+49 −0 crates/core_arch/src/arm/v6.rs
+87 −0 crates/core_arch/src/arm/v7.rs
+3 −0 crates/core_arch/src/arm_shared/mod.rs
+221 −625 crates/core_arch/src/arm_shared/neon/generated.rs
+9 −0 crates/core_arch/src/arm_shared/registers/aarch32.rs
+121 −0 crates/core_arch/src/arm_shared/registers/mod.rs
+39 −0 crates/core_arch/src/arm_shared/registers/v6m.rs
+17 −0 crates/core_arch/src/arm_shared/registers/v7m.rs
+0 −2 crates/core_arch/src/lib.rs
+2 −7 crates/core_arch/src/mod.rs
+1 −106 crates/core_arch/src/powerpc/altivec.rs
+0 −5 crates/core_arch/src/riscv32/mod.rs
+0 −367 crates/core_arch/src/riscv32/zk.rs
+0 −4 crates/core_arch/src/riscv64/mod.rs
+0 −281 crates/core_arch/src/riscv64/zk.rs
+171 −52 crates/core_arch/src/riscv_shared/mod.rs
+0 −150 crates/core_arch/src/riscv_shared/zb.rs
+0 −462 crates/core_arch/src/riscv_shared/zk.rs
+2 −2 crates/core_arch/src/wasm32/relaxed_simd.rs
+29 −9 crates/core_arch/src/wasm32/simd128.rs
+17 −11 crates/core_arch/src/x86/avx.rs
+25 −33 crates/core_arch/src/x86/avx2.rs
+77 −81 crates/core_arch/src/x86/avx512bw.rs
+244 −258 crates/core_arch/src/x86/avx512f.rs
+3 −3 crates/core_arch/src/x86/mod.rs
+30 −34 crates/core_arch/src/x86/sse2.rs
+15 −6 crates/intrinsic-test/missing_aarch64.txt
+18 −0 crates/intrinsic-test/missing_arm.txt
+4 −4 crates/intrinsic-test/src/argument.rs
+1 −3 crates/std_detect/src/detect/arch/arm.rs
+0 −8 crates/std_detect/src/detect/os/linux/arm.rs
+2 −0 crates/std_detect/src/lib.rs
+5 −14 crates/std_detect/tests/cpu-detection.rs
+38 −305 crates/stdarch-gen/neon.spec
+1 −20 crates/stdarch-gen/src/main.rs
+1 −3 crates/stdarch-test/Cargo.toml
+25 −33 crates/stdarch-test/src/disassembly.rs
+6 −9 crates/stdarch-test/src/lib.rs
+1 −1 examples/connect5.rs

0 comments on commit be78042

Please sign in to comment.