├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── pull_request_template.md └── workflows │ └── ci.yml ├── .gitignore ├── .mergify.yml ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Cargo.lock ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── check.sh ├── deny.toml ├── examples └── example.rs └── src ├── lib.rs └── promise.rs /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @emilk 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Device:** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Additional context** 32 | Add any other context about the problem here. 33 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: enhancement 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | ### Checklist 2 | 3 | * [ ] I have read the [Contributor Guide](../../CONTRIBUTING.md) 4 | * [ ] I have read and agree to the [Code of Conduct](../../CODE_OF_CONDUCT.md) 5 | * [ ] I have added a description of my changes and why I'd like them included in the section below 6 | 7 | ### Description of Changes 8 | 9 | Describe your changes here 10 | 11 | ### Related Issues 12 | 13 | List related issues here 14 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | # TODO: Replace this line with the commented ones to actually run the action in your repo(s) 2 | on: 3 | push: 4 | branches: 5 | - main 6 | tags: 7 | - "*" 8 | pull_request: 9 | 10 | name: CI 11 | jobs: 12 | lint: 13 | name: Lint 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v2 17 | - uses: actions-rs/toolchain@v1 18 | with: 19 | toolchain: stable 20 | override: true 21 | 22 | # make sure all code has been formatted with rustfmt 23 | - name: check rustfmt 24 | run: | 25 | rustup component add rustfmt 26 | cargo fmt -- --check --color always 27 | 28 | # run clippy to verify we have no warnings 29 | - run: cargo fetch 30 | - name: cargo clippy 31 | run: | 32 | rustup component add clippy 33 | cargo clippy --all-targets --features "tokio" -- -D warnings 34 | 35 | test: 36 | name: Test 37 | strategy: 38 | matrix: 39 | os: [ubuntu-latest, windows-latest, macOS-latest] 40 | runs-on: ${{ matrix.os }} 41 | steps: 42 | - uses: actions/checkout@v2 43 | - uses: actions-rs/toolchain@v1 44 | with: 45 | toolchain: stable 46 | override: true 47 | - run: cargo fetch 48 | - name: cargo test build 49 | # Note the use of release here means longer compile time, but much 50 | # faster test execution time. If you don't have any heavy tests it 51 | # might be faster to take off release and just compile in debug 52 | run: cargo build --tests --release 53 | - name: cargo test smol 54 | run: cargo test --release -F smol -F smol_tick_poll 55 | - name: cargo test tokio 56 | run: cargo test --release -F tokio 57 | - name: cargo test async-std 58 | run: cargo test --release -F async-std 59 | 60 | # TODO: Remove this check if you don't use cargo-deny in the repo 61 | deny-check: 62 | name: cargo-deny 63 | runs-on: ubuntu-latest 64 | steps: 65 | - uses: actions/checkout@v2 66 | - uses: EmbarkStudios/cargo-deny-action@v1 67 | 68 | # TODO: Remove this check if you don't publish the crate(s) from this repo 69 | publish-check: 70 | name: Publish Check 71 | runs-on: ubuntu-latest 72 | steps: 73 | - uses: actions/checkout@v2 74 | - uses: actions-rs/toolchain@v1 75 | with: 76 | toolchain: stable 77 | override: true 78 | - run: cargo fetch 79 | - name: cargo publish check 80 | run: cargo publish --dry-run 81 | 82 | # TODO: Remove this job if you don't publish the crate(s) from this repo 83 | # You must add a crates.io API token to your GH secrets and name it CRATES_IO_TOKEN 84 | publish: 85 | name: Publish 86 | needs: [test, deny-check, publish-check] 87 | runs-on: ubuntu-latest 88 | if: startsWith(github.ref, 'refs/tags/') 89 | steps: 90 | - uses: actions/checkout@v2 91 | - uses: actions-rs/toolchain@v1 92 | with: 93 | toolchain: stable 94 | override: true 95 | - run: cargo fetch 96 | - name: cargo publish 97 | env: 98 | CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_TOKEN }} 99 | run: cargo publish 100 | 101 | # TODO: Remove this job if you don't release binaries 102 | # Replace occurances of $BIN_NAME with the name of your binary 103 | release: 104 | name: Release 105 | needs: [test, deny-check] 106 | if: startsWith(github.ref, 'refs/tags/') 107 | strategy: 108 | matrix: 109 | os: [ubuntu-latest, macOS-latest, windows-latest] 110 | include: 111 | - os: ubuntu-latest 112 | rust: stable 113 | target: x86_64-unknown-linux-musl 114 | bin: $BIN_NAME 115 | # We don't enable the progress feature when targeting 116 | # musl since there are some dependencies on shared libs 117 | features: "" 118 | - os: windows-latest 119 | rust: stable 120 | target: x86_64-pc-windows-msvc 121 | bin: $BIN_NAME.exe 122 | features: progress 123 | - os: macOS-latest 124 | rust: stable 125 | target: x86_64-apple-darwin 126 | bin: $BIN_NAME 127 | features: progress 128 | runs-on: ${{ matrix.os }} 129 | steps: 130 | - name: Install stable toolchain 131 | uses: actions-rs/toolchain@v1 132 | with: 133 | toolchain: ${{ matrix.rust }} 134 | override: true 135 | target: ${{ matrix.target }} 136 | - name: Install musl tools 137 | if: matrix.os == 'ubuntu-latest' 138 | run: sudo apt-get install -y musl-tools 139 | - name: Checkout 140 | uses: actions/checkout@v2 141 | - run: cargo fetch --target ${{ matrix.target }} 142 | - name: Release build 143 | shell: bash 144 | run: | 145 | if [ "${{ matrix.features }}" != "" ]; then 146 | cargo build --release --target ${{ matrix.target }} --features ${{ matrix.features }} 147 | else 148 | cargo build --release --target ${{ matrix.target }} 149 | fi 150 | - name: Package 151 | shell: bash 152 | run: | 153 | name=$BIN_NAME 154 | tag=$(git describe --tags --abbrev=0) 155 | release_name="$name-$tag-${{ matrix.target }}" 156 | release_tar="${release_name}.tar.gz" 157 | mkdir "$release_name" 158 | 159 | if [ "${{ matrix.target }}" != "x86_64-pc-windows-msvc" ]; then 160 | strip "target/${{ matrix.target }}/release/${{ matrix.bin }}" 161 | fi 162 | 163 | cp "target/${{ matrix.target }}/release/${{ matrix.bin }}" "$release_name/" 164 | cp README.md LICENSE-APACHE LICENSE-MIT "$release_name/" 165 | tar czvf "$release_tar" "$release_name" 166 | 167 | rm -r "$release_name" 168 | 169 | # Windows environments in github actions don't have the gnu coreutils installed, 170 | # which includes the shasum exe, so we just use powershell instead 171 | if [ "${{ matrix.os }}" == "windows-latest" ]; then 172 | echo "(Get-FileHash \"${release_tar}\" -Algorithm SHA256).Hash | Out-File -Encoding ASCII -NoNewline \"${release_tar}.sha256\"" | pwsh -c - 173 | else 174 | echo -n "$(shasum -ba 256 "${release_tar}" | cut -d " " -f 1)" > "${release_tar}.sha256" 175 | fi 176 | - name: Publish 177 | uses: softprops/action-gh-release@v1 178 | with: 179 | draft: true 180 | files: "$BIN_NAME*" 181 | env: 182 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 183 | 184 | # TODO: Remove this job if you don't publish container images on each release 185 | # TODO: Create a repository on DockerHub with the same name as the GitHub repo 186 | # TODO: Add the new repo to the buildbot group with read & write permissions 187 | # TODO: Add the embarkbot dockerhub password to the repo secrets as DOCKERHUB_PASSWORD 188 | publish-container-images: 189 | name: Publish container images 190 | runs-on: ubuntu-latest 191 | if: startsWith(github.ref, 'refs/tags/') 192 | needs: [test, deny-check] 193 | steps: 194 | - name: Checkout 195 | uses: actions/checkout@v2 196 | - name: Set up QEMU 197 | uses: docker/setup-qemu-action@v1 198 | - name: Set up Docker Buildx 199 | uses: docker/setup-buildx-action@v1 200 | - name: Login to Dockerhub 201 | uses: docker/login-action@v1 202 | with: 203 | username: embarkbot 204 | password: ${{ secrets.DOCKERHUB_PASSWORD }} 205 | - name: Docker meta 206 | id: docker_meta 207 | uses: crazy-max/ghaction-docker-meta@v1 208 | with: 209 | images: embarkstudios/${{ github.event.repository.name }} 210 | tag-semver: | 211 | {{version}} 212 | {{major}}.{{minor}} 213 | - name: Build and push 214 | uses: docker/build-push-action@v2 215 | with: 216 | context: . 217 | file: ./Dockerfile 218 | push: true 219 | tags: ${{ steps.docker_meta.outputs.tags }} 220 | labels: ${{ steps.docker_meta.outputs.labels }} 221 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | .vscode -------------------------------------------------------------------------------- /.mergify.yml: -------------------------------------------------------------------------------- 1 | pull_request_rules: 2 | - name: automatic merge when CI passes and 1 reviews 3 | conditions: 4 | - "#approved-reviews-by>=1" 5 | - "#review-requested=0" 6 | - "#changes-requested-reviews-by=0" 7 | - base=main 8 | actions: 9 | merge: 10 | method: squash 11 | - name: delete head branch after merge 12 | conditions: 13 | - merged 14 | actions: 15 | delete_head_branch: {} 16 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 5 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 6 | 7 | 8 | ## [Unreleased] 9 | - Disable all blocking functions on Wasm, since you are not allowed to block in a browser 10 | 11 | 12 | ## [0.3.0] - 2023-08-27 13 | ### Added 14 | - Support `async-std` executor [#15](https://github.com/EmbarkStudios/poll-promise/pull/15) 15 | - `smol` feature to enable the use of [`smol`](https://github.com/smol-rs/smol) 16 | - refactor `Promise::spawn_async` into two new functions, `Promise::spawn_async` and `Promise::spawn_local` 17 | - `smol_tick_poll` feature to automatically tick the smol executor when polling promises 18 | 19 | ### Changed 20 | - `spawn_async` is now called `spawn_local` on web. 21 | 22 | 23 | ## [0.2.1] - 2023-09-29 24 | ### Fixed 25 | - Undefined behavior in `PromiseImpl::poll` and `PromiseImpl::block_until_ready` 26 | 27 | 28 | ## [0.2.0] - 2022-10-25 29 | ### Added 30 | - `web` feature to enable `Promise::spawn_async` using [`wasm_bindgen_futures::spawn_local`](https://rustwasm.github.io/wasm-bindgen/api/wasm_bindgen_futures/fn.spawn_local.html). 31 | - Add `Promise::abort` to abort the associated async task, if any. 32 | 33 | 34 | ## [0.1.0] - 2022-01-10 35 | ### Added 36 | - Initial commit - add the `Promise` type. 37 | 38 | 39 | [Unreleased]: https://github.com/EmbarkStudios/poll-promise/compare/0.3.0...HEAD 40 | [0.3.0]: https://github.com/EmbarkStudios/poll-promise/releases/tag/0.3.0 41 | [0.2.1]: https://github.com/EmbarkStudios/poll-promise/releases/tag/0.2.1 42 | [0.2.0]: https://github.com/EmbarkStudios/poll-promise/releases/tag/0.2.0 43 | [0.1.0]: https://github.com/EmbarkStudios/poll-promise/releases/tag/0.1.0 44 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at opensource@embark-studios.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Embark Contributor Guidelines 2 | 3 | Welcome! This project is created by the team at [Embark Studios](https://embark.games). We're glad you're interested in contributing! We welcome contributions from people of all backgrounds who are interested in making great software with us. 4 | 5 | At Embark, we aspire to empower everyone to create interactive experiences. To do this, we're exploring and pushing the boundaries of new technologies, and sharing our learnings with the open source community. 6 | 7 | If you have ideas for collaboration, email us at opensource@embark-studios.com. 8 | 9 | We're also hiring full-time engineers to work with us in Stockholm! Check out our current job postings [here](https://www.embark-studios.com/jobs). 10 | 11 | ## Issues 12 | 13 | ### Feature Requests 14 | 15 | If you have ideas or how to improve our projects, you can suggest features by opening a GitHub issue. Make sure to include details about the feature or change, and describe any uses cases it would enable. 16 | 17 | Feature requests will be tagged as `enhancement` and their status will be updated in the comments of the issue. 18 | 19 | ### Bugs 20 | 21 | When reporting a bug or unexpected behaviour in a project, make sure your issue describes steps to reproduce the behaviour, including the platform you were using, what steps you took, and any error messages. 22 | 23 | Reproducible bugs will be tagged as `bug` and their status will be updated in the comments of the issue. 24 | 25 | ### Wontfix 26 | 27 | Issues will be closed and tagged as `wontfix` if we decide that we do not wish to implement it, usually due to being misaligned with the project vision or out of scope. We will comment on the issue with more detailed reasoning. 28 | 29 | ## Contribution Workflow 30 | 31 | ### Open Issues 32 | 33 | If you're ready to contribute, start by looking at our open issues tagged as [`help wanted`](../../issues?q=is%3Aopen+is%3Aissue+label%3A"help+wanted") or [`good first issue`](../../issues?q=is%3Aopen+is%3Aissue+label%3A"good+first+issue"). 34 | 35 | You can comment on the issue to let others know you're interested in working on it or to ask questions. 36 | 37 | ### Making Changes 38 | 39 | 1. Fork the repository. 40 | 41 | 2. Create a new feature branch. 42 | 43 | 3. Make your changes. Ensure that there are no build errors by running the project with your changes locally. 44 | 45 | 4. Open a pull request with a name and description of what you did. You can read more about working with pull requests on GitHub [here](https://help.github.com/en/articles/creating-a-pull-request-from-a-fork). 46 | 47 | 5. A maintainer will review your pull request and may ask you to make changes. 48 | 49 | ## Code Guidelines 50 | 51 | ### Rust 52 | 53 | You can read about our standards and recommendations for working with Rust [here](https://github.com/EmbarkStudios/rust-ecosystem/blob/main/guidelines.md). 54 | 55 | ### Python 56 | 57 | We recommend following [PEP8 conventions](https://www.python.org/dev/peps/pep-0008/) when working with Python modules. 58 | 59 | ### JavaScript & TypeScript 60 | 61 | We use [Prettier](https://prettier.io/) with the default settings to auto-format our JavaScript and TypeScript code. 62 | 63 | ## Licensing 64 | 65 | Unless otherwise specified, all Embark open source projects shall comply with the Rust standard licensing model (MIT + Apache 2.0) and are thereby licensed under a dual license, allowing licensees to choose either MIT OR Apache-2.0 at their option. 66 | 67 | ## Contributor Terms 68 | 69 | Thank you for your interest in Embark Studios’ open source project. By providing a contribution (new or modified code, other input, feedback or suggestions etc.) you agree to these Contributor Terms. 70 | 71 | You confirm that each of your contributions has been created by you and that you are the copyright owner. You also confirm that you have the right to provide the contribution to us and that you do it under the Rust dual licence model (MIT + Apache 2.0). 72 | 73 | If you want to contribute something that is not your original creation, you may submit it to Embark Studios separately from any contribution, including details of its source and of any license or other restriction (such as related patents, trademarks, agreements etc.) 74 | 75 | Please also note that our projects are released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md) to ensure that they are welcoming places for everyone to contribute. By participating in any Embark Studios open source project, you agree to keep to the Contributor Code of Conduct. 76 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "async-channel" 7 | version = "1.9.0" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "81953c529336010edd6d8e358f886d9581267795c61b19475b71314bffa46d35" 10 | dependencies = [ 11 | "concurrent-queue", 12 | "event-listener", 13 | "futures-core", 14 | ] 15 | 16 | [[package]] 17 | name = "async-executor" 18 | version = "1.5.0" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | checksum = "17adb73da160dfb475c183343c8cccd80721ea5a605d3eb57125f0a7b7a92d0b" 21 | dependencies = [ 22 | "async-lock", 23 | "async-task", 24 | "concurrent-queue", 25 | "fastrand", 26 | "futures-lite", 27 | "slab", 28 | ] 29 | 30 | [[package]] 31 | name = "async-fs" 32 | version = "1.6.0" 33 | source = "registry+https://github.com/rust-lang/crates.io-index" 34 | checksum = "279cf904654eeebfa37ac9bb1598880884924aab82e290aa65c9e77a0e142e06" 35 | dependencies = [ 36 | "async-lock", 37 | "autocfg", 38 | "blocking", 39 | "futures-lite", 40 | ] 41 | 42 | [[package]] 43 | name = "async-global-executor" 44 | version = "2.3.1" 45 | source = "registry+https://github.com/rust-lang/crates.io-index" 46 | checksum = "f1b6f5d7df27bd294849f8eec66ecfc63d11814df7a4f5d74168a2394467b776" 47 | dependencies = [ 48 | "async-channel", 49 | "async-executor", 50 | "async-io", 51 | "async-lock", 52 | "blocking", 53 | "futures-lite", 54 | "once_cell", 55 | ] 56 | 57 | [[package]] 58 | name = "async-io" 59 | version = "1.13.0" 60 | source = "registry+https://github.com/rust-lang/crates.io-index" 61 | checksum = "0fc5b45d93ef0529756f812ca52e44c221b35341892d3dcc34132ac02f3dd2af" 62 | dependencies = [ 63 | "async-lock", 64 | "autocfg", 65 | "cfg-if", 66 | "concurrent-queue", 67 | "futures-lite", 68 | "log", 69 | "parking", 70 | "polling", 71 | "rustix", 72 | "slab", 73 | "socket2", 74 | "waker-fn", 75 | ] 76 | 77 | [[package]] 78 | name = "async-lock" 79 | version = "2.8.0" 80 | source = "registry+https://github.com/rust-lang/crates.io-index" 81 | checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b" 82 | dependencies = [ 83 | "event-listener", 84 | ] 85 | 86 | [[package]] 87 | name = "async-net" 88 | version = "1.8.0" 89 | source = "registry+https://github.com/rust-lang/crates.io-index" 90 | checksum = "0434b1ed18ce1cf5769b8ac540e33f01fa9471058b5e89da9e06f3c882a8c12f" 91 | dependencies = [ 92 | "async-io", 93 | "blocking", 94 | "futures-lite", 95 | ] 96 | 97 | [[package]] 98 | name = "async-process" 99 | version = "1.7.0" 100 | source = "registry+https://github.com/rust-lang/crates.io-index" 101 | checksum = "7a9d28b1d97e08915212e2e45310d47854eafa69600756fc735fb788f75199c9" 102 | dependencies = [ 103 | "async-io", 104 | "async-lock", 105 | "autocfg", 106 | "blocking", 107 | "cfg-if", 108 | "event-listener", 109 | "futures-lite", 110 | "rustix", 111 | "signal-hook", 112 | "windows-sys 0.48.0", 113 | ] 114 | 115 | [[package]] 116 | name = "async-std" 117 | version = "1.12.0" 118 | source = "registry+https://github.com/rust-lang/crates.io-index" 119 | checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d" 120 | dependencies = [ 121 | "async-channel", 122 | "async-global-executor", 123 | "async-io", 124 | "async-lock", 125 | "crossbeam-utils", 126 | "futures-channel", 127 | "futures-core", 128 | "futures-io", 129 | "futures-lite", 130 | "gloo-timers", 131 | "kv-log-macro", 132 | "log", 133 | "memchr", 134 | "once_cell", 135 | "pin-project-lite", 136 | "pin-utils", 137 | "slab", 138 | "wasm-bindgen-futures", 139 | ] 140 | 141 | [[package]] 142 | name = "async-task" 143 | version = "4.4.1" 144 | source = "registry+https://github.com/rust-lang/crates.io-index" 145 | checksum = "b9441c6b2fe128a7c2bf680a44c34d0df31ce09e5b7e401fcca3faa483dbc921" 146 | 147 | [[package]] 148 | name = "atomic-waker" 149 | version = "1.1.2" 150 | source = "registry+https://github.com/rust-lang/crates.io-index" 151 | checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" 152 | 153 | [[package]] 154 | name = "autocfg" 155 | version = "1.1.0" 156 | source = "registry+https://github.com/rust-lang/crates.io-index" 157 | checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" 158 | 159 | [[package]] 160 | name = "bitflags" 161 | version = "1.3.2" 162 | source = "registry+https://github.com/rust-lang/crates.io-index" 163 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 164 | 165 | [[package]] 166 | name = "blocking" 167 | version = "1.3.0" 168 | source = "registry+https://github.com/rust-lang/crates.io-index" 169 | checksum = "3c67b173a56acffd6d2326fb7ab938ba0b00a71480e14902b2591c87bc5741e8" 170 | dependencies = [ 171 | "async-channel", 172 | "async-lock", 173 | "async-task", 174 | "atomic-waker", 175 | "fastrand", 176 | "futures-lite", 177 | ] 178 | 179 | [[package]] 180 | name = "bumpalo" 181 | version = "3.11.0" 182 | source = "registry+https://github.com/rust-lang/crates.io-index" 183 | checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d" 184 | 185 | [[package]] 186 | name = "cc" 187 | version = "1.0.83" 188 | source = "registry+https://github.com/rust-lang/crates.io-index" 189 | checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" 190 | dependencies = [ 191 | "libc", 192 | ] 193 | 194 | [[package]] 195 | name = "cfg-if" 196 | version = "1.0.0" 197 | source = "registry+https://github.com/rust-lang/crates.io-index" 198 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 199 | 200 | [[package]] 201 | name = "concurrent-queue" 202 | version = "2.3.0" 203 | source = "registry+https://github.com/rust-lang/crates.io-index" 204 | checksum = "f057a694a54f12365049b0958a1685bb52d567f5593b355fbf685838e873d400" 205 | dependencies = [ 206 | "crossbeam-utils", 207 | ] 208 | 209 | [[package]] 210 | name = "crossbeam-utils" 211 | version = "0.8.16" 212 | source = "registry+https://github.com/rust-lang/crates.io-index" 213 | checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" 214 | dependencies = [ 215 | "cfg-if", 216 | ] 217 | 218 | [[package]] 219 | name = "ctor" 220 | version = "0.1.26" 221 | source = "registry+https://github.com/rust-lang/crates.io-index" 222 | checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096" 223 | dependencies = [ 224 | "quote", 225 | "syn", 226 | ] 227 | 228 | [[package]] 229 | name = "document-features" 230 | version = "0.2.7" 231 | source = "registry+https://github.com/rust-lang/crates.io-index" 232 | checksum = "e493c573fce17f00dcab13b6ac057994f3ce17d1af4dc39bfd482b83c6eb6157" 233 | dependencies = [ 234 | "litrs", 235 | ] 236 | 237 | [[package]] 238 | name = "errno" 239 | version = "0.3.3" 240 | source = "registry+https://github.com/rust-lang/crates.io-index" 241 | checksum = "136526188508e25c6fef639d7927dfb3e0e3084488bf202267829cf7fc23dbdd" 242 | dependencies = [ 243 | "errno-dragonfly", 244 | "libc", 245 | "windows-sys 0.48.0", 246 | ] 247 | 248 | [[package]] 249 | name = "errno-dragonfly" 250 | version = "0.1.2" 251 | source = "registry+https://github.com/rust-lang/crates.io-index" 252 | checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" 253 | dependencies = [ 254 | "cc", 255 | "libc", 256 | ] 257 | 258 | [[package]] 259 | name = "event-listener" 260 | version = "2.5.3" 261 | source = "registry+https://github.com/rust-lang/crates.io-index" 262 | checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" 263 | 264 | [[package]] 265 | name = "fastrand" 266 | version = "1.9.0" 267 | source = "registry+https://github.com/rust-lang/crates.io-index" 268 | checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" 269 | dependencies = [ 270 | "instant", 271 | ] 272 | 273 | [[package]] 274 | name = "futures-channel" 275 | version = "0.3.28" 276 | source = "registry+https://github.com/rust-lang/crates.io-index" 277 | checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" 278 | dependencies = [ 279 | "futures-core", 280 | ] 281 | 282 | [[package]] 283 | name = "futures-core" 284 | version = "0.3.28" 285 | source = "registry+https://github.com/rust-lang/crates.io-index" 286 | checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" 287 | 288 | [[package]] 289 | name = "futures-io" 290 | version = "0.3.28" 291 | source = "registry+https://github.com/rust-lang/crates.io-index" 292 | checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" 293 | 294 | [[package]] 295 | name = "futures-lite" 296 | version = "1.13.0" 297 | source = "registry+https://github.com/rust-lang/crates.io-index" 298 | checksum = "49a9d51ce47660b1e808d3c990b4709f2f415d928835a17dfd16991515c46bce" 299 | dependencies = [ 300 | "fastrand", 301 | "futures-core", 302 | "futures-io", 303 | "memchr", 304 | "parking", 305 | "pin-project-lite", 306 | "waker-fn", 307 | ] 308 | 309 | [[package]] 310 | name = "gloo-timers" 311 | version = "0.2.6" 312 | source = "registry+https://github.com/rust-lang/crates.io-index" 313 | checksum = "9b995a66bb87bebce9a0f4a95aed01daca4872c050bfcb21653361c03bc35e5c" 314 | dependencies = [ 315 | "futures-channel", 316 | "futures-core", 317 | "js-sys", 318 | "wasm-bindgen", 319 | ] 320 | 321 | [[package]] 322 | name = "hermit-abi" 323 | version = "0.1.19" 324 | source = "registry+https://github.com/rust-lang/crates.io-index" 325 | checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" 326 | dependencies = [ 327 | "libc", 328 | ] 329 | 330 | [[package]] 331 | name = "hermit-abi" 332 | version = "0.3.3" 333 | source = "registry+https://github.com/rust-lang/crates.io-index" 334 | checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" 335 | 336 | [[package]] 337 | name = "instant" 338 | version = "0.1.12" 339 | source = "registry+https://github.com/rust-lang/crates.io-index" 340 | checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" 341 | dependencies = [ 342 | "cfg-if", 343 | ] 344 | 345 | [[package]] 346 | name = "io-lifetimes" 347 | version = "1.0.11" 348 | source = "registry+https://github.com/rust-lang/crates.io-index" 349 | checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" 350 | dependencies = [ 351 | "hermit-abi 0.3.3", 352 | "libc", 353 | "windows-sys 0.48.0", 354 | ] 355 | 356 | [[package]] 357 | name = "js-sys" 358 | version = "0.3.60" 359 | source = "registry+https://github.com/rust-lang/crates.io-index" 360 | checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47" 361 | dependencies = [ 362 | "wasm-bindgen", 363 | ] 364 | 365 | [[package]] 366 | name = "kv-log-macro" 367 | version = "1.0.7" 368 | source = "registry+https://github.com/rust-lang/crates.io-index" 369 | checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" 370 | dependencies = [ 371 | "log", 372 | ] 373 | 374 | [[package]] 375 | name = "libc" 376 | version = "0.2.147" 377 | source = "registry+https://github.com/rust-lang/crates.io-index" 378 | checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" 379 | 380 | [[package]] 381 | name = "linux-raw-sys" 382 | version = "0.3.8" 383 | source = "registry+https://github.com/rust-lang/crates.io-index" 384 | checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" 385 | 386 | [[package]] 387 | name = "litrs" 388 | version = "0.2.3" 389 | source = "registry+https://github.com/rust-lang/crates.io-index" 390 | checksum = "f9275e0933cf8bb20f008924c0cb07a0692fe54d8064996520bf998de9eb79aa" 391 | 392 | [[package]] 393 | name = "log" 394 | version = "0.4.17" 395 | source = "registry+https://github.com/rust-lang/crates.io-index" 396 | checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" 397 | dependencies = [ 398 | "cfg-if", 399 | "value-bag", 400 | ] 401 | 402 | [[package]] 403 | name = "memchr" 404 | version = "2.6.3" 405 | source = "registry+https://github.com/rust-lang/crates.io-index" 406 | checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" 407 | 408 | [[package]] 409 | name = "num_cpus" 410 | version = "1.13.1" 411 | source = "registry+https://github.com/rust-lang/crates.io-index" 412 | checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" 413 | dependencies = [ 414 | "hermit-abi 0.1.19", 415 | "libc", 416 | ] 417 | 418 | [[package]] 419 | name = "once_cell" 420 | version = "1.15.0" 421 | source = "registry+https://github.com/rust-lang/crates.io-index" 422 | checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1" 423 | 424 | [[package]] 425 | name = "parking" 426 | version = "2.1.1" 427 | source = "registry+https://github.com/rust-lang/crates.io-index" 428 | checksum = "e52c774a4c39359c1d1c52e43f73dd91a75a614652c825408eec30c95a9b2067" 429 | 430 | [[package]] 431 | name = "pin-project-lite" 432 | version = "0.2.13" 433 | source = "registry+https://github.com/rust-lang/crates.io-index" 434 | checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" 435 | 436 | [[package]] 437 | name = "pin-utils" 438 | version = "0.1.0" 439 | source = "registry+https://github.com/rust-lang/crates.io-index" 440 | checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" 441 | 442 | [[package]] 443 | name = "poll-promise" 444 | version = "0.3.0" 445 | dependencies = [ 446 | "async-net", 447 | "async-std", 448 | "document-features", 449 | "smol", 450 | "static_assertions", 451 | "tokio", 452 | "wasm-bindgen", 453 | "wasm-bindgen-futures", 454 | ] 455 | 456 | [[package]] 457 | name = "polling" 458 | version = "2.8.0" 459 | source = "registry+https://github.com/rust-lang/crates.io-index" 460 | checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce" 461 | dependencies = [ 462 | "autocfg", 463 | "bitflags", 464 | "cfg-if", 465 | "concurrent-queue", 466 | "libc", 467 | "log", 468 | "pin-project-lite", 469 | "windows-sys 0.48.0", 470 | ] 471 | 472 | [[package]] 473 | name = "proc-macro2" 474 | version = "1.0.46" 475 | source = "registry+https://github.com/rust-lang/crates.io-index" 476 | checksum = "94e2ef8dbfc347b10c094890f778ee2e36ca9bb4262e86dc99cd217e35f3470b" 477 | dependencies = [ 478 | "unicode-ident", 479 | ] 480 | 481 | [[package]] 482 | name = "quote" 483 | version = "1.0.21" 484 | source = "registry+https://github.com/rust-lang/crates.io-index" 485 | checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" 486 | dependencies = [ 487 | "proc-macro2", 488 | ] 489 | 490 | [[package]] 491 | name = "rustix" 492 | version = "0.37.24" 493 | source = "registry+https://github.com/rust-lang/crates.io-index" 494 | checksum = "4279d76516df406a8bd37e7dff53fd37d1a093f997a3c34a5c21658c126db06d" 495 | dependencies = [ 496 | "bitflags", 497 | "errno", 498 | "io-lifetimes", 499 | "libc", 500 | "linux-raw-sys", 501 | "windows-sys 0.48.0", 502 | ] 503 | 504 | [[package]] 505 | name = "signal-hook" 506 | version = "0.3.17" 507 | source = "registry+https://github.com/rust-lang/crates.io-index" 508 | checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" 509 | dependencies = [ 510 | "libc", 511 | "signal-hook-registry", 512 | ] 513 | 514 | [[package]] 515 | name = "signal-hook-registry" 516 | version = "1.4.1" 517 | source = "registry+https://github.com/rust-lang/crates.io-index" 518 | checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" 519 | dependencies = [ 520 | "libc", 521 | ] 522 | 523 | [[package]] 524 | name = "slab" 525 | version = "0.4.9" 526 | source = "registry+https://github.com/rust-lang/crates.io-index" 527 | checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" 528 | dependencies = [ 529 | "autocfg", 530 | ] 531 | 532 | [[package]] 533 | name = "smol" 534 | version = "1.3.0" 535 | source = "registry+https://github.com/rust-lang/crates.io-index" 536 | checksum = "13f2b548cd8447f8de0fdf1c592929f70f4fc7039a05e47404b0d096ec6987a1" 537 | dependencies = [ 538 | "async-channel", 539 | "async-executor", 540 | "async-fs", 541 | "async-io", 542 | "async-lock", 543 | "async-net", 544 | "async-process", 545 | "blocking", 546 | "futures-lite", 547 | ] 548 | 549 | [[package]] 550 | name = "socket2" 551 | version = "0.4.9" 552 | source = "registry+https://github.com/rust-lang/crates.io-index" 553 | checksum = "64a4a911eed85daf18834cfaa86a79b7d266ff93ff5ba14005426219480ed662" 554 | dependencies = [ 555 | "libc", 556 | "winapi", 557 | ] 558 | 559 | [[package]] 560 | name = "static_assertions" 561 | version = "1.1.0" 562 | source = "registry+https://github.com/rust-lang/crates.io-index" 563 | checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" 564 | 565 | [[package]] 566 | name = "syn" 567 | version = "1.0.101" 568 | source = "registry+https://github.com/rust-lang/crates.io-index" 569 | checksum = "e90cde112c4b9690b8cbe810cba9ddd8bc1d7472e2cae317b69e9438c1cba7d2" 570 | dependencies = [ 571 | "proc-macro2", 572 | "quote", 573 | "unicode-ident", 574 | ] 575 | 576 | [[package]] 577 | name = "tokio" 578 | version = "1.26.0" 579 | source = "registry+https://github.com/rust-lang/crates.io-index" 580 | checksum = "03201d01c3c27a29c8a5cee5b55a93ddae1ccf6f08f65365c2c918f8c1b76f64" 581 | dependencies = [ 582 | "autocfg", 583 | "num_cpus", 584 | "pin-project-lite", 585 | "tokio-macros", 586 | "windows-sys 0.45.0", 587 | ] 588 | 589 | [[package]] 590 | name = "tokio-macros" 591 | version = "1.8.2" 592 | source = "registry+https://github.com/rust-lang/crates.io-index" 593 | checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" 594 | dependencies = [ 595 | "proc-macro2", 596 | "quote", 597 | "syn", 598 | ] 599 | 600 | [[package]] 601 | name = "unicode-ident" 602 | version = "1.0.4" 603 | source = "registry+https://github.com/rust-lang/crates.io-index" 604 | checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd" 605 | 606 | [[package]] 607 | name = "value-bag" 608 | version = "1.0.0-alpha.9" 609 | source = "registry+https://github.com/rust-lang/crates.io-index" 610 | checksum = "2209b78d1249f7e6f3293657c9779fe31ced465df091bbd433a1cf88e916ec55" 611 | dependencies = [ 612 | "ctor", 613 | "version_check", 614 | ] 615 | 616 | [[package]] 617 | name = "version_check" 618 | version = "0.9.4" 619 | source = "registry+https://github.com/rust-lang/crates.io-index" 620 | checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" 621 | 622 | [[package]] 623 | name = "waker-fn" 624 | version = "1.1.1" 625 | source = "registry+https://github.com/rust-lang/crates.io-index" 626 | checksum = "f3c4517f54858c779bbcbf228f4fca63d121bf85fbecb2dc578cdf4a39395690" 627 | 628 | [[package]] 629 | name = "wasm-bindgen" 630 | version = "0.2.83" 631 | source = "registry+https://github.com/rust-lang/crates.io-index" 632 | checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268" 633 | dependencies = [ 634 | "cfg-if", 635 | "wasm-bindgen-macro", 636 | ] 637 | 638 | [[package]] 639 | name = "wasm-bindgen-backend" 640 | version = "0.2.83" 641 | source = "registry+https://github.com/rust-lang/crates.io-index" 642 | checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142" 643 | dependencies = [ 644 | "bumpalo", 645 | "log", 646 | "once_cell", 647 | "proc-macro2", 648 | "quote", 649 | "syn", 650 | "wasm-bindgen-shared", 651 | ] 652 | 653 | [[package]] 654 | name = "wasm-bindgen-futures" 655 | version = "0.4.33" 656 | source = "registry+https://github.com/rust-lang/crates.io-index" 657 | checksum = "23639446165ca5a5de86ae1d8896b737ae80319560fbaa4c2887b7da6e7ebd7d" 658 | dependencies = [ 659 | "cfg-if", 660 | "js-sys", 661 | "wasm-bindgen", 662 | "web-sys", 663 | ] 664 | 665 | [[package]] 666 | name = "wasm-bindgen-macro" 667 | version = "0.2.83" 668 | source = "registry+https://github.com/rust-lang/crates.io-index" 669 | checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810" 670 | dependencies = [ 671 | "quote", 672 | "wasm-bindgen-macro-support", 673 | ] 674 | 675 | [[package]] 676 | name = "wasm-bindgen-macro-support" 677 | version = "0.2.83" 678 | source = "registry+https://github.com/rust-lang/crates.io-index" 679 | checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" 680 | dependencies = [ 681 | "proc-macro2", 682 | "quote", 683 | "syn", 684 | "wasm-bindgen-backend", 685 | "wasm-bindgen-shared", 686 | ] 687 | 688 | [[package]] 689 | name = "wasm-bindgen-shared" 690 | version = "0.2.83" 691 | source = "registry+https://github.com/rust-lang/crates.io-index" 692 | checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" 693 | 694 | [[package]] 695 | name = "web-sys" 696 | version = "0.3.60" 697 | source = "registry+https://github.com/rust-lang/crates.io-index" 698 | checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f" 699 | dependencies = [ 700 | "js-sys", 701 | "wasm-bindgen", 702 | ] 703 | 704 | [[package]] 705 | name = "winapi" 706 | version = "0.3.9" 707 | source = "registry+https://github.com/rust-lang/crates.io-index" 708 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 709 | dependencies = [ 710 | "winapi-i686-pc-windows-gnu", 711 | "winapi-x86_64-pc-windows-gnu", 712 | ] 713 | 714 | [[package]] 715 | name = "winapi-i686-pc-windows-gnu" 716 | version = "0.4.0" 717 | source = "registry+https://github.com/rust-lang/crates.io-index" 718 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 719 | 720 | [[package]] 721 | name = "winapi-x86_64-pc-windows-gnu" 722 | version = "0.4.0" 723 | source = "registry+https://github.com/rust-lang/crates.io-index" 724 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 725 | 726 | [[package]] 727 | name = "windows-sys" 728 | version = "0.45.0" 729 | source = "registry+https://github.com/rust-lang/crates.io-index" 730 | checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" 731 | dependencies = [ 732 | "windows-targets 0.42.2", 733 | ] 734 | 735 | [[package]] 736 | name = "windows-sys" 737 | version = "0.48.0" 738 | source = "registry+https://github.com/rust-lang/crates.io-index" 739 | checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" 740 | dependencies = [ 741 | "windows-targets 0.48.5", 742 | ] 743 | 744 | [[package]] 745 | name = "windows-targets" 746 | version = "0.42.2" 747 | source = "registry+https://github.com/rust-lang/crates.io-index" 748 | checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" 749 | dependencies = [ 750 | "windows_aarch64_gnullvm 0.42.2", 751 | "windows_aarch64_msvc 0.42.2", 752 | "windows_i686_gnu 0.42.2", 753 | "windows_i686_msvc 0.42.2", 754 | "windows_x86_64_gnu 0.42.2", 755 | "windows_x86_64_gnullvm 0.42.2", 756 | "windows_x86_64_msvc 0.42.2", 757 | ] 758 | 759 | [[package]] 760 | name = "windows-targets" 761 | version = "0.48.5" 762 | source = "registry+https://github.com/rust-lang/crates.io-index" 763 | checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" 764 | dependencies = [ 765 | "windows_aarch64_gnullvm 0.48.5", 766 | "windows_aarch64_msvc 0.48.5", 767 | "windows_i686_gnu 0.48.5", 768 | "windows_i686_msvc 0.48.5", 769 | "windows_x86_64_gnu 0.48.5", 770 | "windows_x86_64_gnullvm 0.48.5", 771 | "windows_x86_64_msvc 0.48.5", 772 | ] 773 | 774 | [[package]] 775 | name = "windows_aarch64_gnullvm" 776 | version = "0.42.2" 777 | source = "registry+https://github.com/rust-lang/crates.io-index" 778 | checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" 779 | 780 | [[package]] 781 | name = "windows_aarch64_gnullvm" 782 | version = "0.48.5" 783 | source = "registry+https://github.com/rust-lang/crates.io-index" 784 | checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" 785 | 786 | [[package]] 787 | name = "windows_aarch64_msvc" 788 | version = "0.42.2" 789 | source = "registry+https://github.com/rust-lang/crates.io-index" 790 | checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" 791 | 792 | [[package]] 793 | name = "windows_aarch64_msvc" 794 | version = "0.48.5" 795 | source = "registry+https://github.com/rust-lang/crates.io-index" 796 | checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" 797 | 798 | [[package]] 799 | name = "windows_i686_gnu" 800 | version = "0.42.2" 801 | source = "registry+https://github.com/rust-lang/crates.io-index" 802 | checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" 803 | 804 | [[package]] 805 | name = "windows_i686_gnu" 806 | version = "0.48.5" 807 | source = "registry+https://github.com/rust-lang/crates.io-index" 808 | checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" 809 | 810 | [[package]] 811 | name = "windows_i686_msvc" 812 | version = "0.42.2" 813 | source = "registry+https://github.com/rust-lang/crates.io-index" 814 | checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" 815 | 816 | [[package]] 817 | name = "windows_i686_msvc" 818 | version = "0.48.5" 819 | source = "registry+https://github.com/rust-lang/crates.io-index" 820 | checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" 821 | 822 | [[package]] 823 | name = "windows_x86_64_gnu" 824 | version = "0.42.2" 825 | source = "registry+https://github.com/rust-lang/crates.io-index" 826 | checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" 827 | 828 | [[package]] 829 | name = "windows_x86_64_gnu" 830 | version = "0.48.5" 831 | source = "registry+https://github.com/rust-lang/crates.io-index" 832 | checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" 833 | 834 | [[package]] 835 | name = "windows_x86_64_gnullvm" 836 | version = "0.42.2" 837 | source = "registry+https://github.com/rust-lang/crates.io-index" 838 | checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" 839 | 840 | [[package]] 841 | name = "windows_x86_64_gnullvm" 842 | version = "0.48.5" 843 | source = "registry+https://github.com/rust-lang/crates.io-index" 844 | checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" 845 | 846 | [[package]] 847 | name = "windows_x86_64_msvc" 848 | version = "0.42.2" 849 | source = "registry+https://github.com/rust-lang/crates.io-index" 850 | checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" 851 | 852 | [[package]] 853 | name = "windows_x86_64_msvc" 854 | version = "0.48.5" 855 | source = "registry+https://github.com/rust-lang/crates.io-index" 856 | checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" 857 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "poll-promise" 3 | version = "0.3.0" 4 | authors = ["Embark "] 5 | license = "MIT OR Apache-2.0" 6 | description = "Poll the result of an async operation in a game or immediate mode GUI." 7 | edition = "2021" 8 | homepage = "https://github.com/EmbarkStudios/poll-promise" 9 | repository = "https://github.com/EmbarkStudios/poll-promise" 10 | readme = "README.md" 11 | categories = ["asynchronous"] 12 | keywords = ["promise", "poll", "async", "gamedev", "gui"] 13 | include = ["LICENSE-APACHE", "LICENSE-MIT", "**/*.rs", "Cargo.toml"] 14 | 15 | [package.metadata.docs.rs] 16 | all-features = true 17 | rustdoc-args = ["--cfg", "docsrs"] 18 | 19 | [lib] 20 | 21 | [features] 22 | ## If you enable the `async-std` feature you can use [`Promise::spawn_async`] and [`Promise::spawn_blocking`] 23 | ## which will spawn tasks in the surrounding async-std runtime. 24 | async-std = ["dep:async-std"] 25 | 26 | ## If you enable the `smol` feature you can use [`Promise::spawn_async`] and [`Promise::spawn_local`] 27 | ## which will spawn tasks using the smol executor. Remember to tick the smol executor with [`tick`] and [`tick_local`]. 28 | smol = ["dep:smol"] 29 | 30 | ## Enabling the `smol_tick_poll` feature (together with `smol`) calling [`Promise::poll`] will automatically tick the smol executor. 31 | ## This means you do not have to worry about calling [`tick`] but comes at the cost of loss of finer control over the executor. 32 | ## 33 | ## Since calling [`tick_local`] will block the current thread, running multiple local promises at once with `smol_tick_poll` enabled 34 | ## may also cause stuttering. 35 | ## 36 | ## poll-promise will automatically tick the smol executor with this feature disabled for you when using [`Promise::block_until_ready`] 37 | ## and friends, however. 38 | smol_tick_poll = [] 39 | 40 | ## If you enable the `tokio` feature you can use [`Promise::spawn_async`], [`Promise::spawn_local`] and [`Promise::spawn_blocking`] 41 | ## which will spawn tasks in the surrounding tokio runtime. 42 | tokio = ["dep:tokio"] 43 | 44 | ## If you enable the `web` feature you can use [`Promise::spawn_local`] which will spawn tasks using 45 | ## [`wasm_bindgen_futures::spawn_local`](https://rustwasm.github.io/wasm-bindgen/api/wasm_bindgen_futures/fn.spawn_local.html). 46 | web = ["wasm-bindgen", "wasm-bindgen-futures"] 47 | 48 | 49 | [dependencies] 50 | async-std = { version = "1.12", optional = true } 51 | document-features = "0.2" 52 | smol = { version = "1.2.5", optional = true } 53 | static_assertions = "1.1" 54 | tokio = { version = "1", features = [ 55 | "rt", 56 | "rt-multi-thread", 57 | "sync", 58 | "macros", 59 | ], optional = true } 60 | 61 | wasm-bindgen = { version = "0.2", optional = true } 62 | wasm-bindgen-futures = { version = "0.4", optional = true } 63 | 64 | [dev-dependencies] 65 | async-net = "1.7.0" 66 | -------------------------------------------------------------------------------- /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 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 Embark Studios 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | # ⌛ `poll-promise` 10 | 11 | **A Rust promise for games and immediate mode GUIs** 12 | 13 | [![Embark](https://img.shields.io/badge/embark-open%20source-blueviolet.svg)](https://embark.dev) 14 | [![Embark](https://img.shields.io/badge/discord-ark-%237289da.svg?logo=discord)](https://discord.gg/dAuKfZS) 15 | [![Crates.io](https://img.shields.io/crates/v/poll-promise.svg)](https://crates.io/crates/poll-promise) 16 | [![Docs](https://docs.rs/poll-promise/badge.svg)](https://docs.rs/poll-promise) 17 | [![dependency status](https://deps.rs/repo/github/EmbarkStudios/poll-promise/status.svg)](https://deps.rs/repo/github/EmbarkStudios/poll-promise) 18 | [![Build status](https://github.com/EmbarkStudios/physx-rs/workflows/CI/badge.svg)](https://github.com/EmbarkStudios/physx-rs/actions) 19 |
20 | 21 | ## Description 22 | 23 | `poll-promise` is a Rust crate for polling the result of a concurrent (e.g. `async`) operation. This is in particular useful in games and immediate mode GUI:s, where one often wants to start a background operation and then ask "are we there yet?" on each subsequent frame until the operation completes. 24 | 25 | Example: 26 | 27 | ``` rust 28 | let promise = poll_promise::Promise::spawn_thread("slow_operation", something_slow); 29 | 30 | // Then in the game loop or immediate mode GUI code: 31 | if let Some(result) = promise.ready() { 32 | // Use/show result 33 | } else { 34 | // Show a loading icon 35 | } 36 | ``` 37 | 38 | If you enable the `tokio` feature you can use `poll-promise` with the [tokio](https://github.com/tokio-rs/tokio) runtime. 39 | 40 | ### Caveat 41 | The crate is primarily useful as a high-level building block in apps. 42 | 43 | This crate provides convenience methods to spawn threads and tokio tasks, and methods that block on waiting for a result. 44 | This is gererally a bad idea to do in a library, as decisions about execution environments and thread blocking should be left to the app. 45 | So we do not recommend using this crate for libraries in its current state. 46 | 47 | ## See also 48 | Similar functionality is provided by: 49 | 50 | * [`eventuals::Eventual`](https://docs.rs/eventuals/latest/eventuals/struct.Eventual.html) 51 | * [`tokio::sync::watch::channel`](https://docs.rs/tokio/latest/tokio/sync/watch/fn.channel.html) 52 | 53 | ## Contribution 54 | 55 | [![Contributor Covenant](https://img.shields.io/badge/contributor%20covenant-v1.4-ff69b4.svg)](../main/CODE_OF_CONDUCT.md) 56 | 57 | We welcome community contributions to this project. 58 | 59 | Please read our [Contributor Guide](CONTRIBUTING.md) for more information on how to get started. 60 | Please also read our [Contributor Terms](CONTRIBUTING.md#contributor-terms) before you make any contributions. 61 | 62 | Any contribution intentionally submitted for inclusion in an Embark Studios project, shall comply with the Rust standard licensing model (MIT OR Apache 2.0) and therefore be dual licensed as described below, without any additional terms or conditions: 63 | 64 | ### License 65 | 66 | This contribution is dual licensed under EITHER OF 67 | 68 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or ) 69 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or ) 70 | 71 | at your option. 72 | 73 | For clarity, "your" refers to Embark or any other licensee/user of the contribution. 74 | -------------------------------------------------------------------------------- /check.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | script_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P ) 3 | cd "$script_path" 4 | set -eux 5 | 6 | # Checks all tests, lints etc. 7 | # Basically does what the CI does. 8 | 9 | cargo check --workspace --all-targets --features "tokio" 10 | cargo check --target wasm32-unknown-unknown --features "web" 11 | cargo test --workspace --doc --features "tokio" 12 | cargo test --workspace --all-targets --features "tokio" 13 | cargo clippy --workspace --all-targets --features "tokio" -- -D warnings -W clippy::all 14 | cargo fmt --all -- --check 15 | 16 | cargo doc --no-deps --features "tokio" 17 | # cargo doc --target wasm32-unknown-unknown --no-deps --features "web" 18 | -------------------------------------------------------------------------------- /deny.toml: -------------------------------------------------------------------------------- 1 | targets = [ 2 | { triple = "x86_64-unknown-linux-gnu" }, 3 | { triple = "aarch64-apple-darwin" }, 4 | { triple = "x86_64-apple-darwin" }, 5 | { triple = "x86_64-pc-windows-msvc" }, 6 | { triple = "aarch64-linux-android" }, 7 | { triple = "x86_64-unknown-linux-musl" }, 8 | ] 9 | 10 | [advisories] 11 | vulnerability = "deny" 12 | unmaintained = "warn" 13 | yanked = "deny" 14 | ignore = [] 15 | 16 | [bans] 17 | multiple-versions = "deny" 18 | wildcards = "allow" # at least until https://github.com/EmbarkStudios/cargo-deny/issues/241 is fixed 19 | deny = [ 20 | { name = "openssl" }, # we use rustls instead 21 | { name = "openssl-sys" }, # we use rustls instead 22 | ] 23 | 24 | skip = [ 25 | ] 26 | skip-tree = [ 27 | { name = "async-io", version = "<= 1.12.0" } 28 | ] 29 | 30 | 31 | [licenses] 32 | unlicensed = "deny" 33 | allow-osi-fsf-free = "neither" 34 | confidence-threshold = 0.92 # We want really high confidence when inferring licenses from text 35 | copyleft = "deny" 36 | allow = [ 37 | "Apache-2.0", # https://tldrlegal.com/license/apache-license-2.0-(apache-2.0) 38 | "Apache-2.0 WITH LLVM-exception", # https://spdx.org/licenses/LLVM-exception.html 39 | "BSD-2-Clause", # https://tldrlegal.com/license/bsd-2-clause-license-(freebsd) 40 | "BSD-3-Clause", # https://tldrlegal.com/license/bsd-3-clause-license-(revised) 41 | "MIT", # https://tldrlegal.com/license/mit-license 42 | "Unicode-DFS-2016", # https://spdx.org/licenses/Unicode-DFS-2016.html 43 | "Zlib", # https://tldrlegal.com/license/zlib-libpng-license-(zlib) 44 | "ISC", # https://tldrlegal.com/license/-isc-license 45 | ] 46 | -------------------------------------------------------------------------------- /examples/example.rs: -------------------------------------------------------------------------------- 1 | fn slow_operation() -> String { 2 | std::thread::sleep(std::time::Duration::from_secs(2)); 3 | "Hello from other thread!".to_owned() 4 | } 5 | 6 | fn main() { 7 | let promise = poll_promise::Promise::spawn_thread("bg_thread", slow_operation); 8 | 9 | eprint!("Waiting"); 10 | 11 | // This loop would normally be a game loop, or the executor of an immediate mode GUI. 12 | loop { 13 | // Poll the promise: 14 | if let Some(result) = promise.ready() { 15 | eprintln!("\nDONE: {:?}", result); 16 | break; 17 | } else { 18 | eprint!("."); // show that we are waiting 19 | } 20 | 21 | // Do other stuff, e.g. game logic or painting a UI 22 | std::thread::sleep(std::time::Duration::from_millis(100)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! `poll-promise` is a Rust crate for polling the result of a concurrent (e.g. `async`) operation. 2 | //! 3 | //! It is particularly useful in games and immediate mode GUI:s, where one often wants to start 4 | //! a background operation and then ask "are we there yet?" on each subsequent frame 5 | //! until the operation completes. 6 | //! 7 | //! Example: 8 | //! 9 | //! ``` 10 | //! # fn something_slow() {} 11 | //! # use poll_promise::Promise; 12 | //! # 13 | //! let promise = Promise::spawn_thread("slow_operation", something_slow); 14 | //! 15 | //! // Then in the game loop or immediate mode GUI code: 16 | //! if let Some(result) = promise.ready() { 17 | //! // Use/show result 18 | //! } else { 19 | //! // Show a loading screen 20 | //! } 21 | //! ``` 22 | //! 23 | //! ## Features 24 | //! `poll-promise` can be used with any async runtime (or without one!), 25 | //! but a few convenience methods are added 26 | //! when compiled with the following features: 27 | //! 28 | #![doc = document_features::document_features!()] 29 | #![cfg_attr(docsrs, feature(doc_auto_cfg))] 30 | //! 31 | 32 | // BEGIN - Embark standard lints v6 for Rust 1.55+ 33 | // do not change or add/remove here, but one can add exceptions after this section 34 | // for more info see: 35 | #![deny(unsafe_code)] 36 | #![warn( 37 | clippy::all, 38 | clippy::await_holding_lock, 39 | clippy::char_lit_as_u8, 40 | clippy::checked_conversions, 41 | clippy::dbg_macro, 42 | clippy::debug_assert_with_mut_call, 43 | clippy::doc_markdown, 44 | clippy::empty_enum, 45 | clippy::enum_glob_use, 46 | clippy::exit, 47 | clippy::expl_impl_clone_on_copy, 48 | clippy::explicit_deref_methods, 49 | clippy::explicit_into_iter_loop, 50 | clippy::fallible_impl_from, 51 | clippy::filter_map_next, 52 | clippy::flat_map_option, 53 | clippy::float_cmp_const, 54 | clippy::fn_params_excessive_bools, 55 | clippy::from_iter_instead_of_collect, 56 | clippy::if_let_mutex, 57 | clippy::implicit_clone, 58 | clippy::imprecise_flops, 59 | clippy::inefficient_to_string, 60 | clippy::invalid_upcast_comparisons, 61 | clippy::large_digit_groups, 62 | clippy::large_stack_arrays, 63 | clippy::large_types_passed_by_value, 64 | clippy::let_unit_value, 65 | clippy::linkedlist, 66 | clippy::lossy_float_literal, 67 | clippy::macro_use_imports, 68 | clippy::manual_ok_or, 69 | clippy::map_err_ignore, 70 | clippy::map_flatten, 71 | clippy::map_unwrap_or, 72 | clippy::match_on_vec_items, 73 | clippy::match_same_arms, 74 | clippy::match_wild_err_arm, 75 | clippy::match_wildcard_for_single_variants, 76 | clippy::mem_forget, 77 | clippy::mismatched_target_os, 78 | clippy::missing_enforced_import_renames, 79 | clippy::mut_mut, 80 | clippy::mutex_integer, 81 | clippy::needless_borrow, 82 | clippy::needless_continue, 83 | clippy::needless_for_each, 84 | clippy::option_option, 85 | clippy::path_buf_push_overwrite, 86 | clippy::ptr_as_ptr, 87 | clippy::rc_mutex, 88 | clippy::ref_option_ref, 89 | clippy::rest_pat_in_fully_bound_structs, 90 | clippy::same_functions_in_if_condition, 91 | clippy::semicolon_if_nothing_returned, 92 | clippy::single_match_else, 93 | clippy::string_add_assign, 94 | clippy::string_add, 95 | clippy::string_lit_as_bytes, 96 | clippy::string_to_string, 97 | clippy::todo, 98 | clippy::trait_duplication_in_bounds, 99 | clippy::unimplemented, 100 | clippy::unnested_or_patterns, 101 | clippy::unused_self, 102 | clippy::useless_transmute, 103 | clippy::verbose_file_reads, 104 | clippy::zero_sized_map_values, 105 | future_incompatible, 106 | nonstandard_style, 107 | rust_2018_idioms 108 | )] 109 | // END - Embark standard lints v6 for Rust 1.55+ 110 | // crate-specific exceptions: 111 | #![deny(missing_docs, rustdoc::missing_crate_level_docs)] 112 | 113 | mod promise; 114 | 115 | pub use promise::{Promise, Sender, TaskType}; 116 | 117 | #[cfg(feature = "smol")] 118 | static EXECUTOR: smol::Executor<'static> = smol::Executor::new(); 119 | #[cfg(feature = "smol")] 120 | thread_local! { 121 | static LOCAL_EXECUTOR: smol::LocalExecutor<'static> = smol::LocalExecutor::new(); 122 | } 123 | 124 | /// 'Tick' the `smol` thread executor. 125 | /// 126 | /// Poll promise will call this for you when using [`Promise::block_until_ready`] and friends. 127 | /// If so desired [`Promise::poll`] will run this for you with the `smol_tick_poll` feature. 128 | #[cfg(feature = "smol")] 129 | pub fn tick() -> bool { 130 | crate::EXECUTOR.try_tick() 131 | } 132 | 133 | /// 'Tick' the `smol` local thread executor. 134 | /// 135 | /// Poll promise will call this for you when using [`Promise::block_until_ready`] and friends. 136 | /// If so desired [`Promise::poll`] will run this for you with the `smol_tick_poll` feature. 137 | #[cfg(feature = "smol")] 138 | pub fn tick_local() -> bool { 139 | crate::LOCAL_EXECUTOR.with(|exec| exec.try_tick()) 140 | } 141 | 142 | #[cfg(test)] 143 | mod test { 144 | use crate::Promise; 145 | 146 | #[test] 147 | fn it_spawns_threads() { 148 | let promise = Promise::spawn_thread("test", || { 149 | std::thread::sleep(std::time::Duration::from_secs(1)); 150 | 0 151 | }); 152 | 153 | assert_eq!(0, promise.block_and_take()); 154 | } 155 | 156 | #[test] 157 | #[cfg(feature = "smol")] 158 | fn it_runs_async_threaded() { 159 | let promise = Promise::spawn_async(async move { 0 }); 160 | 161 | assert_eq!(0, promise.block_and_take()); 162 | } 163 | 164 | #[tokio::test(flavor = "multi_thread")] 165 | #[cfg(feature = "tokio")] 166 | async fn it_runs_async_threaded() { 167 | let promise = Promise::spawn_async(async move { 0 }); 168 | 169 | assert_eq!(0, promise.block_and_take()); 170 | } 171 | 172 | #[test] 173 | #[cfg(feature = "async-std")] 174 | fn it_runs_async_threaded() { 175 | let promise = Promise::spawn_async(async move { 0 }); 176 | 177 | assert_eq!(0, promise.block_and_take()); 178 | } 179 | 180 | #[test] 181 | #[cfg(feature = "smol")] 182 | fn it_runs_locally() { 183 | let promise = Promise::spawn_local(async move { 0 }); 184 | 185 | assert_eq!(0, promise.block_and_take()); 186 | } 187 | 188 | #[test] 189 | #[cfg(feature = "smol")] 190 | fn it_runs_background() { 191 | let promise = Promise::spawn_async(async move { 192 | let mut e = 0; 193 | for i in -10000..0 { 194 | e += i; 195 | } 196 | e 197 | }); 198 | #[cfg(not(feature = "smol_tick_poll"))] 199 | crate::tick(); 200 | 201 | std::thread::sleep(std::time::Duration::from_secs(1)); 202 | assert!(promise.ready().is_some(), "was not finished"); 203 | } 204 | 205 | #[tokio::test(flavor = "multi_thread")] 206 | #[cfg(feature = "tokio")] 207 | async fn it_runs_background() { 208 | let promise = Promise::spawn_async(async move { 209 | let mut e = 0; 210 | for i in -10000..0 { 211 | e += i; 212 | } 213 | e 214 | }); 215 | 216 | std::thread::sleep(std::time::Duration::from_secs(1)); 217 | assert!(promise.ready().is_some(), "was not finished"); 218 | } 219 | 220 | #[test] 221 | #[cfg(feature = "async-std")] 222 | fn it_runs_background() { 223 | let promise = Promise::spawn_async(async move { 224 | let mut e = 0; 225 | for i in -10000..0 { 226 | e += i; 227 | } 228 | e 229 | }); 230 | 231 | std::thread::sleep(std::time::Duration::from_secs(1)); 232 | assert!(promise.ready().is_some(), "was not finished"); 233 | } 234 | 235 | #[test] 236 | #[cfg(feature = "smol")] 237 | fn it_can_block() { 238 | let promise = Promise::spawn_local(async move { 239 | std::thread::sleep(std::time::Duration::from_secs(1)); 240 | }); 241 | #[cfg(not(feature = "smol_tick_poll"))] 242 | crate::tick_local(); 243 | 244 | assert!(promise.ready().is_some(), "was not finished"); 245 | } 246 | 247 | #[test] 248 | #[cfg(feature = "smol")] 249 | fn it_can_run_async_functions() { 250 | let promise = Promise::spawn_async(async move { something_async().await }); 251 | #[cfg(not(feature = "smol_tick_poll"))] 252 | crate::tick(); 253 | 254 | assert!(promise.block_and_take(), "example.com is ipv4"); 255 | } 256 | #[tokio::test(flavor = "multi_thread")] 257 | #[cfg(feature = "tokio")] 258 | async fn it_can_run_async_functions() { 259 | let promise = Promise::spawn_async(async move { something_async().await }); 260 | 261 | assert!(promise.block_and_take(), "example.com is ipv4"); 262 | } 263 | 264 | #[test] 265 | #[cfg(feature = "async-std")] 266 | fn it_can_run_async_functions() { 267 | let promise = Promise::spawn_async(async move { something_async().await }); 268 | 269 | assert!(promise.block_and_take(), "example.com is ipv4"); 270 | } 271 | 272 | #[test] 273 | #[cfg(feature = "smol")] 274 | fn it_can_run_async_functions_locally() { 275 | let promise = Promise::spawn_local(async move { something_async().await }); 276 | #[cfg(not(feature = "smol_tick_poll"))] 277 | crate::tick_local(); 278 | 279 | assert!(promise.block_and_take(), "example.com is ipv4"); 280 | } 281 | 282 | #[cfg(any(feature = "smol", feature = "tokio", feature = "async-std"))] 283 | async fn something_async() -> bool { 284 | async_net::resolve("example.com:80").await.unwrap()[0].is_ipv4() 285 | } 286 | } 287 | -------------------------------------------------------------------------------- /src/promise.rs: -------------------------------------------------------------------------------- 1 | use std::cell::UnsafeCell; 2 | 3 | /// Used to send a result to a [`Promise`]. 4 | /// 5 | /// You must call [`Self::send`] with a value eventually. 6 | /// 7 | /// If you drop the `Sender` without putting a value into it, 8 | /// it will cause the connected [`Promise`] to panic when polled. 9 | #[must_use = "You should call Sender::send with the result"] 10 | pub struct Sender(std::sync::mpsc::Sender); 11 | 12 | impl Sender { 13 | /// Send the result to the [`Promise`]. 14 | /// 15 | /// If the [`Promise`] has dropped, this does nothing. 16 | pub fn send(self, value: T) { 17 | self.0.send(value).ok(); // We ignore the error caused by the receiver being dropped. 18 | } 19 | } 20 | 21 | /// The type of a running task. 22 | #[derive(Clone, Copy)] 23 | #[allow(dead_code)] 24 | pub enum TaskType { 25 | /// This task is running in the local thread. 26 | Local, 27 | /// This task is running async in another thread. 28 | Async, 29 | /// This task is running in a different manner. 30 | None, 31 | } 32 | 33 | // ---------------------------------------------------------------------------- 34 | 35 | /// A promise that waits for the reception of a single value, 36 | /// presumably from some async task. 37 | /// 38 | /// A `Promise` starts out waiting for a value. 39 | /// Each time you call a member method it will check if that value is ready. 40 | /// Once ready, the `Promise` will store the value until you drop the `Promise`. 41 | /// 42 | /// Example: 43 | /// 44 | /// ``` 45 | /// # fn something_slow() {} 46 | /// # use poll_promise::Promise; 47 | /// # 48 | /// let promise = Promise::spawn_thread("slow_operation", move || something_slow()); 49 | /// 50 | /// // Then in the game loop or immediate mode GUI code: 51 | /// if let Some(result) = promise.ready() { 52 | /// // Use/show result 53 | /// } else { 54 | /// // Show a loading screen 55 | /// } 56 | /// ``` 57 | /// 58 | /// If you enable the `tokio` feature you can use `poll-promise` with the [tokio](https://github.com/tokio-rs/tokio) 59 | /// runtime to run `async` tasks using [`Promise::spawn_async`], [`Promise::spawn_local`], and [`Promise::spawn_blocking`]. 60 | #[must_use] 61 | pub struct Promise { 62 | data: PromiseImpl, 63 | task_type: TaskType, 64 | 65 | #[cfg(feature = "tokio")] 66 | join_handle: Option>, 67 | 68 | #[cfg(feature = "smol")] 69 | smol_task: Option>, 70 | 71 | #[cfg(feature = "async-std")] 72 | async_std_join_handle: Option>, 73 | } 74 | 75 | #[cfg(all( 76 | not(docsrs), 77 | any( 78 | all(feature = "tokio", feature = "smol"), 79 | all(feature = "tokio", feature = "async-std"), 80 | all(feature = "tokio", feature = "web"), 81 | all(feature = "smol", feature = "async-std"), 82 | all(feature = "smol", feature = "web"), 83 | all(feature = "async-std", feature = "web"), 84 | ) 85 | ))] 86 | compile_error!( 87 | "You can only specify one of the executor features: 'tokio', 'smol', 'async-std' or 'web'" 88 | ); 89 | 90 | // Ensure that Promise is !Sync, confirming the safety of the unsafe code. 91 | static_assertions::assert_not_impl_all!(Promise: Sync); 92 | static_assertions::assert_impl_all!(Promise: Send); 93 | 94 | impl Promise { 95 | /// Create a [`Promise`] and a corresponding [`Sender`]. 96 | /// 97 | /// Put the promised value into the sender when it is ready. 98 | /// If you drop the `Sender` without putting a value into it, 99 | /// it will cause a panic when polling the `Promise`. 100 | /// 101 | /// See also [`Self::spawn_blocking`], [`Self::spawn_async`], [`Self::spawn_local`], and [`Self::spawn_thread`]. 102 | pub fn new() -> (Sender, Self) { 103 | // We need a channel that we can wait blocking on (for `Self::block_until_ready`). 104 | // (`tokio::sync::oneshot` does not support blocking receive). 105 | let (tx, rx) = std::sync::mpsc::channel(); 106 | ( 107 | Sender(tx), 108 | Self { 109 | data: PromiseImpl(UnsafeCell::new(PromiseStatus::Pending(rx))), 110 | task_type: TaskType::None, 111 | 112 | #[cfg(feature = "tokio")] 113 | join_handle: None, 114 | 115 | #[cfg(feature = "async-std")] 116 | async_std_join_handle: None, 117 | 118 | #[cfg(feature = "smol")] 119 | smol_task: None, 120 | }, 121 | ) 122 | } 123 | 124 | /// Create a promise that already has the result. 125 | pub fn from_ready(value: T) -> Self { 126 | Self { 127 | data: PromiseImpl(UnsafeCell::new(PromiseStatus::Ready(value))), 128 | task_type: TaskType::None, 129 | 130 | #[cfg(feature = "tokio")] 131 | join_handle: None, 132 | 133 | #[cfg(feature = "async-std")] 134 | async_std_join_handle: None, 135 | 136 | #[cfg(feature = "smol")] 137 | smol_task: None, 138 | } 139 | } 140 | 141 | /// Spawn a future. Runs the task concurrently. 142 | /// 143 | /// See [`Self::spawn_local`]. 144 | /// 145 | /// You need to compile `poll-promise` with the "tokio" feature for this to be available. 146 | /// 147 | /// ## tokio 148 | /// This should be used for spawning asynchronous work that does _not_ do any heavy CPU computations 149 | /// as that will block other spawned tasks and will delay them. For example network IO, timers, etc. 150 | /// 151 | /// These type of future can have manually blocking code within it though, but has to then manually use 152 | /// [`tokio::task::block_in_place`](https://docs.rs/tokio/1.15.0/tokio/task/fn.block_in_place.html) on that, 153 | /// or `.await` that future. 154 | /// 155 | /// If you have a function or closure that you just want to offload to processed in the background, use the [`Self::spawn_blocking`] function instead. 156 | /// 157 | /// See the [tokio docs](https://docs.rs/tokio/1.15.0/tokio/index.html#cpu-bound-tasks-and-blocking-code) for more details about 158 | /// CPU-bound tasks vs async IO tasks. 159 | /// 160 | /// This is a convenience method, using [`Self::new`] with [`tokio::task::spawn`]. 161 | /// 162 | /// ## Example 163 | /// ``` no_run 164 | /// # async fn something_async() {} 165 | /// # use poll_promise::Promise; 166 | /// let promise = Promise::spawn_async(async move { something_async().await }); 167 | /// ``` 168 | #[cfg(any(feature = "tokio", feature = "smol", feature = "async-std"))] 169 | pub fn spawn_async(future: impl std::future::Future + 'static + Send) -> Self { 170 | let (sender, mut promise) = Self::new(); 171 | promise.task_type = TaskType::Async; 172 | 173 | #[cfg(feature = "tokio")] 174 | { 175 | promise.join_handle = 176 | Some(tokio::task::spawn(async move { sender.send(future.await) })); 177 | } 178 | 179 | #[cfg(feature = "smol")] 180 | { 181 | promise.smol_task = 182 | Some(crate::EXECUTOR.spawn(async move { sender.send(future.await) })); 183 | } 184 | 185 | #[cfg(feature = "async-std")] 186 | { 187 | promise.async_std_join_handle = 188 | Some(async_std::task::spawn( 189 | async move { sender.send(future.await) }, 190 | )); 191 | } 192 | 193 | promise 194 | } 195 | 196 | /// Spawn a future. Runs it in the local thread. 197 | /// 198 | /// You need to compile `poll-promise` with either the "tokio", "smol", or "web" feature for this to be available. 199 | /// 200 | /// This is a convenience method, using [`Self::new`] with [`tokio::task::spawn_local`]. 201 | /// Unlike [`Self::spawn_async`] this method does not require [`Send`]. 202 | /// However, you will have to set up [`tokio::task::LocalSet`]s yourself. 203 | /// 204 | /// ## Example 205 | /// ``` no_run 206 | /// # async fn something_async() {} 207 | /// # use poll_promise::Promise; 208 | /// let promise = Promise::spawn_local(async move { something_async().await }); 209 | /// ``` 210 | #[cfg(any(feature = "tokio", feature = "web", feature = "smol"))] 211 | pub fn spawn_local(future: impl std::future::Future + 'static) -> Self { 212 | // When using the web feature we don't mutate promise. 213 | #[allow(unused_mut)] 214 | let (sender, mut promise) = Self::new(); 215 | promise.task_type = TaskType::Local; 216 | 217 | // This *generally* works but not super well. 218 | // Tokio doesn't do any fancy local scheduling. 219 | #[cfg(feature = "tokio")] 220 | { 221 | promise.join_handle = Some(tokio::task::spawn_local(async move { 222 | sender.send(future.await); 223 | })); 224 | } 225 | 226 | #[cfg(feature = "web")] 227 | { 228 | wasm_bindgen_futures::spawn_local(async move { sender.send(future.await) }); 229 | } 230 | 231 | #[cfg(feature = "smol")] 232 | { 233 | promise.smol_task = Some( 234 | crate::LOCAL_EXECUTOR 235 | .with(|exec| exec.spawn(async move { sender.send(future.await) })), 236 | ); 237 | } 238 | 239 | promise 240 | } 241 | 242 | /// Spawn a blocking closure in a background task. 243 | /// 244 | /// You need to compile `poll-promise` with the "tokio" feature for this to be available. 245 | /// 246 | /// ## tokio 247 | /// This is a simple mechanism to offload a heavy function/closure to be processed in the thread pool for blocking CPU work. 248 | /// 249 | /// It can't do any async code. For that, use [`Self::spawn_async`]. 250 | /// 251 | /// This is a convenience method, using [`Self::new`] with [`tokio::task::spawn`] and [`tokio::task::block_in_place`]. 252 | /// 253 | /// ``` no_run 254 | /// # fn something_cpu_intensive() {} 255 | /// # use poll_promise::Promise; 256 | /// let promise = Promise::spawn_blocking(move || something_cpu_intensive()); 257 | /// ``` 258 | #[cfg(any(feature = "tokio", feature = "async-std"))] 259 | pub fn spawn_blocking(f: F) -> Self 260 | where 261 | F: FnOnce() -> T + Send + 'static, 262 | { 263 | let (sender, mut promise) = Self::new(); 264 | #[cfg(feature = "tokio")] 265 | { 266 | promise.join_handle = Some(tokio::task::spawn(async move { 267 | sender.send(tokio::task::block_in_place(f)); 268 | })); 269 | } 270 | 271 | #[cfg(feature = "async-std")] 272 | { 273 | promise.async_std_join_handle = Some(async_std::task::spawn_blocking(move || { 274 | sender.send(f()); 275 | })); 276 | } 277 | 278 | promise 279 | } 280 | 281 | /// Spawn a blocking closure in a background thread. 282 | /// 283 | /// The first argument is the name of the thread you spawn, passed to [`std::thread::Builder::name`]. 284 | /// It shows up in panic messages. 285 | /// 286 | /// This is a convenience method, using [`Self::new`] and [`std::thread::Builder`]. 287 | /// 288 | /// If you are compiling with the "tokio" or "web" features, you should use [`Self::spawn_blocking`] or [`Self::spawn_async`] instead. 289 | /// 290 | /// ``` 291 | /// # fn something_slow() {} 292 | /// # use poll_promise::Promise; 293 | /// let promise = Promise::spawn_thread("slow_operation", move || something_slow()); 294 | /// ``` 295 | #[cfg(not(target_arch = "wasm32"))] // can't spawn threads in wasm. 296 | pub fn spawn_thread(thread_name: impl Into, f: F) -> Self 297 | where 298 | F: FnOnce() -> T + Send + 'static, 299 | { 300 | let (sender, promise) = Self::new(); 301 | std::thread::Builder::new() 302 | .name(thread_name.into()) 303 | .spawn(move || sender.send(f())) 304 | .expect("Failed to spawn thread"); 305 | promise 306 | } 307 | 308 | /// Polls the promise and either returns a reference to the data, or [`None`] if still pending. 309 | /// 310 | /// Panics if the connected [`Sender`] was dropped before a value was sent. 311 | pub fn ready(&self) -> Option<&T> { 312 | match self.poll() { 313 | std::task::Poll::Pending => None, 314 | std::task::Poll::Ready(value) => Some(value), 315 | } 316 | } 317 | 318 | /// Polls the promise and either returns a mutable reference to the data, or [`None`] if still pending. 319 | /// 320 | /// Panics if the connected [`Sender`] was dropped before a value was sent. 321 | pub fn ready_mut(&mut self) -> Option<&mut T> { 322 | match self.poll_mut() { 323 | std::task::Poll::Pending => None, 324 | std::task::Poll::Ready(value) => Some(value), 325 | } 326 | } 327 | 328 | /// Returns either the completed promise object or the promise itself if it is not completed yet. 329 | /// 330 | /// Panics if the connected [`Sender`] was dropped before a value was sent. 331 | pub fn try_take(self) -> Result { 332 | self.data.try_take().map_err(|data| Self { 333 | data, 334 | task_type: self.task_type, 335 | 336 | #[cfg(feature = "tokio")] 337 | join_handle: None, 338 | 339 | #[cfg(feature = "async-std")] 340 | async_std_join_handle: None, 341 | 342 | #[cfg(feature = "smol")] 343 | smol_task: self.smol_task, 344 | }) 345 | } 346 | 347 | /// Block execution until ready, then returns a reference to the value. 348 | /// 349 | /// Panics if the connected [`Sender`] was dropped before a value was sent. 350 | #[cfg(not(target_arch = "wasm32"))] // Not allowed to block on Wasm 351 | pub fn block_until_ready(&self) -> &T { 352 | self.data.block_until_ready(self.task_type) 353 | } 354 | 355 | /// Block execution until ready, then returns a mutable reference to the value. 356 | /// 357 | /// Panics if the connected [`Sender`] was dropped before a value was sent. 358 | #[cfg(not(target_arch = "wasm32"))] // Not allowed to block on Wasm 359 | pub fn block_until_ready_mut(&mut self) -> &mut T { 360 | self.data.block_until_ready_mut(self.task_type) 361 | } 362 | 363 | /// Block execution until ready, then returns the promised value and consumes the `Promise`. 364 | /// 365 | /// Panics if the connected [`Sender`] was dropped before a value was sent. 366 | #[cfg(not(target_arch = "wasm32"))] // Not allowed to block on Wasm 367 | pub fn block_and_take(self) -> T { 368 | self.data.block_until_ready(self.task_type); 369 | match self.data.0.into_inner() { 370 | PromiseStatus::Pending(_) => unreachable!(), 371 | PromiseStatus::Ready(value) => value, 372 | } 373 | } 374 | 375 | /// Returns either a reference to the ready value [`std::task::Poll::Ready`] 376 | /// or [`std::task::Poll::Pending`]. 377 | /// 378 | /// Panics if the connected [`Sender`] was dropped before a value was sent. 379 | pub fn poll(&self) -> std::task::Poll<&T> { 380 | self.data.poll(self.task_type) 381 | } 382 | 383 | /// Returns either a mut reference to the ready value in a [`std::task::Poll::Ready`] 384 | /// or a [`std::task::Poll::Pending`]. 385 | /// 386 | /// Panics if the connected [`Sender`] was dropped before a value was sent. 387 | pub fn poll_mut(&mut self) -> std::task::Poll<&mut T> { 388 | self.data.poll_mut(self.task_type) 389 | } 390 | 391 | /// Returns the type of task this promise is running. 392 | /// See [`TaskType`]. 393 | pub fn task_type(&self) -> TaskType { 394 | self.task_type 395 | } 396 | 397 | /// Abort the running task spawned by [`Self::spawn_async`]. 398 | #[cfg(feature = "tokio")] 399 | pub fn abort(self) { 400 | if let Some(join_handle) = self.join_handle { 401 | join_handle.abort(); 402 | } 403 | } 404 | } 405 | 406 | // ---------------------------------------------------------------------------- 407 | 408 | enum PromiseStatus { 409 | Pending(std::sync::mpsc::Receiver), 410 | Ready(T), 411 | } 412 | 413 | struct PromiseImpl(UnsafeCell>); 414 | 415 | impl PromiseImpl { 416 | #[allow(unused_variables)] 417 | fn poll_mut(&mut self, task_type: TaskType) -> std::task::Poll<&mut T> { 418 | let inner = self.0.get_mut(); 419 | match inner { 420 | PromiseStatus::Pending(rx) => { 421 | #[cfg(all(feature = "smol", feature = "smol_tick_poll"))] 422 | Self::tick(task_type); 423 | if let Ok(value) = rx.try_recv() { 424 | *inner = PromiseStatus::Ready(value); 425 | match inner { 426 | PromiseStatus::Ready(ref mut value) => std::task::Poll::Ready(value), 427 | PromiseStatus::Pending(_) => unreachable!(), 428 | } 429 | } else { 430 | std::task::Poll::Pending 431 | } 432 | } 433 | PromiseStatus::Ready(ref mut value) => std::task::Poll::Ready(value), 434 | } 435 | } 436 | 437 | /// Returns either the completed promise object or the promise itself if it is not completed yet. 438 | fn try_take(self) -> Result { 439 | let inner = self.0.into_inner(); 440 | match inner { 441 | PromiseStatus::Pending(ref rx) => match rx.try_recv() { 442 | Ok(value) => Ok(value), 443 | Err(std::sync::mpsc::TryRecvError::Empty) => { 444 | Err(PromiseImpl(UnsafeCell::new(inner))) 445 | } 446 | Err(std::sync::mpsc::TryRecvError::Disconnected) => { 447 | panic!("The Promise Sender was dropped") 448 | } 449 | }, 450 | PromiseStatus::Ready(value) => Ok(value), 451 | } 452 | } 453 | 454 | #[allow(unsafe_code)] 455 | #[allow(unused_variables)] 456 | fn poll(&self, task_type: TaskType) -> std::task::Poll<&T> { 457 | let this = unsafe { 458 | // SAFETY: This is safe since Promise (and PromiseData) are !Sync and thus 459 | // need external synchronization anyway. We can only transition from 460 | // Pending->Ready, not the other way around, so once we're Ready we'll 461 | // stay ready. 462 | self.0.get().as_mut().expect("UnsafeCell should be valid") 463 | }; 464 | match this { 465 | PromiseStatus::Pending(rx) => { 466 | #[cfg(all(feature = "smol", feature = "smol_tick_poll"))] 467 | Self::tick(task_type); 468 | match rx.try_recv() { 469 | Ok(value) => { 470 | *this = PromiseStatus::Ready(value); 471 | match this { 472 | PromiseStatus::Ready(ref value) => std::task::Poll::Ready(value), 473 | PromiseStatus::Pending(_) => unreachable!(), 474 | } 475 | } 476 | Err(std::sync::mpsc::TryRecvError::Empty) => std::task::Poll::Pending, 477 | Err(std::sync::mpsc::TryRecvError::Disconnected) => { 478 | panic!("The Promise Sender was dropped") 479 | } 480 | } 481 | } 482 | PromiseStatus::Ready(ref value) => std::task::Poll::Ready(value), 483 | } 484 | } 485 | 486 | #[allow(unused_variables)] 487 | #[cfg(not(target_arch = "wasm32"))] // Not allowed to block on Wasm 488 | fn block_until_ready_mut(&mut self, task_type: TaskType) -> &mut T { 489 | // Constantly poll until we're ready. 490 | #[cfg(feature = "smol")] 491 | while self.poll(task_type).is_pending() { 492 | // Tick unless poll does it for us. 493 | #[cfg(not(feature = "smol_tick_poll"))] 494 | Self::tick(task_type); 495 | } 496 | let inner = self.0.get_mut(); 497 | match inner { 498 | PromiseStatus::Pending(rx) => { 499 | let value = rx.recv().expect("The Promise Sender was dropped"); 500 | *inner = PromiseStatus::Ready(value); 501 | match inner { 502 | PromiseStatus::Ready(ref mut value) => value, 503 | PromiseStatus::Pending(_) => unreachable!(), 504 | } 505 | } 506 | PromiseStatus::Ready(ref mut value) => value, 507 | } 508 | } 509 | 510 | #[allow(unsafe_code)] 511 | #[allow(unused_variables)] 512 | #[cfg(not(target_arch = "wasm32"))] // Not allowed to block on Wasm 513 | fn block_until_ready(&self, task_type: TaskType) -> &T { 514 | // Constantly poll until we're ready. 515 | #[cfg(feature = "smol")] 516 | while self.poll(task_type).is_pending() { 517 | // Tick unless poll does it for us. 518 | #[cfg(not(feature = "smol_tick_poll"))] 519 | Self::tick(task_type); 520 | } 521 | let this = unsafe { 522 | // SAFETY: This is safe since Promise (and PromiseData) are !Sync and thus 523 | // need external synchronization anyway. We can only transition from 524 | // Pending->Ready, not the other way around, so once we're Ready we'll 525 | // stay ready. 526 | self.0.get().as_mut().expect("UnsafeCell should be valid") 527 | }; 528 | match this { 529 | PromiseStatus::Pending(rx) => { 530 | let value = rx.recv().expect("The Promise Sender was dropped"); 531 | *this = PromiseStatus::Ready(value); 532 | match this { 533 | PromiseStatus::Ready(ref value) => value, 534 | PromiseStatus::Pending(_) => unreachable!(), 535 | } 536 | } 537 | PromiseStatus::Ready(ref value) => value, 538 | } 539 | } 540 | 541 | #[cfg(feature = "smol")] 542 | fn tick(task_type: TaskType) { 543 | match task_type { 544 | TaskType::Local => { 545 | crate::tick_local(); 546 | } 547 | TaskType::Async => { 548 | crate::tick(); 549 | } 550 | TaskType::None => (), 551 | }; 552 | } 553 | } 554 | --------------------------------------------------------------------------------