mirror of
https://github.com/servo/servo.git
synced 2025-08-07 06:25:32 +01:00
Use thread::sleep instead of deprecated sleep_ms
Similarly, change one instance of `thread::park_timeout_ms`. Fixes #8694
This commit is contained in:
parent
b737e4e0fa
commit
3659218c59
10 changed files with 65 additions and 25 deletions
32
components/util/time.rs
Normal file
32
components/util/time.rs
Normal file
|
@ -0,0 +1,32 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::time::Duration;
|
||||
use std::{u32, u64};
|
||||
|
||||
pub const NANOS_PER_SEC: u32 = 1_000_000_000;
|
||||
|
||||
pub fn duration_from_seconds(secs: f64) -> Duration {
|
||||
|
||||
// Get number of seconds and check that it fits in a u64.
|
||||
let whole_secs = secs.trunc();
|
||||
assert!(whole_secs >= 0.0 && whole_secs <= u64::MAX as f64);
|
||||
|
||||
// Get number of nanoseconds. This should always fit in a u32, but check anyway.
|
||||
let nanos = (secs.fract() * (NANOS_PER_SEC as f64)).trunc();
|
||||
assert!(nanos >= 0.0 && nanos <= u32::MAX as f64);
|
||||
|
||||
Duration::new(whole_secs as u64, nanos as u32)
|
||||
}
|
||||
|
||||
pub fn duration_from_nanoseconds(nanos: u64) -> Duration {
|
||||
// Get number of seconds.
|
||||
let secs = nanos / NANOS_PER_SEC as u64;
|
||||
|
||||
// Get number of extra nanoseconds. This should always fit in a u32, but check anyway.
|
||||
let subsec_nanos = nanos % NANOS_PER_SEC as u64;
|
||||
assert!(subsec_nanos <= u32::MAX as u64);
|
||||
|
||||
Duration::new(secs, subsec_nanos as u32)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue