mirror of https://github.com/smol-rs/waker-fn
Compare commits
6 Commits
8299fa7bba
...
6101041f81
Author | SHA1 | Date |
---|---|---|
John Nunley | 6101041f81 | |
Taiki Endo | a1a6224e1d | |
John Nunley | 4c18540fb4 | |
John Nunley | 2a261c7158 | |
Taiki Endo | 6b47138087 | |
Taiki Endo | 09d952e3e1 |
|
@ -1,5 +1,8 @@
|
|||
name: CI
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
|
@ -10,6 +13,7 @@ on:
|
|||
|
||||
env:
|
||||
CARGO_INCREMENTAL: 0
|
||||
CARGO_NET_GIT_FETCH_WITH_CLI: true
|
||||
CARGO_NET_RETRY: 10
|
||||
CARGO_TERM_COLOR: always
|
||||
RUST_BACKTRACE: 1
|
||||
|
@ -30,7 +34,7 @@ jobs:
|
|||
os: [ubuntu-latest]
|
||||
rust: [nightly, beta, stable]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Rust
|
||||
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
|
||||
- run: cargo build --all --all-features --all-targets
|
||||
|
@ -45,9 +49,9 @@ jobs:
|
|||
matrix:
|
||||
# When updating this, the reminder to update the minimum supported
|
||||
# Rust version in Cargo.toml.
|
||||
rust: ['1.36']
|
||||
rust: ['1.51']
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Rust
|
||||
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
|
||||
- run: cargo build
|
||||
|
@ -55,7 +59,7 @@ jobs:
|
|||
clippy:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Rust
|
||||
run: rustup update stable
|
||||
- run: cargo clippy --all-features --all-targets
|
||||
|
@ -63,7 +67,7 @@ jobs:
|
|||
fmt:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Rust
|
||||
run: rustup update stable
|
||||
- run: cargo fmt --all --check
|
||||
|
@ -71,7 +75,7 @@ jobs:
|
|||
miri:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Rust
|
||||
run: rustup toolchain install nightly --component miri && rustup default nightly
|
||||
- run: cargo miri test
|
||||
|
@ -80,9 +84,13 @@ jobs:
|
|||
RUSTFLAGS: ${{ env.RUSTFLAGS }} -Z randomize-layout
|
||||
|
||||
security_audit:
|
||||
permissions:
|
||||
checks: write
|
||||
contents: read
|
||||
issues: write
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
# https://github.com/rustsec/audit-check/issues/2
|
||||
- uses: rustsec/audit-check@master
|
||||
with:
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
name: Release
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
|
@ -10,7 +13,7 @@ jobs:
|
|||
if: github.repository_owner == 'smol-rs'
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- uses: taiki-e/create-gh-release-action@v1
|
||||
with:
|
||||
changelog: CHANGELOG.md
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
# Version 1.1.1
|
||||
|
||||
- Reimplement using 100% safe code. (#7)
|
||||
|
||||
# Version 1.1.0
|
||||
|
||||
- Make the crate `#![no_std]`.
|
||||
|
|
|
@ -3,10 +3,10 @@ name = "waker-fn"
|
|||
# When publishing a new version:
|
||||
# - Update CHANGELOG.md
|
||||
# - Create "v1.x.y" git tag
|
||||
version = "1.1.0"
|
||||
version = "1.1.1"
|
||||
authors = ["Stjepan Glavina <stjepang@gmail.com>"]
|
||||
edition = "2018"
|
||||
rust-version = "1.36"
|
||||
rust-version = "1.51"
|
||||
description = "Convert closures into wakers"
|
||||
license = "Apache-2.0 OR MIT"
|
||||
repository = "https://github.com/smol-rs/waker-fn"
|
||||
|
|
44
src/lib.rs
44
src/lib.rs
|
@ -3,13 +3,20 @@
|
|||
//! A [`Waker`] is just a fancy callback. This crate converts regular closures into wakers.
|
||||
|
||||
#![no_std]
|
||||
#![forbid(unsafe_code)]
|
||||
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
|
||||
#![doc(
|
||||
html_favicon_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
|
||||
)]
|
||||
#![doc(
|
||||
html_logo_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
|
||||
)]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::sync::Arc;
|
||||
use core::mem::{self, ManuallyDrop};
|
||||
use core::task::{RawWaker, RawWakerVTable, Waker};
|
||||
use alloc::task::Wake;
|
||||
use core::task::Waker;
|
||||
|
||||
/// Converts a closure into a [`Waker`].
|
||||
///
|
||||
|
@ -26,38 +33,17 @@ use core::task::{RawWaker, RawWakerVTable, Waker};
|
|||
/// waker.wake(); // Prints "woken".
|
||||
/// ```
|
||||
pub fn waker_fn<F: Fn() + Send + Sync + 'static>(f: F) -> Waker {
|
||||
let raw = Arc::into_raw(Arc::new(f)) as *const ();
|
||||
let vtable = &Helper::<F>::VTABLE;
|
||||
unsafe { Waker::from_raw(RawWaker::new(raw, vtable)) }
|
||||
Waker::from(Arc::new(Helper(f)))
|
||||
}
|
||||
|
||||
struct Helper<F>(F);
|
||||
|
||||
impl<F: Fn() + Send + Sync + 'static> Helper<F> {
|
||||
const VTABLE: RawWakerVTable = RawWakerVTable::new(
|
||||
Self::clone_waker,
|
||||
Self::wake,
|
||||
Self::wake_by_ref,
|
||||
Self::drop_waker,
|
||||
);
|
||||
|
||||
unsafe fn clone_waker(ptr: *const ()) -> RawWaker {
|
||||
let arc = ManuallyDrop::new(Arc::from_raw(ptr as *const F));
|
||||
let _arc_clone: mem::ManuallyDrop<_> = arc.clone();
|
||||
RawWaker::new(ptr, &Self::VTABLE)
|
||||
impl<F: Fn() + Send + Sync + 'static> Wake for Helper<F> {
|
||||
fn wake(self: Arc<Self>) {
|
||||
(self.0)();
|
||||
}
|
||||
|
||||
unsafe fn wake(ptr: *const ()) {
|
||||
let arc = Arc::from_raw(ptr as *const F);
|
||||
(arc)();
|
||||
}
|
||||
|
||||
unsafe fn wake_by_ref(ptr: *const ()) {
|
||||
let arc = ManuallyDrop::new(Arc::from_raw(ptr as *const F));
|
||||
(arc)();
|
||||
}
|
||||
|
||||
unsafe fn drop_waker(ptr: *const ()) {
|
||||
drop(Arc::from_raw(ptr as *const F));
|
||||
fn wake_by_ref(self: &Arc<Self>) {
|
||||
(self.0)();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue