├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── examples └── main.rs ├── src ├── join.rs ├── join_stream.rs ├── lib.rs ├── maybe_done.rs ├── poll_fn.rs ├── ready.rs ├── select.rs ├── try_join.rs └── try_select.rs └── tests └── test.rs /.gitignore: -------------------------------------------------------------------------------- 1 | coverage/ 2 | target/ 3 | tmp/ 4 | dist/ 5 | npm-debug.log* 6 | Cargo.lock 7 | .DS_Store 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | rust: 3 | - nightly 4 | 5 | before_script: | 6 | rustup component add clippy-preview; 7 | if ! rustup component add clippy; then 8 | target=`curl https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu/clippy`; 9 | echo "'clippy' is unavailable on the toolchain 'nightly', using the toolchain 'nightly-$target' instead"; 10 | rustup toolchain install nightly-$target; 11 | rustup default nightly-$target; 12 | rustup component add clippy; 13 | fi 14 | rustup component add rustfmt-preview; 15 | if ! rustup component add rustfmt; then 16 | target=`curl https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu/rustfmt`; 17 | echo "'rustfmt' is unavailable on the toolchain 'nightly', use the toolchain 'nightly-$target' instead"; 18 | rustup toolchain install nightly-$target; 19 | rustup default nightly-$target; 20 | rustup component add rustfmt; 21 | fi 22 | script: | 23 | cargo fmt -- --check && 24 | cargo clippy -- -D clippy && 25 | cargo build --verbose && 26 | cargo test --verbose 27 | cache: cargo 28 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "async-macros" 3 | version = "1.0.0" 4 | license = "MIT OR Apache-2.0" 5 | repository = "https://github.com/async-rs/async-macros" 6 | documentation = "https://docs.rs/async-macros" 7 | description = "Macros for async-std." 8 | keywords = ["async", "macros", "async-rs", "futures", "await"] 9 | categories = ["asynchronous"] 10 | authors = ["Yoshua Wuyts "] 11 | readme = "README.md" 12 | edition = "2018" 13 | 14 | [dependencies] 15 | futures-core = "0.3.0" 16 | pin-utils = "0.1.0-alpha.4" 17 | 18 | [dev-dependencies] 19 | futures = "0.3.0" 20 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | Copyright 2019 Yoshua Wuyts 179 | 180 | Licensed under the Apache License, Version 2.0 (the "License"); 181 | you may not use this file except in compliance with the License. 182 | You may obtain a copy of the License at 183 | 184 | http://www.apache.org/licenses/LICENSE-2.0 185 | 186 | Unless required by applicable law or agreed to in writing, software 187 | distributed under the License is distributed on an "AS IS" BASIS, 188 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 189 | See the License for the specific language governing permissions and 190 | limitations under the License. 191 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Yoshua Wuyts 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # async-macros 2 | [![crates.io version][1]][2] [![build status][3]][4] 3 | [![downloads][5]][6] [![docs.rs docs][7]][8] 4 | 5 | Macros for async-std. 6 | 7 | - [Documentation][8] 8 | - [Crates.io][2] 9 | - [Releases][releases] 10 | 11 | ## Installation 12 | ```sh 13 | $ cargo add async-macros 14 | ``` 15 | 16 | ## Safety 17 | This crate uses `unsafe` for pin projections. 18 | 19 | ## Contributing 20 | Want to join us? Check out our ["Contributing" guide][contributing] and take a 21 | look at some of these issues: 22 | 23 | - [Issues labeled "good first issue"][good-first-issue] 24 | - [Issues labeled "help wanted"][help-wanted] 25 | 26 | ## References 27 | - https://github.com/rust-lang-nursery/futures-rs - the `join` + `try_join` 28 | macros are direct ports of the old `macro_rules` impls from `futures-rs`. 29 | 30 | ## License 31 | [MIT](./LICENSE-MIT) OR [Apache-2.0](./LICENSE-APACHE) 32 | 33 | [1]: https://img.shields.io/crates/v/async-macros.svg?style=flat-square 34 | [2]: https://crates.io/crates/async-macros 35 | [3]: https://travis-ci.com/async-rs/async-macros.svg?branch=master 36 | [4]: https://travis-ci.com/async-rs/async-macros 37 | [5]: https://img.shields.io/crates/d/async-macros.svg?style=flat-square 38 | [6]: https://crates.io/crates/async-macros 39 | [7]: https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square 40 | [8]: https://docs.rs/async-macros 41 | 42 | [releases]: https://github.com/async-rs/async-macros/releases 43 | [contributing]: https://github.com/async-rs/async-macros/blob/master.github/CONTRIBUTING.md 44 | [good-first-issue]: https://github.com/async-rs/async-macros/labels/good%20first%20issue 45 | [help-wanted]: https://github.com/async-rs/async-macros/labels/help%20wanted 46 | -------------------------------------------------------------------------------- /examples/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | futures::executor::block_on(async { 3 | async fn main() -> Result<(), std::io::Error> { 4 | use async_macros::try_select; 5 | use futures::future; 6 | use std::io::{Error, ErrorKind}; 7 | 8 | let a = future::pending::>(); 9 | let b = future::ready(Err(Error::from(ErrorKind::Other))); 10 | let c = future::ready(Ok(1u8)); 11 | 12 | assert_eq!(try_select!(a, b, c).await?, 1u8); 13 | Ok(()) 14 | } 15 | main().await.unwrap(); 16 | }); 17 | } 18 | -------------------------------------------------------------------------------- /src/join.rs: -------------------------------------------------------------------------------- 1 | #![allow(non_snake_case)] 2 | 3 | /// Awaits multiple futures simultaneously, returning all results once complete. 4 | /// 5 | /// While `join!(a, b)` is similar to `(a.await, b.await)`, 6 | /// `join!` polls both futures concurrently and therefore is more efficent. 7 | /// 8 | /// This macro is only usable inside of async functions, closures, and blocks. 9 | /// 10 | /// # Examples 11 | /// 12 | /// ``` 13 | /// # futures::executor::block_on(async { 14 | /// use async_macros::join; 15 | /// use futures::future; 16 | /// 17 | /// let a = future::ready(1u8); 18 | /// let b = future::ready(2u8); 19 | /// 20 | /// assert_eq!(join!(a, b).await, (1, 2)); 21 | /// # }); 22 | /// ``` 23 | #[macro_export] 24 | macro_rules! join { 25 | ($($fut:ident),* $(,)?) => { { 26 | async { 27 | $( 28 | // Move future into a local so that it is pinned in one place and 29 | // is no longer accessible by the end user. 30 | let mut $fut = $crate::MaybeDone::new($fut); 31 | )* 32 | $crate::utils::poll_fn(move |cx| { 33 | use $crate::utils::future::Future; 34 | use $crate::utils::task::Poll; 35 | use $crate::utils::pin::Pin; 36 | 37 | let mut all_done = true; 38 | $( 39 | let fut = unsafe { Pin::new_unchecked(&mut $fut) }; 40 | all_done &= Future::poll(fut, cx).is_ready(); 41 | )* 42 | if all_done { 43 | Poll::Ready(($( 44 | unsafe { Pin::new_unchecked(&mut $fut) }.take().unwrap(), 45 | )*)) 46 | } else { 47 | Poll::Pending 48 | } 49 | }).await 50 | } 51 | } } 52 | } 53 | -------------------------------------------------------------------------------- /src/join_stream.rs: -------------------------------------------------------------------------------- 1 | use std::pin::Pin; 2 | use std::task::{Context, Poll}; 3 | 4 | use futures_core::Stream; 5 | 6 | /// A stream joining two or more streams. 7 | /// 8 | /// This stream is returned by `join!`. 9 | #[derive(Debug)] 10 | pub struct JoinStream { 11 | left: L, 12 | right: R, 13 | } 14 | 15 | impl Unpin for JoinStream {} 16 | 17 | impl JoinStream { 18 | #[doc(hidden)] 19 | pub fn new(left: L, right: R) -> Self { 20 | Self { left, right } 21 | } 22 | } 23 | 24 | impl Stream for JoinStream 25 | where 26 | L: Stream + Unpin, 27 | R: Stream + Unpin, 28 | { 29 | type Item = T; 30 | 31 | fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { 32 | if let Poll::Ready(Some(item)) = Pin::new(&mut self.left).poll_next(cx) { 33 | // The first stream made progress. The JoinStream needs to be polled 34 | // again to check the progress of the second stream. 35 | cx.waker().wake_by_ref(); 36 | Poll::Ready(Some(item)) 37 | } else { 38 | Pin::new(&mut self.right).poll_next(cx) 39 | } 40 | } 41 | } 42 | 43 | /// Combines multiple streams into a single stream of all their outputs. 44 | /// 45 | /// This macro is only usable inside of async functions, closures, and blocks. 46 | /// 47 | /// # Examples 48 | /// 49 | /// ``` 50 | /// # futures::executor::block_on(async { 51 | /// use async_macros::join_stream as join; 52 | /// use futures::stream::{self, StreamExt}; 53 | /// use futures::future::ready; 54 | /// 55 | /// let a = stream::once(ready(1u8)); 56 | /// let b = stream::once(ready(2u8)); 57 | /// let c = stream::once(ready(3u8)); 58 | /// 59 | /// let mut s = join!(a, b, c); 60 | /// 61 | /// assert_eq!(s.next().await, Some(1u8)); 62 | /// assert_eq!(s.next().await, Some(2u8)); 63 | /// assert_eq!(s.next().await, Some(3u8)); 64 | /// assert_eq!(s.next().await, None); 65 | /// # }); 66 | /// ``` 67 | #[macro_export] 68 | macro_rules! join_stream { 69 | ($stream1:ident, $stream2:ident, $($stream:ident),* $(,)?) => {{ 70 | let joined = $crate::JoinStream::new($stream1, $stream2); 71 | $( 72 | let joined = $crate::JoinStream::new(joined, $stream); 73 | )* 74 | joined 75 | }}; 76 | } 77 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Macros for async-std. 2 | //! 3 | //! # Examples 4 | //! 5 | //! ``` 6 | //! # futures::executor::block_on(async { 7 | //! use async_macros::join; 8 | //! use futures::future; 9 | //! 10 | //! let a = future::ready(1u8); 11 | //! let b = future::ready(2u8); 12 | //! 13 | //! assert_eq!(join!(a, b).await, (1, 2)); 14 | //! # }); 15 | //! ``` 16 | 17 | #![forbid(future_incompatible, rust_2018_idioms)] 18 | #![deny(missing_debug_implementations, nonstandard_style)] 19 | #![warn(missing_docs, unreachable_pub)] 20 | #![cfg_attr(test, deny(warnings))] 21 | 22 | mod join; 23 | mod join_stream; 24 | mod maybe_done; 25 | mod poll_fn; 26 | mod ready; 27 | mod select; 28 | mod try_join; 29 | mod try_select; 30 | 31 | pub use join_stream::JoinStream; 32 | pub use maybe_done::MaybeDone; 33 | 34 | /// Helper re-exports for use in macros. 35 | pub mod utils { 36 | pub use super::poll_fn::poll_fn; 37 | pub use core::{future, pin, result, task}; 38 | } 39 | -------------------------------------------------------------------------------- /src/maybe_done.rs: -------------------------------------------------------------------------------- 1 | //! A type that wraps a future to keep track of its completion status. 2 | //! 3 | //! This implementation was taken from the original `macro_rules` `join/try_join` 4 | //! macros in the `futures-preview` crate. 5 | 6 | use core::future::Future; 7 | use core::mem; 8 | use core::pin::Pin; 9 | 10 | use futures_core::ready; 11 | use futures_core::task::{Context, Poll}; 12 | 13 | /// A future that may have completed. 14 | #[derive(Debug)] 15 | pub enum MaybeDone { 16 | /// A not-yet-completed future 17 | Future(Fut), 18 | /// The output of the completed future 19 | Done(Fut::Output), 20 | /// The empty variant after the result of a [`MaybeDone`] has been 21 | /// taken using the [`take`](MaybeDone::take) method. 22 | Gone, 23 | } 24 | 25 | impl MaybeDone { 26 | /// Create a new instance of `MaybeDone`. 27 | pub fn new(future: Fut) -> MaybeDone { 28 | Self::Future(future) 29 | } 30 | 31 | /// Returns an [`Option`] containing a reference to the output of the future. 32 | /// The output of this method will be [`Some`] if and only if the inner 33 | /// future has been completed and [`take`](MaybeDone::take) 34 | /// has not yet been called. 35 | #[inline] 36 | pub fn output(self: Pin<&Self>) -> Option<&Fut::Output> { 37 | let this = self.get_ref(); 38 | match this { 39 | MaybeDone::Done(res) => Some(res), 40 | _ => None, 41 | } 42 | } 43 | 44 | /// Returns an [`Option`] containing a mutable reference to the output of the future. 45 | /// The output of this method will be [`Some`] if and only if the inner 46 | /// future has been completed and [`take`](MaybeDone::take) 47 | /// has not yet been called. 48 | #[inline] 49 | pub fn output_mut(self: Pin<&mut Self>) -> Option<&mut Fut::Output> { 50 | unsafe { 51 | let this = self.get_unchecked_mut(); 52 | match this { 53 | MaybeDone::Done(res) => Some(res), 54 | _ => None, 55 | } 56 | } 57 | } 58 | 59 | /// Attempt to take the output of a `MaybeDone` without driving it 60 | /// towards completion. 61 | #[inline] 62 | pub fn take(self: Pin<&mut Self>) -> Option { 63 | unsafe { 64 | let this = self.get_unchecked_mut(); 65 | match this { 66 | MaybeDone::Done(_) => {} 67 | MaybeDone::Future(_) | MaybeDone::Gone => return None, 68 | }; 69 | if let MaybeDone::Done(output) = mem::replace(this, MaybeDone::Gone) { 70 | Some(output) 71 | } else { 72 | unreachable!() 73 | } 74 | } 75 | } 76 | 77 | // fn ok(self) -> Option {} 78 | } 79 | 80 | impl Future for MaybeDone { 81 | type Output = (); 82 | 83 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 84 | let res = unsafe { 85 | match Pin::as_mut(&mut self).get_unchecked_mut() { 86 | MaybeDone::Future(a) => ready!(Pin::new_unchecked(a).poll(cx)), 87 | MaybeDone::Done(_) => return Poll::Ready(()), 88 | MaybeDone::Gone => panic!("MaybeDone polled after value taken"), 89 | } 90 | }; 91 | self.set(MaybeDone::Done(res)); 92 | Poll::Ready(()) 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/poll_fn.rs: -------------------------------------------------------------------------------- 1 | //! Definition of the `PollFn` adapter combinator 2 | 3 | use core::fmt; 4 | use core::pin::Pin; 5 | use futures_core::future::Future; 6 | use futures_core::task::{Context, Poll}; 7 | 8 | /// Future for the [`poll_fn`] function. 9 | #[must_use = "futures do nothing unless you `.await` or poll them"] 10 | pub struct PollFn { 11 | f: F, 12 | } 13 | 14 | impl Unpin for PollFn {} 15 | 16 | /// Creates a new future wrapping around a function returning [`Poll`]. 17 | /// 18 | /// Polling the returned future delegates to the wrapped function. 19 | /// 20 | /// # Examples 21 | /// 22 | /// ``` 23 | /// # futures::executor::block_on(async { 24 | /// use futures::future::poll_fn; 25 | /// use futures::task::{Context, Poll}; 26 | /// 27 | /// fn read_line(_cx: &mut Context<'_>) -> Poll { 28 | /// Poll::Ready("Hello, World!".into()) 29 | /// } 30 | /// 31 | /// let read_future = poll_fn(read_line); 32 | /// assert_eq!(read_future.await, "Hello, World!".to_owned()); 33 | /// # }); 34 | /// ``` 35 | pub fn poll_fn(f: F) -> PollFn 36 | where 37 | F: FnMut(&mut Context<'_>) -> Poll, 38 | { 39 | PollFn { f } 40 | } 41 | 42 | impl fmt::Debug for PollFn { 43 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 44 | f.debug_struct("PollFn").finish() 45 | } 46 | } 47 | 48 | impl Future for PollFn 49 | where 50 | F: FnMut(&mut Context<'_>) -> Poll, 51 | { 52 | type Output = T; 53 | 54 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 55 | (&mut self.f)(cx) 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/ready.rs: -------------------------------------------------------------------------------- 1 | /// Extracts the successful type of a `Poll`. 2 | /// 3 | /// This macro bakes in propagation of `Pending` signals by returning early. 4 | #[macro_export] 5 | macro_rules! ready { 6 | ($e:expr $(,)?) => { 7 | match $e { 8 | $crate::utils::task::Poll::Ready(t) => t, 9 | $crate::utils::task::Poll::Pending => return $crate::utils::task::Poll::Pending, 10 | } 11 | }; 12 | } 13 | -------------------------------------------------------------------------------- /src/select.rs: -------------------------------------------------------------------------------- 1 | #![allow(non_snake_case)] 2 | 3 | /// Waits for either one of several similarly-typed futures to complete. 4 | /// 5 | /// Awaits multiple futures simultaneously, returning all results once complete. 6 | /// 7 | /// This function will return a new future which awaits for either one of both 8 | /// futures to complete. If multiple futures are completed at the same time, 9 | /// resolution will occur in the order that they have been passed. 10 | /// 11 | /// Note that this macro consumes all futures passed, and once a future is 12 | /// completed, all other futures are dropped. 13 | /// 14 | /// This macro is only usable inside of async functions, closures, and blocks. 15 | /// 16 | /// # Examples 17 | /// 18 | /// ``` 19 | /// # futures::executor::block_on(async { 20 | /// use async_macros::select; 21 | /// use futures::future; 22 | /// 23 | /// let a = future::pending(); 24 | /// let b = future::ready(1u8); 25 | /// let c = future::ready(2u8); 26 | /// 27 | /// assert_eq!(select!(a, b, c).await, 1u8); 28 | /// # }); 29 | /// ``` 30 | #[macro_export] 31 | macro_rules! select { 32 | ($($fut:ident),* $(,)?) => { { 33 | async { 34 | $( 35 | // Move future into a local so that it is pinned in one place and 36 | // is no longer accessible by the end user. 37 | let mut $fut = $crate::MaybeDone::new($fut); 38 | )* 39 | $crate::utils::poll_fn(move |cx| { 40 | use $crate::utils::future::Future; 41 | use $crate::utils::task::Poll; 42 | use $crate::utils::pin::Pin; 43 | 44 | $( 45 | let fut = unsafe { Pin::new_unchecked(&mut $fut) }; 46 | if Future::poll(fut, cx).is_ready() { 47 | let fut = unsafe { Pin::new_unchecked(&mut $fut) }; 48 | let output = fut.take().unwrap(); 49 | return Poll::Ready(output); 50 | } 51 | )* 52 | 53 | // If nothing matched we return Pending. 54 | Poll::Pending 55 | }).await 56 | } 57 | } } 58 | } 59 | -------------------------------------------------------------------------------- /src/try_join.rs: -------------------------------------------------------------------------------- 1 | /// Awaits multiple fallible futures simultaneously, returning all results once 2 | /// complete. 3 | /// 4 | /// `try_join!` is similar to [`join!`], but completes immediately if any of 5 | /// the futures return an error. 6 | /// 7 | /// This macro is only usable inside of async functions, closures, and blocks. 8 | /// 9 | /// # Examples 10 | /// 11 | /// When used on multiple futures that return `Ok`, `try_join!` will return 12 | /// `Ok` of a tuple of the values: 13 | /// 14 | /// ``` 15 | /// # futures::executor::block_on(async { 16 | /// use async_macros::try_join; 17 | /// use futures::future; 18 | /// 19 | /// let a = future::ready(Ok::(1)); 20 | /// let b = future::ready(Ok::(2)); 21 | /// 22 | /// assert_eq!(try_join!(a, b).await, Ok((1, 2))); 23 | /// # }); 24 | /// ``` 25 | /// 26 | /// If one of the futures resolves to an error, `try_join!` will return 27 | /// that error: 28 | /// 29 | /// ``` 30 | /// # futures::executor::block_on(async { 31 | /// use async_macros::try_join; 32 | /// use futures::future; 33 | /// 34 | /// let a = future::ready(Ok::(1)); 35 | /// let b = future::ready(Err::(2)); 36 | /// 37 | /// assert_eq!(try_join!(a, b).await, Err(2)); 38 | /// # }); 39 | /// ``` 40 | #[macro_export] 41 | macro_rules! try_join { 42 | ($($fut:ident),* $(,)?) => { { 43 | async { 44 | use $crate::utils::future::Future; 45 | use $crate::utils::pin::Pin; 46 | use $crate::utils::poll_fn; 47 | use $crate::utils::result::Result; 48 | use $crate::utils::task::Poll; 49 | 50 | $( 51 | // Move future into a local so that it is pinned in one place and 52 | // is no longer accessible by the end user. 53 | let mut $fut = $crate::MaybeDone::new($fut); 54 | )* 55 | 56 | let res: Result<_, _> = poll_fn(move |cx| { 57 | let mut all_done = true; 58 | $( 59 | let fut = unsafe { Pin::new_unchecked(&mut $fut) }; 60 | if Future::poll(fut, cx).is_pending() { 61 | all_done = false; 62 | } else if unsafe { Pin::new_unchecked(&mut $fut) }.output_mut().unwrap().is_err() { 63 | // `.err().unwrap()` rather than `.unwrap_err()` so that we don't introduce 64 | // a `T: Debug` bound. 65 | return Poll::Ready( 66 | Result::Err(unsafe { Pin::new_unchecked(&mut $fut) } 67 | .take() 68 | .unwrap() 69 | .err() 70 | .unwrap() 71 | )); 72 | } 73 | )* 74 | if all_done { 75 | let res = ($( 76 | // `.ok().unwrap()` rather than `.unwrap()` so that we don't introduce 77 | // an `E: Debug` bound. 78 | unsafe { Pin::new_unchecked(&mut $fut) } 79 | .take() 80 | .unwrap() 81 | .ok() 82 | .unwrap(), 83 | )*); 84 | Poll::Ready(Result::Ok(res)) 85 | } else { 86 | Poll::Pending 87 | } 88 | }).await; 89 | res 90 | } 91 | } } 92 | } 93 | -------------------------------------------------------------------------------- /src/try_select.rs: -------------------------------------------------------------------------------- 1 | #![allow(non_snake_case)] 2 | 3 | /// Waits for either one of several similarly-typed futures to complete. 4 | /// 5 | /// Awaits multiple futures simultaneously, returning all results once complete. 6 | /// 7 | /// `try_select!` is similar to [`select!`], but keeps going if a future 8 | /// resolved to an error until all futures have been resolved. In which case 9 | /// the error of the last item in the list will be returned. 10 | /// 11 | /// This macro is only usable inside of async functions, closures, and blocks. 12 | /// 13 | /// # Examples 14 | /// 15 | /// ``` 16 | /// # futures::executor::block_on(async { 17 | /// # async fn main() -> Result<(), std::io::Error> { 18 | /// use async_macros::try_select; 19 | /// use futures::future; 20 | /// use std::io::{Error, ErrorKind}; 21 | /// 22 | /// let a = future::pending::>(); 23 | /// let b = future::ready(Err(Error::from(ErrorKind::Other))); 24 | /// let c = future::ready(Ok(1u8)); 25 | /// 26 | /// assert_eq!(try_select!(a, b, c).await?, 1u8); 27 | /// # Ok(()) 28 | /// # } 29 | /// # main().await.unwrap(); 30 | /// # }); 31 | /// ``` 32 | #[macro_export] 33 | macro_rules! try_select { 34 | ($($fut:ident),+ $(,)?) => { { 35 | async { 36 | use $crate::utils::future::Future; 37 | use $crate::utils::pin::Pin; 38 | use $crate::utils::poll_fn; 39 | use $crate::utils::result::Result; 40 | use $crate::utils::task::Poll; 41 | 42 | $( 43 | // Move future into a local so that it is pinned in one place and 44 | // is no longer accessible by the end user. 45 | let mut $fut = $crate::MaybeDone::new($fut); 46 | )* 47 | 48 | let res: Result<_, _> = poll_fn(move |cx| { 49 | let mut all_done = true; 50 | 51 | $( 52 | let fut = unsafe { Pin::new_unchecked(&mut $fut) }; 53 | if Future::poll(fut, cx).is_ready() { 54 | let fut = Pin::new(&$fut); 55 | if fut.output().unwrap().is_ok() { 56 | let fut = unsafe { Pin::new_unchecked(&mut $fut) }; 57 | let res = fut.take().unwrap(); 58 | return Poll::Ready(res); 59 | } else { 60 | all_done = false; 61 | } 62 | } else { 63 | all_done = false; 64 | } 65 | )* 66 | 67 | if all_done { 68 | // We need to iterate over all items to get the last error. 69 | let mut err = None; 70 | $( 71 | if err.is_none() { 72 | let fut = unsafe { Pin::new_unchecked(&mut $fut) }; 73 | err = Some(fut.take().unwrap()); 74 | } 75 | )* 76 | return Poll::Ready(err.unwrap()); 77 | } else { 78 | Poll::Pending 79 | } 80 | }).await; 81 | res 82 | } 83 | } } 84 | } 85 | -------------------------------------------------------------------------------- /tests/test.rs: -------------------------------------------------------------------------------- 1 | use std::error::Error; 2 | 3 | #[test] 4 | fn should_work() -> Result<(), Box> { 5 | Ok(()) 6 | } 7 | --------------------------------------------------------------------------------