1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
# event-listener-strategy
[](
https://github.com/smol-rs/event-listener-strategy/actions)
[](
https://github.com/smol-rs/event-listener-strategy)
[](
https://crates.io/crates/event-listener-strategy)
[](
https://docs.rs/event-listener-strategy)
A strategy for using the [`event-listener`] crate in both blocking and non-blocking contexts.
One of the stand-out features of the [`event-listener`] crate is the ability to use it in both
asynchronous and synchronous contexts. However, sometimes using it like this causes a lot of
boilerplate to be duplicated. This crate aims to reduce that boilerplate by providing an `EventListenerFuture` trait that implements both blocking and non-blocking functionality.
[`event-listener`]: https://docs.rs/event-listener
# Examples
```
use event_listener::{Event, EventListener};
use event_listener_strategy::{EventListenerFuture, FutureWrapper, Strategy};
use std::pin::Pin;
use std::task::Poll;
use std::thread;
use std::sync::Arc;
// A future that waits three seconds for an event to be fired.
fn wait_three_seconds() -> WaitThreeSeconds {
let event = Event::new();
let listener = event.listen();
thread::spawn(move || {
thread::sleep(std::time::Duration::from_secs(3));
event.notify(1);
});
WaitThreeSeconds { listener }
}
struct WaitThreeSeconds {
listener: Pin<Box<EventListener>>,
}
impl EventListenerFuture for WaitThreeSeconds {
type Output = ();
fn poll_with_strategy<'a, S: Strategy<'a>>(
mut self: Pin<&'a mut Self>,
strategy: &mut S,
context: &mut S::Context,
) -> Poll<Self::Output> {
strategy.poll(self.listener.as_mut(), context)
}
}
// Use the future in a blocking context.
let future = wait_three_seconds();
future.wait();
// Use the future in a non-blocking context.
futures_lite::future::block_on(async {
let future = FutureWrapper::new(wait_three_seconds());
future.await;
});
```
## License
Licensed under either of
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT)
at your option.
#### Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.
|