rust-lang / rust-lang/rust

Allow `Waker::from` to accept trait object `Arc<dyn Wake>`

Open
#132,339 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-feature-request T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I don't know it this issue is duplicate.

Consider this code snippet

use std::{sync::Arc, task::{Wake, Waker}};

trait Foo: Send + Sync {}
impl Wake for dyn Foo {
    fn wake(self: Arc<Self>) {
        todo!()
    }
}

struct Object;
impl Foo for Object {}

fn main() {
    let foo = Arc::new(Object) as Arc<dyn Foo>;
    Waker::from(foo);
}

Currently this code doesn't work, Because Waker::from cannot be used with Arc<dyn Wake>

https://github.com/rust-lang/rust/blob/1e4f10ba6476e48a42a79b9f846a2d9366525b9e/library/alloc/src/task.rs#L109

It would be ideal if Waker::from could support W: ?Sized objects by relaxing the Sized bound on W.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open library/alloc/src/task.rs at the linked Waker::from implementation and reproduce the provided Rust snippet on stable to confirm the current failure. Trace the conversion's bounds and update it so Arc is accepted; done when the snippet compiles and existing standard-library checks remain passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend-api-design
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.