# How \`Waker::wake\` in std consume the waker

**URL:** <https://internals.rust-lang.org/t/how-waker-wake-in-std-consume-the-waker/17104>\
**Category:** Uncategorized\
**Created:** [July 30, 2022, 7:03pm UTC](https://internals.rust-lang.org/t/how-waker-wake-in-std-consume-the-waker/17104 "2022-07-30T19:03:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gftea](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gftea/32/9473_2.png) [@gftea](https://internals.rust-lang.org/u/gftea)\
**Post date:** [July 30, 2022, 7:03pm UTC](https://internals.rust-lang.org/t/how-waker-wake-in-std-consume-the-waker/17104/1 "2022-07-30T19:03:53Z")

</div>

I do not understand below comments, how waker is consumed by calling `(wake)(data)`?

> ```
> // Don't call `drop` -- the waker will be consumed by `wake`.
> 
> ```

```rust
    pub fn wake(self) {
        // The actual wakeup call is delegated through a virtual function call
        // to the implementation which is defined by the executor.
        let wake = self.waker.vtable.wake;
        let data = self.waker.data;

        // Don't call `drop` -- the waker will be consumed by `wake`.
        crate::mem::forget(self);

        // SAFETY: This is safe because `Waker::from_raw` is the only way
        // to initialize `wake` and `data` requiring the user to acknowledge
        // that the contract of `RawWaker` is upheld.
        unsafe { (wake)(data) };
    }

```

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)\
**Post date:** [July 30, 2022, 7:38pm UTC](https://internals.rust-lang.org/t/how-waker-wake-in-std-consume-the-waker/17104/2 "2022-07-30T19:38:39Z")

</div>

Unlike an ordinary Rust move, this is not determined by Rust language rules, but by the specification of the [`RawWakerVTable`](https://doc.rust-lang.org/std/task/struct.RawWakerVTable.html):

> [`wake`](https://doc.rust-lang.org/std/task/struct.RawWakerVTable.html#wake)
> 
> This function will be called when `wake` is called on the [`Waker`](https://doc.rust-lang.org/std/task/struct.Waker.html). It must wake up the task associated with this [`RawWaker`](https://doc.rust-lang.org/std/task/struct.RawWaker.html).
> 
> The implementation of this function must make sure to release any resources that are associated with this instance of a [`RawWaker`](https://doc.rust-lang.org/std/task/struct.RawWaker.html) and associated task.

“Must make sure to release any resources” is another way of describing the concept meant by “consumes” — it is what must be done at most once (and preferably exactly once) in all possible code paths. So, the code you quoted calls `forget` on the `Waker` to ensure that it doesn't end up calling the vtable's `drop()` (via `impl Drop for Waker`) _in addition to_ calling the vtable's `wake()`.

---

<div class="post-metadata">

**Author:** ![gftea](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gftea/32/9473_2.png) [@gftea](https://internals.rust-lang.org/u/gftea)\
**Post date:** [July 30, 2022, 7:50pm UTC](https://internals.rust-lang.org/t/how-waker-wake-in-std-consume-the-waker/17104/3 "2022-07-30T19:50:51Z")

</div>

@kpreid , thanks, so it leaves the implementor to clean up the resources which is pointed by pointer `*const ()`, right?

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)\
**Post date:** [July 30, 2022, 8:03pm UTC](https://internals.rust-lang.org/t/how-waker-wake-in-std-consume-the-waker/17104/4 "2022-07-30T20:03:35Z")

</div>

The implementor of the waker, who provides the function pointers to the `RawWakerVTable`, yes.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 28, 2022, 8:04pm UTC](https://internals.rust-lang.org/t/how-waker-wake-in-std-consume-the-waker/17104/5 "2022-10-28T20:04:24Z")

</div>

Closed on October 28, 2022, 8:04pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
