Compare commits

...

6 Commits

Author SHA1 Message Date
John Nunley 6101041f81
v1.1.1
Signed-off-by: John Nunley <dev@notgull.net>
2023-09-25 10:19:08 -07:00
Taiki Endo a1a6224e1d Update actions/checkout action to v4 2023-09-10 18:29:47 +09:00
John Nunley 4c18540fb4
Add smol-rs logo (#8) 2023-07-17 14:30:05 +09:00
John Nunley 2a261c7158
Reimplement using 100% safe code (#7)
Uses the Wake trait to eliminate all unsafe code in this crate.
2023-06-11 08:27:36 -07:00
Taiki Endo 6b47138087 Minimize GITHUB_TOKEN permissions
Refs: https://github.blog/changelog/2021-04-20-github-actions-control-permissions-for-github_token
2023-06-09 00:17:16 +09:00
Taiki Endo 09d952e3e1 Set CARGO_NET_GIT_FETCH_WITH_CLI=true in CI 2023-01-21 20:08:23 +09:00
5 changed files with 40 additions and 39 deletions

View File

@ -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:

View File

@ -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

View File

@ -1,3 +1,7 @@
# Version 1.1.1
- Reimplement using 100% safe code. (#7)
# Version 1.1.0
- Make the crate `#![no_std]`.

View File

@ -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"

View File

@ -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)();
}
}