awaitable-0.4.0/.cargo_vcs_info.json0000644000000001360000000000100127660ustar { "git": { "sha1": "c1c15ec756cea9326b29c4bfa6b6825436a72e81" }, "path_in_vcs": "" }awaitable-0.4.0/.github/dependabot.yml000064400000000000000000000004361046102023000157510ustar 00000000000000version: 2 updates: - package-ecosystem: "github-actions" # Workflow files stored in the # default location of `.github/workflows` directory: "/" schedule: interval: "daily" - package-ecosystem: "cargo" directory: "/" schedule: interval: "daily" awaitable-0.4.0/.github/workflows/rust.yml000064400000000000000000000033331046102023000166750ustar 00000000000000name: Rust env: CARGO_TERM_COLOR: always on: push: paths-ignore: - 'README.md' - 'LICENSE' - '.gitignore' pull_request: paths-ignore: - 'README.md' - 'LICENSE' - '.gitignore' jobs: check: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/cache@v2 with: path: | ~/.cargo/bin/ ~/.cargo/registry/index/ ~/.cargo/registry/cache/ ~/.cargo/git/db/ target/ key: ${{ github.event.repository.name }}-${{ runner.os }}-cargo-check-${{ hashFiles('**/Cargo.lock') }}-v2 - name: Run check run: cargo check --all --all-features check_format: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Check format run: cargo fmt --all -- --check Run_clippy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/cache@v2 with: path: | ~/.cargo/bin/ ~/.cargo/registry/index/ ~/.cargo/registry/cache/ ~/.cargo/git/db/ target/ key: ${{ github.event.repository.name }}-${{ runner.os }}-cargo-clippy-${{ hashFiles('**/Cargo.lock') }}-v2 - name: Run clippy run: cargo clippy --all --all-features build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/cache@v2 with: path: | ~/.cargo/bin/ ~/.cargo/registry/index/ ~/.cargo/registry/cache/ ~/.cargo/git/db/ target/ key: ${{ github.event.repository.name }}-${{ runner.os }}-cargo-test-${{ hashFiles('**/Cargo.lock') }}-v2 - name: Run tests run: cargo test --all-features awaitable-0.4.0/.gitignore000064400000000000000000000006311046102023000135460ustar 00000000000000# Generated by Cargo # will have compiled files and executables /target/ # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk # Added by cargo # # already existing elements were commented out /target #Cargo.lock awaitable-0.4.0/Cargo.toml0000644000000020660000000000100107700ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "awaitable" version = "0.4.0" authors = ["Jiahao XU "] description = "Awaitable type with input and output that can be stored in container." readme = "README.md" keywords = [ "async", "utilities", ] categories = ["asynchronous"] license = "MIT" repository = "https://github.com/NobodyXu/awaitable" [package.metadata.docs.rs] all-features = true rustdoc-args = [ "--cfg", "docsrs", ] [dependencies.awaitable-error] version = "0.1.0" [dependencies.cfg-if] version = "1.0.0" [dependencies.parking_lot] version = "0.12.0" optional = true awaitable-0.4.0/Cargo.toml.orig000064400000000000000000000014261046102023000144500ustar 00000000000000[package] name = "awaitable" version = "0.4.0" edition = "2018" authors = ["Jiahao XU "] license = "MIT" description = "Awaitable type with input and output that can be stored in container." repository = "https://github.com/NobodyXu/awaitable" keywords = ["async", "utilities"] categories = ["asynchronous"] [workspace] members = ["awaitable-error"] [dependencies] awaitable-error = { version = "0.1.0", path = "awaitable-error" } cfg-if = "1.0.0" parking_lot = { version = "0.12.0", optional = true } # docs.rs-specific configuration, shamelessly copied from # https://stackoverflow.com/a/61417700/8375400. [package.metadata.docs.rs] # document all features all-features = true # defines the configuration attribute `docsrs` rustdoc-args = ["--cfg", "docsrs"] awaitable-0.4.0/LICENSE000064400000000000000000000020521046102023000125620ustar 00000000000000MIT License Copyright (c) 2021 Jiahao XU Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. awaitable-0.4.0/README.md000064400000000000000000000010051046102023000130310ustar 00000000000000# awaitable [![Rust](https://github.com/NobodyXu/awaitable/actions/workflows/rust.yml/badge.svg)](https://github.com/NobodyXu/awaitable/actions/workflows/rust.yml) [![crate.io downloads](https://img.shields.io/crates/d/awaitable)](https://crates.io/crates/awaitable) [![crate.io version](https://img.shields.io/crates/v/awaitable)](https://crates.io/crates/awaitable) [![docs](https://docs.rs/awaitable/badge.svg)](https://docs.rs/awaitable) Awaitable type with input and output that can be stored in container. awaitable-0.4.0/src/lib.rs000064400000000000000000000102451046102023000134630ustar 00000000000000#![cfg_attr(docsrs, feature(doc_auto_cfg))] use cfg_if::cfg_if; use std::{mem, task::Waker}; cfg_if! { if #[cfg(feature = "parking_lot")] { use parking_lot::{const_mutex, Mutex}; } else { use std::sync::{Mutex as StdMutex, MutexGuard}; #[derive(Debug)] #[repr(transparent)] struct Mutex(StdMutex); impl Mutex { fn new(val: T) -> Self { Self(StdMutex::new(val)) } #[track_caller] fn lock(&self) -> MutexGuard<'_, T> { self.0.lock().unwrap() } } } } pub use awaitable_error::Error; #[derive(Debug)] enum InnerState { Uninitialized, Ongoing(Option, Option), /// The awaitable is done Done(Output), Consumed, } /// Awaitable guarantees that there is no spurious wakeup #[derive(Debug)] pub struct Awaitable(Mutex>); impl Default for Awaitable { fn default() -> Self { Self::new() } } impl Awaitable { /// Create an uninitialized `Awaitable`. /// /// Must be `reset` before it can be used. pub fn new() -> Self { Self(Mutex::new(InnerState::Uninitialized)) } /// Create an uninitialized `Awaitable`. /// /// Must be `reset` before it can be used. #[cfg(feature = "parking_lot")] pub const fn const_new() -> Self { Self(const_mutex(InnerState::Uninitialized)) } } impl Awaitable { /// Reset `Awaitable` to its initial state. /// /// After this call, `install_waker`, `take_input` and `done` /// can be called. pub fn reset(&self, input: Option) { *self.0.lock() = InnerState::Ongoing(input, None); } /// Return true if the task is already done. /// /// ** /// `install_waker` must not be called after `take_output` is called. /// ** pub fn install_waker(&self, waker: Waker) -> Result { use InnerState::*; let mut guard = self.0.lock(); match &mut *guard { Uninitialized => Err(Error::Uninitialized), Ongoing(_input, stored_waker) => { *stored_waker = Some(waker); Ok(false) } Done(_) => Ok(true), Consumed => Err(Error::AlreadyConsumed), } } /// **`take_input` must not be called after `take_output` is called. pub fn take_input(&self) -> Result, Error> { use InnerState::*; let mut guard = self.0.lock(); match &mut *guard { Uninitialized => Err(Error::Uninitialized), Ongoing(input, _stored_waker) => Ok(input.take()), Done(_) => Ok(None), Consumed => Err(Error::AlreadyConsumed), } } /// `done` must be only called once on one instance of `Awaitable`. /// /// **`done` must not be called after `take_output` is called.** pub fn done(&self, value: Output) -> Result<(), Error> { use InnerState::*; let prev_state = mem::replace(&mut *self.0.lock(), Done(value)); match prev_state { Uninitialized => Err(Error::Uninitialized), Done(_) => Err(Error::AlreadyDone), Ongoing(_input, stored_waker) => { if let Some(waker) = stored_waker { waker.wake(); } Ok(()) } Consumed => Err(Error::AlreadyConsumed), } } /// Return `Some(output)` if the awaitable is done. pub fn take_output(&self) -> Option { use InnerState::*; let prev_state = mem::replace(&mut *self.0.lock(), Consumed); match prev_state { Done(value) => Some(value), _ => None, } } /// Return true if current state is `Done`. pub fn is_done(&self) -> bool { matches!(&*self.0.lock(), InnerState::Done(_)) } /// Return true if current state is `Consumed`. pub fn is_consumed(&self) -> bool { matches!(&*self.0.lock(), InnerState::Consumed) } }