| 1 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#, timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#}; |
| 2 |
|
| 3 |
static TIMErust-analyzer cargo mock_instant 0.6.0 global/TIME.: Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#<Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#> = Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]new().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::ZEROrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]ZERO.); |
| 4 |
static SYSTEM_TIMErust-analyzer cargo mock_instant 0.6.0 global/SYSTEM_TIME.: Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#<Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#> = Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]new().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::ZEROrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]ZERO.); |
| 5 |
|
| 6 |
fn with_timerust-analyzer cargo mock_instant 0.6.0 global/with_time().(d: impl Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&mut Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#)) { |
| 7 |
let mut t = TIMErust-analyzer cargo mock_instant 0.6.0 global/TIME..lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 8 |
d(&mut t); |
| 9 |
} |
| 10 |
|
| 11 |
fn get_timerust-analyzer cargo mock_instant 0.6.0 global/get_time().() -> Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration# { |
| 12 |
*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`MutexGuard<'_, T>`][Deref]deref().TIMErust-analyzer cargo mock_instant 0.6.0 global/TIME..lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 13 |
} |
| 14 |
|
| 15 |
fn with_system_timerust-analyzer cargo mock_instant 0.6.0 global/with_system_time().(d: impl Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&mut Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#)) { |
| 16 |
let mut t = SYSTEM_TIMErust-analyzer cargo mock_instant 0.6.0 global/SYSTEM_TIME..lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 17 |
d(&mut t); |
| 18 |
} |
| 19 |
|
| 20 |
fn get_system_timerust-analyzer cargo mock_instant 0.6.0 global/get_system_time().() -> Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration# { |
| 21 |
*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`MutexGuard<'_, T>`][Deref]deref().SYSTEM_TIMErust-analyzer cargo mock_instant 0.6.0 global/SYSTEM_TIME..lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 22 |
} |
| 23 |
|
| 24 |
craterust-analyzer cargo mock_instant 0.6.0 crate/::macrosrust-analyzer cargo mock_instant 0.6.0 macros/::define_mock_clockrust-analyzer cargo mock_instant 0.6.0 macros/define_mock_clock!! { |
| 25 |
false; |
| 26 |
/// This uses a global mutex state for the deterministic clock |
| 27 |
} |
| 28 |
|
| 29 |
craterust-analyzer cargo mock_instant 0.6.0 crate/::macrosrust-analyzer cargo mock_instant 0.6.0 macros/::define_instantrust-analyzer cargo mock_instant 0.6.0 macros/define_instant!! { |
| 30 |
MockClockrust-analyzer cargo mock_instant 0.6.0 global/MockClock#::timerust-analyzer cargo mock_instant 0.6.0 global/impl#[MockClock]time().; |
| 31 |
false; |
| 32 |
/// This uses a global mutex for its time source |
| 33 |
} |
| 34 |
|
| 35 |
craterust-analyzer cargo mock_instant 0.6.0 crate/::macrosrust-analyzer cargo mock_instant 0.6.0 macros/::define_system_timerust-analyzer cargo mock_instant 0.6.0 macros/define_system_time!! { |
| 36 |
MockClockrust-analyzer cargo mock_instant 0.6.0 global/MockClock#::system_timerust-analyzer cargo mock_instant 0.6.0 global/impl#[MockClock]system_time().; |
| 37 |
false; |
| 38 |
/// This uses a global mutex for its time source |
| 39 |
} |
| 40 |
|
| •41 |
craterust-analyzer cargo mock_instant 0.6.0 crate/::macrosrust-analyzer cargo mock_instant 0.6.0 macros/::define_instant_testsrust-analyzer cargo mock_instant 0.6.0 macros/define_instant_tests!!(let _guard = testsrust-analyzer cargo mock_instant 0.6.0 global/tests/::TESTLOCKrust-analyzer cargo mock_instant 0.6.0 global/tests/TESTLOCK..lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().()); |
| 42 |
|
| 43 |
#[cfg(test)] |
| 44 |
mod testsrust-analyzer cargo mock_instant 0.6.0 global/tests/ { |
| 45 |
use superrust-analyzer cargo mock_instant 0.6.0 global/::*; |
| •46 |
pub (superrust-analyzer cargo mock_instant 0.6.0 global/) static TESTLOCKrust-analyzer cargo mock_instant 0.6.0 global/tests/TESTLOCK.: Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#<()> = Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]new().(()); |
| 47 |
|
| 48 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 49 |
fn is_thread_localrust-analyzer cargo mock_instant 0.6.0 global/tests/is_thread_local().() { |
| 50 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().MockClockrust-analyzer cargo mock_instant 0.6.0 global/MockClock#::is_thread_localrust-analyzer cargo mock_instant 0.6.0 global/impl#[MockClock]is_thread_local().()); |
| 51 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().Instantrust-analyzer cargo mock_instant 0.6.0 global/Instant#::nowrust-analyzer cargo mock_instant 0.6.0 global/impl#[Instant]now().().is_thread_localrust-analyzer cargo mock_instant 0.6.0 global/impl#[Instant]is_thread_local().()); |
| 52 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().SystemTimerust-analyzer cargo mock_instant 0.6.0 global/SystemTime#::nowrust-analyzer cargo mock_instant 0.6.0 global/impl#[SystemTime]now().().is_thread_localrust-analyzer cargo mock_instant 0.6.0 global/impl#[SystemTime]is_thread_local().()); |
| 53 |
} |
| 54 |
|
| 55 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 56 |
fn thread_sharingrust-analyzer cargo mock_instant 0.6.0 global/tests/thread_sharing().() { |
| •57 |
let _guard = TESTLOCKrust-analyzer cargo mock_instant 0.6.0 global/tests/TESTLOCK..lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().(); |
| 58 |
MockClockrust-analyzer cargo mock_instant 0.6.0 global/MockClock#::set_timerust-analyzer cargo mock_instant 0.6.0 global/impl#[MockClock]set_time().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::ZEROrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]ZERO.); |
| 59 |
|
| 60 |
let start = Instantrust-analyzer cargo mock_instant 0.6.0 global/Instant#::nowrust-analyzer cargo mock_instant 0.6.0 global/impl#[Instant]now().(); |
| 61 |
|
| 62 |
stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().(move || { |
| 63 |
let start = Instantrust-analyzer cargo mock_instant 0.6.0 global/Instant#::nowrust-analyzer cargo mock_instant 0.6.0 global/impl#[Instant]now().(); |
| 64 |
MockClockrust-analyzer cargo mock_instant 0.6.0 global/MockClock#::advancerust-analyzer cargo mock_instant 0.6.0 global/impl#[MockClock]advance().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(3)); |
| 65 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(start.elapsedrust-analyzer cargo mock_instant 0.6.0 global/impl#[Instant]elapsed().(), Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(3)); |
| 66 |
}) |
| 67 |
.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/impl#[`JoinHandle<T>`]join().() |
| 68 |
.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 69 |
|
| 70 |
stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().(move || { |
| 71 |
let next = Instantrust-analyzer cargo mock_instant 0.6.0 global/Instant#::nowrust-analyzer cargo mock_instant 0.6.0 global/impl#[Instant]now().(); |
| 72 |
MockClockrust-analyzer cargo mock_instant 0.6.0 global/MockClock#::advancerust-analyzer cargo mock_instant 0.6.0 global/impl#[MockClock]advance().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(30)); |
| 73 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(next.elapsedrust-analyzer cargo mock_instant 0.6.0 global/impl#[Instant]elapsed().(), Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(30)); |
| 74 |
}) |
| 75 |
.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/impl#[`JoinHandle<T>`]join().() |
| 76 |
.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 77 |
|
| 78 |
MockClockrust-analyzer cargo mock_instant 0.6.0 global/MockClock#::advancerust-analyzer cargo mock_instant 0.6.0 global/impl#[MockClock]advance().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(10)); |
| 79 |
// using seconds because a mutex can be slow on some operating systems |
| 80 |
// the creation/locking time may be in microseconds so we'll have something like 43.002 != 43 |
| 81 |
assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(start.elapsedrust-analyzer cargo mock_instant 0.6.0 global/impl#[Instant]elapsed().().as_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_secs().(), 43); |
| 82 |
} |
| 83 |
} |