Skip to content

Commit

Permalink
lib.rs: Switch from wrapping to saturating add
Browse files Browse the repository at this point in the history
  • Loading branch information
vpetrigo committed Aug 17, 2024
1 parent 609f805 commit b6f4a47
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@
//! socket
//! .set_read_timeout(Some(Duration::from_secs(2)))
//! .expect("Unable to set UDP socket read timeout");
//! # #[cfg(all(feature = "std", feature = "sup"))]
//! # #[cfg(all(feature = "std"))]
//! let result = sntpc::simple_get_time("time.google.com:123", socket);
//! # #[cfg(all(feature = "std", feature = "sup"))]
//! # #[cfg(all(feature = "std"))]
//! match result {
//! Ok(time) => {
//! println!("Got time: {}.{}", time.sec(), sntpc::fraction_to_milliseconds(time.sec_fraction()));
Expand Down Expand Up @@ -604,7 +604,7 @@ fn roundtrip_calculate(
t4: u64,
units: Units,
) -> u64 {
let delta = t4.wrapping_sub(t1).wrapping_sub(t3.wrapping_sub(t2));
let delta = t4.wrapping_sub(t1).saturating_sub(t3.wrapping_sub(t2));
let delta_sec = (delta & SECONDS_MASK) >> 32;
let delta_sec_fraction = delta & SECONDS_FRAC_MASK;

Expand All @@ -619,8 +619,8 @@ fn roundtrip_calculate(
}

fn offset_calculate(t1: u64, t2: u64, t3: u64, t4: u64, units: Units) -> i64 {
let theta =
(t2.wrapping_sub(t1) / 2) as i64 + (t3.wrapping_sub(t4) / 2) as i64;
let theta = ((t2.wrapping_sub(t1) / 2) as i64)
.saturating_add((t3.wrapping_sub(t4) / 2) as i64);
let theta_sec = (theta.unsigned_abs() & SECONDS_MASK) >> 32;
let theta_sec_fraction = theta.unsigned_abs() & SECONDS_FRAC_MASK;

Expand Down

0 comments on commit b6f4a47

Please sign in to comment.