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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
// SPDX-Licenser-Identifier: MIT OR Apache-2.0
//! 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.
//!
//! # 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: Some(listener) }
//! }
//!
//! struct WaitThreeSeconds {
//! listener: Option<EventListener>,
//! }
//!
//! impl EventListenerFuture for WaitThreeSeconds {
//! type Output = ();
//!
//! fn poll_with_strategy<'a, S: Strategy<'a>>(
//! mut self: Pin<&mut Self>,
//! strategy: &mut S,
//! context: &mut S::Context,
//! ) -> Poll<Self::Output> {
//! strategy.poll(&mut self.listener, 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;
//! });
//! ```
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![forbid(future_incompatible, missing_docs)]
#![doc(
html_favicon_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
)]
use core::future::Future;
use core::marker::PhantomData;
use core::pin::Pin;
use core::task::{Context, Poll};
use event_listener::{EventListener, Listener};
#[doc(hidden)]
pub use pin_project_lite::pin_project;
/// A wrapper around an [`EventListenerFuture`] that can be easily exported for use.
///
/// This type implements [`Future`], has a `_new()` constructor, and a `wait()` method
/// that uses the [`Blocking`] strategy to poll the future until it is ready.
///
/// # Examples
///
/// ```
/// mod my_future {
/// use event_listener_strategy::{easy_wrapper, EventListenerFuture, Strategy};
/// use std::pin::Pin;
/// use std::task::Poll;
///
/// struct MyFuture;
///
/// impl EventListenerFuture for MyFuture {
/// type Output = ();
///
/// fn poll_with_strategy<'a, S: Strategy<'a>>(
/// self: Pin<&mut Self>,
/// strategy: &mut S,
/// context: &mut S::Context,
/// ) -> Poll<Self::Output> {
/// /* ... */
/// # Poll::Ready(())
/// }
/// }
///
/// easy_wrapper! {
/// /// A future that does something.
/// pub struct MyFutureWrapper(MyFuture => ());
/// /// Wait for it.
/// pub wait();
/// }
///
/// impl MyFutureWrapper {
/// /// Create a new instance of the future.
/// pub fn new() -> Self {
/// Self::_new(MyFuture)
/// }
/// }
/// }
///
/// use my_future::MyFutureWrapper;
///
/// // Use the future in a blocking context.
/// let future = MyFutureWrapper::new();
/// future.wait();
///
/// // Use the future in a non-blocking context.
/// futures_lite::future::block_on(async {
/// let future = MyFutureWrapper::new();
/// future.await;
/// });
/// ```
#[macro_export]
macro_rules! easy_wrapper {
(
$(#[$meta:meta])*
$vis:vis struct $name:ident
$(<
$( $lifetime:lifetime $(: $lifetime_bound:lifetime)? ),* $(,)?
$( $generics:ident
$(: $generics_bound:path)?
$(: ?$generics_unsized_bound:path)?
$(: $generics_lifetime_bound:lifetime)?
$(= $generics_default:ty)?
),* $(,)?
>)?
($inner:ty => $output:ty)
$(where
$( $where_clause_ty:ty
$(: $where_clause_bound:path)?
$(: ?$where_clause_unsized_bound:path)?
$(: $where_clause_lifetime_bound:lifetime)?
),* $(,)?
)?
;
$(#[$wait_meta:meta])*
$wait_vis: vis wait();
) => {
$crate::pin_project! {
$(#[$meta])*
$vis struct $name $(<
$( $lifetime $(: $lifetime_bound)? ),*
$( $generics
$(: $generics_bound)?
$(: ?$generics_unsized_bound)?
$(: $generics_lifetime_bound)?
$(= $generics_default)?
),*
>)? $(
where
$( $where_clause_ty
$(: $where_clause_bound)?
$(: ?$where_clause_unsized_bound)?
$(: $where_clause_lifetime_bound)?
),*
)? {
#[pin]
_inner: $crate::FutureWrapper<$inner>
}
}
impl $(<
$( $lifetime $(: $lifetime_bound)? ,)*
$( $generics
$(: $generics_bound)?
$(: ?$generics_unsized_bound)?
$(: $generics_lifetime_bound)?
$(= $generics_default)?
),*
>)? $name $(<
$( $lifetime ,)*
$( $generics ),*
>)? $(
where
$( $where_clause_ty
$(: $where_clause_bound)?
$(: ?$where_clause_unsized_bound)?
$(: $where_clause_lifetime_bound)?
),*
)? {
#[inline]
fn _new(inner: $inner) -> Self {
Self {
_inner: $crate::FutureWrapper::new(inner)
}
}
$(#[$wait_meta])*
#[inline]
$wait_vis fn wait(self) -> $output {
use $crate::EventListenerFuture;
self._inner.into_inner().wait()
}
pub(crate) fn poll_with_strategy<'__strategy, __S: $crate::Strategy<'__strategy>>(
self: ::core::pin::Pin<&mut Self>,
strategy: &mut __S,
context: &mut __S::Context,
) -> ::core::task::Poll<$output> {
self.project()._inner.get_pin_mut().poll_with_strategy(strategy, context)
}
}
impl $(<
$( $lifetime $(: $lifetime_bound)? ,)*
$( $generics
$(: $generics_bound)?
$(: ?$generics_unsized_bound)?
$(: $generics_lifetime_bound)?
$(= $generics_default)?
),*
>)? ::core::future::Future for $name $(
<
$( $lifetime ,)*
$( $generics ),*
>
)? $(
where
$( $where_clause_ty
$(: $where_clause_bound)?
$(: ?$where_clause_unsized_bound)?
$(: $where_clause_lifetime_bound)?
),*
)? {
type Output = $output;
#[inline]
fn poll(
self: ::core::pin::Pin<&mut Self>,
context: &mut ::core::task::Context<'_>
) -> ::core::task::Poll<Self::Output> {
self.project()._inner.poll(context)
}
}
};
}
/// A future that runs using the [`event-listener`] crate.
///
/// This is similar to the [`Future`] trait from libstd, with one notable difference: it takes
/// a strategy that tells it whether to operate in a blocking or non-blocking context. The
/// `poll_with_strategy` method is the equivalent of the `poll` method in this regard; it uses
/// the [`Strategy`] trait to determine how to poll the future.
///
/// From here, there are two additional things one can do with this trait:
///
/// - The `wait` method, which uses the [`Blocking`] strategy to poll the future until it is
/// ready, blocking the current thread until it is.
/// - The [`FutureWrapper`] type, which implements [`Future`] and uses the [`NonBlocking`]
/// strategy to poll the future.
pub trait EventListenerFuture {
/// The type of value produced on completion.
type Output;
/// Poll the future using the provided strategy.
///
/// This function should use the `Strategy::poll` method to poll the future, and proceed
/// based on the result.
fn poll_with_strategy<'a, S: Strategy<'a>>(
self: Pin<&mut Self>,
strategy: &mut S,
context: &mut S::Context,
) -> Poll<Self::Output>;
/// Wait for the future to complete, blocking the current thread.
///
/// This function uses the [`Blocking`] strategy to poll the future until it is ready.
///
/// The future should only return `Pending` if `Strategy::poll` returns error. Otherwise,
/// this function polls the future in a hot loop.
#[cfg(all(feature = "std", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(all(feature = "std", not(target_family = "wasm"))))]
fn wait(mut self) -> Self::Output
where
Self: Sized,
{
// SAFETY: `self`/`this` is not moved out after this.
let mut this = unsafe { Pin::new_unchecked(&mut self) };
loop {
if let Poll::Ready(res) = this
.as_mut()
.poll_with_strategy(&mut Blocking::default(), &mut ())
{
return res;
}
}
}
}
pin_project_lite::pin_project! {
/// A wrapper around an [`EventListenerFuture`] that implements [`Future`].
///
/// [`Future`]: core::future::Future
#[derive(Debug, Clone)]
pub struct FutureWrapper<F: ?Sized> {
#[pin]
inner: F,
}
}
impl<F: EventListenerFuture> FutureWrapper<F> {
/// Create a new `FutureWrapper` from the provided future.
#[inline]
pub fn new(inner: F) -> Self {
Self { inner }
}
/// Consume the `FutureWrapper`, returning the inner future.
#[inline]
pub fn into_inner(self) -> F {
self.inner
}
}
impl<F: ?Sized> FutureWrapper<F> {
/// Get a reference to the inner future.
#[inline]
pub fn get_ref(&self) -> &F {
&self.inner
}
/// Get a mutable reference to the inner future.
#[inline]
pub fn get_mut(&mut self) -> &mut F {
&mut self.inner
}
/// Get a pinned mutable reference to the inner future.
#[inline]
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut F> {
self.project().inner
}
/// Get a pinned reference to the inner future.
#[inline]
pub fn get_pin_ref(self: Pin<&Self>) -> Pin<&F> {
self.project_ref().inner
}
}
impl<F: EventListenerFuture> From<F> for FutureWrapper<F> {
#[inline]
fn from(inner: F) -> Self {
Self { inner }
}
}
impl<F: EventListenerFuture + ?Sized> Future for FutureWrapper<F> {
type Output = F::Output;
#[inline]
fn poll(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
self.project()
.inner
.poll_with_strategy(&mut NonBlocking::default(), context)
}
}
/// A strategy for polling an [`EventListenerFuture`] or an [`EventListener`].
///
/// This trait is used by the [`EventListenerFuture::poll_with_strategy`] method to determine
/// how to poll the future. It can also be used standalone, by calling the [`Strategy::wait`]
/// method.
///
/// [`EventListenerFuture::poll_with_strategy`]: EventListenerFuture::poll_with_strategy
/// [`EventListener`]: event_listener::EventListener
///
/// # Examples
///
/// ```
/// use event_listener::{Event, EventListener};
/// use event_listener_strategy::{EventListenerFuture, Strategy, Blocking, NonBlocking};
/// use std::pin::Pin;
///
/// async fn wait_on<'a, S: Strategy<'a>>(evl: EventListener, strategy: &mut S) {
/// strategy.wait(evl).await;
/// }
///
/// # futures_lite::future::block_on(async {
/// // Block on the future.
/// let ev = Event::new();
/// let listener = ev.listen();
/// ev.notify(1);
///
/// wait_on(listener, &mut Blocking::default()).await;
///
/// // Poll the future.
/// let listener = ev.listen();
/// ev.notify(1);
///
/// wait_on(listener, &mut NonBlocking::default()).await;
/// # });
/// ```
pub trait Strategy<'a> {
/// The context needed to poll the future.
type Context: ?Sized;
/// The future returned by the [`Strategy::wait`] method.
type Future: Future + 'a;
/// Poll the event listener until it is ready.
fn poll<T, L: Listener<T> + Unpin>(
&mut self,
event_listener: &mut Option<L>,
context: &mut Self::Context,
) -> Poll<T>;
/// Wait for the event listener to become ready.
fn wait(&mut self, evl: EventListener) -> Self::Future;
}
/// A strategy that uses polling to efficiently wait for an event.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct NonBlocking<'a> {
/// The type `&'a mut &'a T` is invariant over `'a`, like `Context` is.
///
/// We used to just use `Context` here, but then `Context` became `!Send`
/// and `!Sync`, making all of the futures that use this type `!Send` and
/// `!Sync` as well. So we just take the lifetime invariance and none of
/// the downsides.
_marker: PhantomData<&'a mut &'a ()>,
}
impl<'a, 'evl> Strategy<'evl> for NonBlocking<'a> {
type Context = Context<'a>;
type Future = EventListener;
#[inline]
fn wait(&mut self, evl: EventListener) -> Self::Future {
evl
}
#[inline]
fn poll<T, L: Listener<T> + Unpin>(
&mut self,
event_listener: &mut Option<L>,
context: &mut Self::Context,
) -> Poll<T> {
let poll = Pin::new(
event_listener
.as_mut()
.expect("`event_listener` should never be `None`"),
)
.poll(context);
if poll.is_ready() {
*event_listener = None;
}
poll
}
}
/// A strategy that blocks the current thread until the event is signalled.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
pub struct Blocking {
_private: (),
}
#[cfg(all(feature = "std", not(target_family = "wasm")))]
impl<'evl> Strategy<'evl> for Blocking {
type Context = ();
type Future = Ready;
#[inline]
fn wait(&mut self, evl: EventListener) -> Self::Future {
evl.wait();
Ready { _private: () }
}
#[inline]
fn poll<T, L: Listener<T> + Unpin>(
&mut self,
event_listener: &mut Option<L>,
_context: &mut Self::Context,
) -> Poll<T> {
let result = event_listener
.take()
.expect("`event_listener` should never be `None`")
.wait();
Poll::Ready(result)
}
}
/// A future that is always ready.
#[cfg(feature = "std")]
#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct Ready {
_private: (),
}
#[cfg(feature = "std")]
impl Future for Ready {
type Output = ();
#[inline]
fn poll(self: Pin<&mut Self>, _context: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(())
}
}
#[test]
fn send_and_sync() {
fn assert_send_and_sync<T: Send + Sync>() {}
#[cfg(all(feature = "std", not(target_family = "wasm")))]
{
assert_send_and_sync::<Blocking>();
assert_send_and_sync::<Ready>();
}
assert_send_and_sync::<NonBlocking<'static>>();
assert_send_and_sync::<FutureWrapper<()>>();
}