Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add portable-atomic feature #10

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
if: startsWith(matrix.rust, 'nightly')
run: cargo check -Z features=dev_dep
- run: cargo test
- run: cargo test --features portable-atomic

msrv:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -82,6 +83,10 @@ jobs:
env:
MIRIFLAGS: -Zmiri-strict-provenance -Zmiri-symbolic-alignment-check -Zmiri-disable-isolation
RUSTFLAGS: ${{ env.RUSTFLAGS }} -Z randomize-layout
- run: cargo miri test --features portable-atomic
env:
MIRIFLAGS: -Zmiri-strict-provenance -Zmiri-symbolic-alignment-check -Zmiri-disable-isolation
RUSTFLAGS: ${{ env.RUSTFLAGS }} -Z randomize-layout

security_audit:
permissions:
Expand Down
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ documentation = "https://docs.rs/waker-fn"
keywords = ["async", "waker", "wake", "closure", "callback"]
categories = ["concurrency"]
exclude = ["/.*"]

[features]
# Uses portable-atomic polyfill atomics on targets without them
portable-atomic = ["portable-atomic-util"]

[dependencies]
portable-atomic-util = { version = "0.2", optional = true, default-features = false, features = ["alloc"] }
21 changes: 19 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
html_logo_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
)]

#[cfg(not(feature = "portable-atomic"))]
extern crate alloc;

use alloc::sync::Arc;
use alloc::task::Wake;
#[cfg(not(feature = "portable-atomic"))]
use alloc::{sync::Arc, task::Wake};
use core::task::Waker;
#[cfg(feature = "portable-atomic")]
use portable_atomic_util::{task::Wake, Arc};

/// Converts a closure into a [`Waker`].
///
Expand All @@ -38,6 +41,7 @@ pub fn waker_fn<F: Fn() + Send + Sync + 'static>(f: F) -> Waker {

struct Helper<F>(F);

#[cfg(not(feature = "portable-atomic"))]
impl<F: Fn() + Send + Sync + 'static> Wake for Helper<F> {
fn wake(self: Arc<Self>) {
(self.0)();
Expand All @@ -47,3 +51,16 @@ impl<F: Fn() + Send + Sync + 'static> Wake for Helper<F> {
(self.0)();
}
}
// Note: Unlike std::task::Wake, all methods take `this:` instead of `self:`.
// This is because using portable_atomic_util::Arc as a receiver requires the
// unstable arbitrary_self_types feature.
#[cfg(feature = "portable-atomic")]
impl<F: Fn() + Send + Sync + 'static> Wake for Helper<F> {
fn wake(this: Arc<Self>) {
(this.0)();
}

fn wake_by_ref(this: &Arc<Self>) {
(this.0)();
}
}
Loading