| 1 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#, 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 |
stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::thread_localrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/thread_local!! { |
| 4 |
static TIMErust-analyzer cargo mock_instant 0.6.0 thread_local/TIME.: RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#> = const { RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<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 |
static SYSTEM_TIMErust-analyzer cargo mock_instant 0.6.0 thread_local/SYSTEM_TIME.: RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#> = const { RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<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.) }; |
| 6 |
} |
| 7 |
|
| 8 |
fn with_timerust-analyzer cargo mock_instant 0.6.0 thread_local/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#)) { |
| 9 |
TIMErust-analyzer cargo mock_instant 0.6.0 thread_local/TIME..withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/impl#[`LocalKey<T>`]with().(|t| d(&mut t.borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().())); |
| 10 |
} |
| 11 |
|
| 12 |
fn get_timerust-analyzer cargo mock_instant 0.6.0 thread_local/get_time().() -> Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration# { |
| 13 |
TIMErust-analyzer cargo mock_instant 0.6.0 thread_local/TIME..withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/impl#[`LocalKey<T>`]with().(|t| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Ref<'_, T>`][Deref]deref().t.borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow().()) |
| 14 |
} |
| 15 |
|
| 16 |
fn with_system_timerust-analyzer cargo mock_instant 0.6.0 thread_local/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#)) { |
| 17 |
SYSTEM_TIMErust-analyzer cargo mock_instant 0.6.0 thread_local/SYSTEM_TIME..withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/impl#[`LocalKey<T>`]with().(|t| d(&mut t.borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().())); |
| 18 |
} |
| 19 |
|
| 20 |
fn get_system_timerust-analyzer cargo mock_instant 0.6.0 thread_local/get_system_time().() -> Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration# { |
| 21 |
SYSTEM_TIMErust-analyzer cargo mock_instant 0.6.0 thread_local/SYSTEM_TIME..withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/impl#[`LocalKey<T>`]with().(|t| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Ref<'_, T>`][Deref]deref().t.borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow().()) |
| 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 |
true; |
| 26 |
/// This uses thread-local 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 thread_local/MockClock#::timerust-analyzer cargo mock_instant 0.6.0 thread_local/impl#[MockClock]time().; |
| 31 |
true; |
| 32 |
/// This uses a thread-local cell 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 thread_local/MockClock#::system_timerust-analyzer cargo mock_instant 0.6.0 thread_local/impl#[MockClock]system_time().; |
| 37 |
true; |
| 38 |
/// This uses a thread-local cell 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!!({}); |
| 42 |
|
| 43 |
#[cfg(test)] |
| 44 |
mod testsrust-analyzer cargo mock_instant 0.6.0 thread_local/tests/ { |
| 45 |
|
| 46 |
use superrust-analyzer cargo mock_instant 0.6.0 thread_local/::*; |
| 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 thread_local/tests/is_thread_local().() { |
| 50 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(MockClockrust-analyzer cargo mock_instant 0.6.0 thread_local/MockClock#::is_thread_localrust-analyzer cargo mock_instant 0.6.0 thread_local/impl#[MockClock]is_thread_local().()); |
| 51 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Instantrust-analyzer cargo mock_instant 0.6.0 thread_local/Instant#::nowrust-analyzer cargo mock_instant 0.6.0 thread_local/impl#[Instant]now().().is_thread_localrust-analyzer cargo mock_instant 0.6.0 thread_local/impl#[Instant]is_thread_local().()); |
| 52 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(SystemTimerust-analyzer cargo mock_instant 0.6.0 thread_local/SystemTime#::nowrust-analyzer cargo mock_instant 0.6.0 thread_local/impl#[SystemTime]now().().is_thread_localrust-analyzer cargo mock_instant 0.6.0 thread_local/impl#[SystemTime]is_thread_local().()); |
| 53 |
} |
| 54 |
|
| 55 |
// this checks that threads get their own time source |
| 56 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 57 |
fn thread_localityrust-analyzer cargo mock_instant 0.6.0 thread_local/tests/thread_locality().() { |
| 58 |
MockClockrust-analyzer cargo mock_instant 0.6.0 thread_local/MockClock#::set_timerust-analyzer cargo mock_instant 0.6.0 thread_local/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 thread_local/Instant#::nowrust-analyzer cargo mock_instant 0.6.0 thread_local/impl#[Instant]now().(); |
| 61 |
let handles = [ |
| 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 thread_local/Instant#::nowrust-analyzer cargo mock_instant 0.6.0 thread_local/impl#[Instant]now().(); |
| 64 |
MockClockrust-analyzer cargo mock_instant 0.6.0 thread_local/MockClock#::advancerust-analyzer cargo mock_instant 0.6.0 thread_local/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 thread_local/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 |
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 || { |
| 68 |
let start = Instantrust-analyzer cargo mock_instant 0.6.0 thread_local/Instant#::nowrust-analyzer cargo mock_instant 0.6.0 thread_local/impl#[Instant]now().(); |
| 69 |
MockClockrust-analyzer cargo mock_instant 0.6.0 thread_local/MockClock#::advancerust-analyzer cargo mock_instant 0.6.0 thread_local/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)); |
| 70 |
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 thread_local/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)); |
| 71 |
}), |
| 72 |
]; |
| 73 |
|
| 74 |
MockClockrust-analyzer cargo mock_instant 0.6.0 thread_local/MockClock#::advancerust-analyzer cargo mock_instant 0.6.0 thread_local/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)); |
| 75 |
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 thread_local/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().(10)); |
| 76 |
|
| 77 |
for handle in handles { |
| 78 |
handle.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/impl#[`JoinHandle<T>`]join().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 79 |
} |
| 80 |
} |
| 81 |
} |