Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 0cb5e2f

Browse files
committed
perf: move null check from local_wake() to build()
1 parent 60a0819 commit 0cb5e2f

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

library/core/src/task/wake.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl RawWakerVTable {
195195
#[lang = "Context"]
196196
pub struct Context<'a> {
197197
waker: Option<&'a Waker>,
198-
local_waker: Option<&'a LocalWaker>,
198+
local_waker: &'a LocalWaker,
199199
// Ensure we future-proof against variance changes by forcing
200200
// the lifetime to be invariant (argument-position lifetimes
201201
// are contravariant while return-position lifetimes are
@@ -238,11 +238,7 @@ impl<'a> Context<'a> {
238238
/// Returns a reference to the [`LocalWaker`] for the current task.
239239
#[unstable(feature = "local_waker", issue = "none")]
240240
pub fn local_waker(&self) -> &'a LocalWaker {
241-
// Safety:
242-
// It is safe to transmute a `&Waker` into a `&LocalWaker` since both are a transparent
243-
// wrapper around a local waker. Also, the Option<&Waker> here cannot be None since it is
244-
// impossible to construct a Context without any waker set.
245-
self.local_waker.unwrap_or_else(|| unsafe { transmute(self.waker) })
241+
&self.local_waker
246242
}
247243
}
248244

@@ -325,7 +321,7 @@ impl<'a> ContextBuilder<'a> {
325321
}
326322

327323
/// Builds the `Context`.
328-
///
324+
///
329325
/// # Panics
330326
/// Panics if no `Waker` or `LocalWaker` is set.
331327
#[inline]
@@ -337,6 +333,16 @@ impl<'a> ContextBuilder<'a> {
337333
waker.is_some() || local_waker.is_some(),
338334
"at least one waker must be set with either the `local_waker` or `waker` methods on `ContextBuilder`."
339335
);
336+
let local_waker = match local_waker {
337+
Some(local_waker) => local_waker,
338+
None => {
339+
// SAFETY:
340+
// It is safe to transmute a `&Waker` into a `&LocalWaker` since both are a transparent
341+
// wrapper around a local waker. Also, the Option<&Waker> here cannot be None because
342+
// of the previous assert.
343+
unsafe { transmute(self.waker) }
344+
}
345+
};
340346
Context { waker, local_waker, _marker: PhantomData, _marker2: PhantomData }
341347
}
342348
}
@@ -576,9 +582,10 @@ impl fmt::Debug for Waker {
576582
/// return Poll::Ready(())
577583
/// })
578584
/// }
579-
/// # async {
585+
/// # #[allow(unused_must_use)]
586+
/// # async fn __() {
580587
/// yield_now().await;
581-
/// # };
588+
/// # }
582589
/// ```
583590
///
584591
/// [`Future::poll()`]: core::future::Future::poll

0 commit comments

Comments
 (0)