3: Make use of pin! stabilization r=bertptrs a=bertptrs

With Rust 1.68 `std::pin::pin!` is stable therefore we can now use it to get rid of a pesky heap allocation in the executor.

This requires bumping MSRV to 1.68, but I feel this should be the last time that will happen, so after this I can cut the 1.0.

Co-authored-by: Bert Peters <bert@bertptrs.nl>
This commit is contained in:
bors[bot]
2023-03-11 11:39:18 +00:00
committed by GitHub
5 changed files with 25 additions and 10 deletions

View File

@@ -39,7 +39,7 @@ jobs:
- uses: dtolnay/rust-toolchain@v1 - uses: dtolnay/rust-toolchain@v1
with: with:
toolchain: "1.51" toolchain: "1.68"
- run: cargo test - run: cargo test

View File

@@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Changed
- Use dynamic dispatch internally to save on code gen. External API unchanged.
- No longer heap-allocate futures.
### Breaking
- Minimum supported Rust version bumped to 1.68.
## [0.1.1] - 2022-09-05 ## [0.1.1] - 2022-09-05
### Fixed ### Fixed

View File

@@ -1,12 +1,11 @@
[package] [package]
name = "beul" name = "beul"
version = "0.1.1" version = "0.1.1"
# Edition 2021 is not available in MSRV edition = "2021"
edition = "2018"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
description = "It executes futures" description = "It executes futures"
repository = "https://github.com/bertptrs/beul/" repository = "https://github.com/bertptrs/beul/"
rust-version = "1.51" rust-version = "1.68"
authors = [ authors = [
"Bert Peters", "Bert Peters",
] ]

View File

@@ -1,6 +1,7 @@
# Beul # Beul
Beul is a minimalistic futures executor. No dependencies, no unsafe rust. Beul is a minimalistic futures executor. No dependencies, no unsafe rust. It simply executes
futures.
## Usage ## Usage
@@ -12,8 +13,8 @@ beul::execute(async {});
### Backwards compatibility ### Backwards compatibility
This crate requires at least Rust 1.51, due to its reliance on [Wake]. Increases in this version This crate requires at least Rust 1.68, due to its reliance on [std::pin::pin!]. Increases in this
will be considered breaking changes. This crate follows semantic versioning. version will be considered breaking changes. This crate follows semantic versioning.
### Limitations ### Limitations
@@ -37,4 +38,4 @@ work by you, as defined in the Apache-2.0 license, shall be dual licensed as abo
additional terms or conditions. additional terms or conditions.
[Tokio]: https://tokio.rs/ [Tokio]: https://tokio.rs/
[Wake]: https://doc.rust-lang.org/std/task/trait.Wake.html [std::pin::pin!]: https://doc.rust-lang.org/std/pin/macro.pin.html

View File

@@ -16,6 +16,7 @@
//! ``` //! ```
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
use std::future::Future; use std::future::Future;
use std::pin::Pin;
use std::sync::Arc; use std::sync::Arc;
use std::sync::Condvar; use std::sync::Condvar;
use std::sync::Mutex; use std::sync::Mutex;
@@ -60,10 +61,15 @@ impl Wake for CondvarWake {
} }
/// Block on specified [`Future`]. /// Block on specified [`Future`].
///
/// The future will be polled until completion on the current thread.
pub fn execute<T>(f: impl Future<Output = T>) -> T { pub fn execute<T>(f: impl Future<Output = T>) -> T {
// TODO: replace with std::pin::pin once it gets stabilized // Use dynamic dispatch to save on codegen
let mut pinned = Box::pin(f); poll(std::pin::pin!(f))
}
/// Poll a future until completion.
fn poll<T>(mut pinned: Pin<&mut dyn Future<Output = T>>) -> T {
let wake = Arc::new(CondvarWake::default()); let wake = Arc::new(CondvarWake::default());
let waker = Waker::from(Arc::clone(&wake)); let waker = Waker::from(Arc::clone(&wake));