Skip to content

Commit

Permalink
feat(maitake-sync): rename EnqueueWait to Subscribe
Browse files Browse the repository at this point in the history
`maitake-sync`'s `waitmap::Wait' future has an `enqueue` method that
does the same thing as the `subscribe` methods on `WaitCell` and
`WaitQueue`...but has a different name. I think this might be due to
`enqueue` being added earlier or something, or maybe we just weren't
paying attention. Regardless, I figured it was nicer to use the same
name for the method that does the same thing on these different types.

This commit renames the `waitmap::Wait::enqueue` method to `subscribe`,
and the future it returns from `EnqueueWait` to `Subscribe`, analogously
to `WaitQueue` and `WaitCell`. I've left a deprecated type alias and a
deprecated function alias in place, so this isn't a breaking change:
users who reference `Wait::enqueue` or `EnqueueWait` will still be able
to compile their code, but they'll get a warning.

cc @jamesmunns, since I believe you added this API?
  • Loading branch information
hawkw committed Jul 20, 2024
1 parent b2a08e5 commit d7603f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
28 changes: 23 additions & 5 deletions maitake-sync/src/wait_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl<'map, 'wait, K: PartialEq, V> Wait<'map, K, V> {
/// // first pin the `wait` future, to ensure that it does not move
/// // until it has been completed.
/// pin_mut!(wait);
/// wait.as_mut().enqueue().await.unwrap();
/// wait.as_mut().subscribe().await.unwrap();
///
/// // We now know the waiter has been enqueued, at this point we could
/// // send a message that will cause key == 0 to be returned, without
Expand All @@ -285,8 +285,18 @@ impl<'map, 'wait, K: PartialEq, V> Wait<'map, K, V> {
///
/// assert!(matches!(q.wake(&0, 100), WakeOutcome::Woke));
/// ```
pub fn subscribe(self: Pin<&'wait mut Self>) -> Subscribe<'wait, 'map, K, V> {
Subscribe { wait: self }
}

/// Deprecated alias for [`Wait::subscribe`]. See that method for details.
#[deprecated(
since = "0.1.3",
note = "renamed to `subscribe` for consistency, use that instead"
)]
#[allow(deprecated)] // let us use the deprecated type alias
pub fn enqueue(self: Pin<&'wait mut Self>) -> EnqueueWait<'wait, 'map, K, V> {
EnqueueWait { wait: self }
self.subscribe()
}
}

Expand Down Expand Up @@ -662,14 +672,14 @@ feature! {

/// A future that ensures a [`Wait`] has been added to a [`WaitMap`].
///
/// See [`Wait::enqueue`] for more information and usage example.
/// See [`Wait::subscribe`] for more information and usage example.
#[must_use = "futures do nothing unless `.await`ed or `poll`ed"]
#[derive(Debug)]
pub struct EnqueueWait<'a, 'b, K: PartialEq, V> {
pub struct Subscribe<'a, 'b, K: PartialEq, V> {
wait: Pin<&'a mut Wait<'b, K, V>>,
}

impl<'a, 'b, K: PartialEq, V> Future for EnqueueWait<'a, 'b, K, V> {
impl<'a, 'b, K: PartialEq, V> Future for Subscribe<'a, 'b, K, V> {
type Output = WaitResult<()>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Expand All @@ -682,6 +692,14 @@ impl<'a, 'b, K: PartialEq, V> Future for EnqueueWait<'a, 'b, K, V> {
}
}

/// Deprecated alias for [`Subscribe`]. See the [`Wait::subscribe`]
/// documentation for more details.
#[deprecated(
since = "0.1.3",
note = "renamed to `Subscribe` for consistency, use that instead"
)]
pub type EnqueueWait<'a, 'b, K, V> = Subscribe<'a, 'b, K, V>;

impl<K: PartialEq, V> Waiter<K, V> {
/// Wake the task that owns this `Waiter`.
///
Expand Down
6 changes: 3 additions & 3 deletions maitake-sync/src/wait_map/tests/alloc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn enqueue() {
let wait = q.wait(1);

pin_mut!(wait);
wait.as_mut().enqueue().await.unwrap();
wait.as_mut().subscribe().await.unwrap();
ENQUEUED.fetch_add(1, Ordering::Relaxed);

let val = wait.await.unwrap();
Expand Down Expand Up @@ -81,7 +81,7 @@ fn duplicate() {
let wait = q.wait(0);

pin_mut!(wait);
wait.as_mut().enqueue().await.unwrap();
wait.as_mut().subscribe().await.unwrap();
ENQUEUED.fetch_add(1, Ordering::Relaxed);

let val = wait.await.unwrap();
Expand All @@ -98,7 +98,7 @@ fn duplicate() {
let wait = q.wait(0);

pin_mut!(wait);
wait.as_mut().enqueue().await
wait.as_mut().subscribe().await
}
});

Expand Down

0 comments on commit d7603f2

Please sign in to comment.