cute_async/
lib.rs

1//! Personal collection of async utilities
2#![warn(missing_docs)]
3#![cfg_attr(feature = "cargo-clippy", allow(clippy::style))]
4
5///Await future in async context.
6///
7///Because `.await` is retarded.
8///
9///```rust
10///async fn do_async() {
11///}
12///
13///async fn my_main() {
14///    cute_async::matsu!(do_async());
15///}
16///
17///```
18#[macro_export]
19macro_rules! matsu {
20    ($exp:expr) => {
21        ($exp).await
22    }
23}
24
25///Unreachable optimization hint.
26///
27///In debug mod it panics using `unreachable!` macro
28///But hitting this macro in release mode  would result in UB.
29#[macro_export]
30macro_rules! unreach {
31    () => ({
32        #[cfg(not(debug_assertions))]
33        unsafe {
34            std::hint::unreachable_unchecked();
35        }
36        #[cfg(debug_assertions)]
37        {
38            unreachable!()
39        }
40    })
41}
42
43///Gets `Pin` out of value
44///
45///## Usage:
46///
47///```rust,no_run
48///use cute_async::AsPin;
49///
50///use core::task;
51///use core::pin::Pin;
52///use core::future::Future;
53///
54///pub struct MyFuture<T>(T);
55///
56///impl<T: AsPin + Unpin + Future> Future for MyFuture<T> {
57///    type Output = T::Output;
58///
59///    fn poll(mut self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
60///        let inner = self.0.as_pin();
61///        Future::poll(inner, ctx)
62///    }
63///}
64///```
65
66pub trait AsPin: core::ops::Deref {
67    ///Gets `Pin` out of self.
68    fn as_pin(&mut self) -> core::pin::Pin<&'_ mut Self>;
69}
70
71impl<T: core::ops::Deref> AsPin for T {
72    #[inline(always)]
73    fn as_pin(&mut self) -> core::pin::Pin<&'_ mut Self> {
74        unsafe {
75            core::pin::Pin::new_unchecked(self)
76        }
77    }
78}
79
80pub mod fut;
81pub use fut::*;
82pub mod adaptors;
83pub use adaptors::*;