Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use nightly waker_getters APIs #3059

Merged
merged 3 commits into from
Jun 12, 2024
Merged

Conversation

zjp-CN
Copy link
Contributor

@zjp-CN zjp-CN commented Jun 9, 2024

Since rust-lang/rust#96992 has stalled, to prevent potential unsoundness caused by transmuting to &WakerHack, we can use nightly waker_getters APIs by gating it behind nightly feature in embassy-executor without waiting for it to be stablized.

Since rust-lang/rust#96992 has stalled,
to prevent potential unsoundness caused by transmuting to &WakerHack,
we can use nightly waker_getters APIs by gating it behind nightly
feature in embassy-executor without waiting for it to be stablized.
Copy link
Member

@Dirbaio Dirbaio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@Dirbaio Dirbaio added this pull request to the merge queue Jun 12, 2024
Merged via the queue into embassy-rs:main with commit e80ca5f Jun 12, 2024
6 checks passed
@zjp-CN
Copy link
Contributor Author

zjp-CN commented Jun 12, 2024

Sorry, I realized I could make the cfg code better

pub fn task_from_waker(waker: &Waker) -> TaskRef {
    let (vtable, data) = {
        #[cfg(not(feature = "nightly"))]
        {
            struct WakerHack {
                data: *const (),
                vtable: &'static RawWakerVTable,
            }

            // safety: OK because WakerHack has the same layout as Waker.
            // This is not really guaranteed because the structs are `repr(Rust)`, it is
            // indeed the case in the current implementation.
            // TODO use waker_getters when stable. https://github.com/rust-lang/rust/issues/96992
            let hack: &WakerHack = unsafe { core::mem::transmute(waker) };
            (hack.vtable, hack.data)
        }
        #[cfg(feature = "nightly")]
        {
            let raw_waker = waker.as_raw();
            (raw_waker.vtable(), raw_waker.data())
        }
    };

    if vtable != &VTABLE {
        panic!("Found waker not created by the Embassy executor. `embassy_time::Timer` only works with the Embassy executor.")
    }
    // safety: our wakers are always created with `TaskRef::as_ptr`
    unsafe { TaskRef::from_ptr(data as *const TaskHeader) }
}

But it's now merged...

@Dirbaio
Copy link
Member

Dirbaio commented Jun 12, 2024

oh, that's true! can you send it in another PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants