| 1 | use callooprust-analyzer cargo calloop 0.10.5 crate/::{ |
| 2 | timerrust-analyzer cargo calloop 0.10.5 sources/timer/::{TimeoutActionrust-analyzer cargo calloop 0.10.5 sources/timer/TimeoutAction#, Timerrust-analyzer cargo calloop 0.10.5 sources/timer/Timer#}, |
| 3 | EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#, LoopSignalrust-analyzer cargo calloop 0.10.5 loop_logic/LoopSignal#, |
| 4 | }; |
| 5 | |
| 6 | fn mainrust-analyzer cargo calloop 0.10.5 main().() { |
| 7 | // Create the event loop. The loop is parameterised by the kind of shared |
| 8 | // data you want the callbacks to use. In this case, we want to be able to |
| 9 | // stop the loop when the timer fires, so we provide the loop with a |
| 10 | // LoopSignal, which has the ability to stop the loop from within events. We |
| 11 | // just annotate the type here; the actual data is provided later in the |
| 12 | // run() call. |
| 13 | let mut event_loop: EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#<LoopSignalrust-analyzer cargo calloop 0.10.5 loop_logic/LoopSignal#> = |
| 14 | EventLooprust-analyzer cargo calloop 0.10.5 loop_logic/EventLoop#::try_newrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]try_new().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Failed to initialize the event loop!"); |
| 15 | |
| 16 | // Retrieve a handle. It is used to insert new sources into the event loop |
| 17 | // It can be cloned, allowing you to insert sources from within source |
| 18 | // callbacks. |
| 19 | let handle = event_loop.handlerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]handle().(); |
| 20 | |
| 21 | // Create our event source, a timer, that will expire in 2 seconds |
| 22 | let source = Timerrust-analyzer cargo calloop 0.10.5 sources/timer/Timer#::from_durationrust-analyzer cargo calloop 0.10.5 sources/timer/impl#[Timer]from_duration().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::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#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(2)); |
| 23 | |
| 24 | // Inserting an event source takes this general form. It can also be done |
| 25 | // from within the callback of another event source. |
| 26 | handle |
| 27 | .insert_sourcerust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`LoopHandle<'l, Data>`]insert_source().( |
| 28 | // a type which implements the EventSource trait |
| 29 | source, |
| 30 | // a callback that is invoked whenever this source generates an event |
| 31 | |event, _metadata, shared_data| { |
| 32 | // This callback is given 3 values: |
| 33 | // - the event generated by the source (in our case, timer events are the Instant |
| 34 | // representing the deadline for which it has fired) |
| 35 | // - &mut access to some metadata, specific to the event source (in our case, a |
| 36 | // timer handle) |
| 37 | // - &mut access to the global shared data that was passed to EventLoop::run or |
| 38 | // EventLoop::dispatch (in our case, a LoopSignal object to stop the loop) |
| 39 | // |
| 40 | // The return type is just () because nothing uses it. Some |
| 41 | // sources will expect a Result of some kind instead. |
| 42 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Timeout for {:?} expired!", event); |
| 43 | // notify the event loop to stop running using the signal in the shared data |
| 44 | // (see below) |
| 45 | shared_data.stoprust-analyzer cargo calloop 0.10.5 loop_logic/impl#[LoopSignal]stop().(); |
| 46 | // The timer event source requires us to return a TimeoutAction to |
| 47 | // specify if the timer should be rescheduled. In our case we just drop it. |
| 48 | TimeoutActionrust-analyzer cargo calloop 0.10.5 sources/timer/TimeoutAction#::Droprust-analyzer cargo calloop 0.10.5 sources/timer/TimeoutAction#Drop# |
| 49 | }, |
| 50 | ) |
| 51 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Failed to insert event source!"); |
| 52 | |
| 53 | // Create the shared data for our loop. |
| 54 | let mut shared_data = event_loop.get_signalrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]get_signal().(); |
| 55 | |
| 56 | // Actually run the event loop. This will dispatch received events to their |
| 57 | // callbacks, waiting at most 20ms for new events between each invocation of |
| 58 | // the provided callback (pass None for the timeout argument if you want to |
| 59 | // wait indefinitely between events). |
| 60 | // |
| 61 | // This is where we pass the *value* of the shared data, as a mutable |
| 62 | // reference that will be forwarded to all your callbacks, allowing them to |
| 63 | // share some state |
| 64 | event_loop |
| 65 | .runrust-analyzer cargo calloop 0.10.5 loop_logic/impl#[`EventLoop<'l, Data>`]run().( |
| 66 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::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#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(20), |
| 67 | &mut shared_data, |
| 68 | |_shared_data| { |
| 69 | // Finally, this is where you can insert the processing you need |
| 70 | // to do do between each waiting event eg. drawing logic if |
| 71 | // you're doing a GUI app. |
| 72 | }, |
| 73 | ) |
| 74 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Error during event loop!"); |
| 75 | } |