From 0c77e3d16386b055f4688ce24cf39ae53a5b0ed7 Mon Sep 17 00:00:00 2001 From: Aadhish Sriram S Date: Sun, 8 Oct 2023 10:24:29 +0530 Subject: [PATCH] feat: Implement From for Waker This allows Wakers to be created in an allocation-free manner. --- .github/workflows/ci.yml | 2 +- Cargo.toml | 2 +- src/lib.rs | 22 +++++++++++++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 97e0c85..c6a05df 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,7 +54,7 @@ jobs: matrix: # When updating this, the reminder to update the minimum supported # Rust version in Cargo.toml. - rust: ['1.39'] + rust: ['1.51'] steps: - uses: actions/checkout@v4 - name: Install Rust diff --git a/Cargo.toml b/Cargo.toml index 2f279d8..2b10b06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ authors = [ "The Rust Project Developers", ] edition = "2018" -rust-version = "1.39" +rust-version = "1.51" description = "Thread parking and unparking" license = "Apache-2.0 OR MIT" repository = "https://github.com/smol-rs/parking" diff --git a/src/lib.rs b/src/lib.rs index e1cea97..710673f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,6 +47,8 @@ use loom::sync; use std::cell::Cell; use std::fmt; use std::marker::PhantomData; +use std::sync::Arc; +use std::task::{Wake, Waker}; use std::time::Duration; #[cfg(not(all(loom, feature = "loom")))] @@ -54,7 +56,7 @@ use std::time::Instant; use sync::atomic::AtomicUsize; use sync::atomic::Ordering::SeqCst; -use sync::{Arc, Condvar, Mutex}; +use sync::{Condvar, Mutex}; /// Creates a parker and an associated unparker. /// @@ -304,6 +306,12 @@ impl Clone for Unparker { } } +impl From for Waker { + fn from(up: Unparker) -> Self { + Waker::from(up.inner) + } +} + const EMPTY: usize = 0; const PARKED: usize = 1; const NOTIFIED: usize = 2; @@ -416,3 +424,15 @@ impl Inner { true } } + +impl Wake for Inner { + #[inline] + fn wake(self: Arc) { + self.unpark(); + } + + #[inline] + fn wake_by_ref(self: &Arc) { + self.unpark(); + } +}