Skip to main content

traffic_light/
future.rs

1//! Extension trait for [`Future`].
2//!
3//! # Examples
4//!
5//! ```
6//! use std::{
7//!     error,
8//!     result
9//! };
10//!
11//! use traffic_light::future::FutureExt as _;
12//!
13//! fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
14//!     async {
15//!         // ...
16//!         Ok::<(), Box<dyn std::error::Error>>(())
17//!     }.block_on()?;
18//!
19//!     Ok(())
20//! }
21//! ```
22
23use crate::executor::Executor;
24
25/// An extension trait for [`Future`] that provides blocking.
26pub trait FutureExt: Future {
27    /// Blocks the current thread on this future.
28    ///
29    /// # Panics
30    ///
31    /// Panics if this future panics.
32    ///
33    /// # Examples
34    ///
35    /// ```
36    /// # use std::{
37    /// #     error,
38    /// #     result
39    /// # };
40    /// #
41    /// use traffic_light::future::FutureExt as _;
42    ///
43    /// # fn main() -> result::Result<(), Box<dyn error::Error>> {
44    /// let x: result::Result<(), Box<dyn error::Error>> =
45    ///     async {
46    ///         // ...
47    ///         Ok(())
48    ///     }.block_on();
49    ///
50    /// assert!(x.is_ok());
51    /// #
52    /// #     Ok(())
53    /// # }
54    /// ```
55    fn block_on(self) -> Self::Output;
56}
57
58impl<F: Future> FutureExt for F {
59    fn block_on(self) -> Self::Output {
60        Executor::block_on(self)
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn block_on_result_ok() {
70        let x: std::result::Result<(), Box<dyn std::error::Error>> =
71            async {
72                Ok(())
73            }.block_on();
74
75        assert!(x.is_ok());
76    }
77
78    #[test]
79    fn block_on_result_err() {
80        let x: std::result::Result<(), &str> =
81            async {
82                Err("")
83            }.block_on();
84
85        assert!(x.is_err());
86    }
87}