ruloom/
thread_local_yielder.rs1use std::{cell::Cell, mem};
2
3use corosensei::Yielder;
4
5pub(crate) type UnitYielder = Yielder<(), ()>;
6
7pub(crate) struct ThreadLocalYielder;
8
9impl ThreadLocalYielder {
10 #[inline]
11 pub unsafe fn set(yielder: &UnitYielder) {
12 let static_yielder: &'static Yielder<(), ()> = unsafe { mem::transmute(yielder) };
13 YIELDER.with(|cell| {
14 cell.set(Some(static_yielder));
15 });
16 }
17
18 #[inline]
19 pub fn remove() {
20 YIELDER.with(|cell| {
21 cell.set(None);
22 });
23 }
24
25 #[inline]
26 pub unsafe fn get_expect_present() -> &'static UnitYielder {
27 YIELDER.with(|cell| {
28 cell.get()
29 .expect("There's no yielder for the current thread.")
30 })
31 }
32}
33
34thread_local! {
35 static YIELDER: Cell<Option<&'static UnitYielder>> = const { Cell::new(None) };
36}