feat: Implement From<Unparker> for Waker

This allows Wakers to be created in an allocation-free manner.
This commit is contained in:
Aadhish Sriram S 2023-10-08 10:24:29 +05:30 committed by GitHub
parent 9545bcbb41
commit 0c77e3d163
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 3 deletions

View File

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

View File

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

View File

@ -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<Unparker> 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>) {
self.unpark();
}
#[inline]
fn wake_by_ref(self: &Arc<Self>) {
self.unpark();
}
}