MCPcopy Create free account
hub / github.com/bytecodealliance/rustix / checked_add

Method checked_add

src/timespec.rs:106–121  ·  view source on GitHub ↗

Checked `Timespec` addition. Returns `None` if overflow occurred. # Panics If `0 <= .tv_nsec < 1_000_000_000` doesn't hold, this function may panic or return unexpected results. # Example ``` use rustix::event::Timespec; assert_eq!( Timespec { tv_sec: 1, tv_nsec: 2 } .checked_add(Timespec { tv_sec: 30, tv_nsec: 40 }), Some(Timespec { tv_sec: 31, tv_nsec: 42 }) ); assert_eq!( Timespec { tv_sec

(self, rhs: Self)

Source from the content-addressed store, hash-verified

104 /// );
105 /// ```
106 pub const fn checked_add(self, rhs: Self) -> Option<Self> {
107 if let Some(mut tv_sec) = self.tv_sec.checked_add(rhs.tv_sec) {
108 let mut tv_nsec = self.tv_nsec + rhs.tv_nsec;
109 if tv_nsec >= 1_000_000_000 {
110 tv_nsec -= 1_000_000_000;
111 if let Some(carried_sec) = tv_sec.checked_add(1) {
112 tv_sec = carried_sec;
113 } else {
114 return None;
115 }
116 }
117 Some(Self { tv_sec, tv_nsec })
118 } else {
119 None
120 }
121 }
122
123 /// Checked `Timespec` subtraction. Returns `None` if overflow occurred.
124 ///

Callers 9

check_raw_pointerFunction · 0.80
as_c_int_millisMethod · 0.80
addMethod · 0.80
duration_to_secsFunction · 0.80
fadviseFunction · 0.80
fallocateFunction · 0.80
init_from_sysinfo_ehdrFunction · 0.80
base_plusMethod · 0.80
duration_to_secsFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected