Skip to content

Commit e965379

Browse files
committed
📝 use Box<Thread> instead of Thread in spsc::Blocker
1 parent 5ecd454 commit e965379

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/sync/spsc.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,37 +69,36 @@ impl<T> EventSource for Park<'_, T> {
6969
}
7070
}
7171

72-
const _: () = assert!(std::mem::size_of::<Thread>() == std::mem::size_of::<usize>());
7372
struct Blocker {
7473
handle: NonZeroUsize,
7574
}
7675

7776
impl Blocker {
7877
#[inline]
7978
fn new_coroutine(co: CoroutineImpl) -> Self {
80-
let handle: NonZeroUsize = unsafe { std::mem::transmute(co) };
79+
let handle = NonZeroUsize::new(co.into_raw() as usize).unwrap();
8180
Blocker { handle }
8281
}
8382

8483
#[inline]
8584
fn new_thread(thread: Thread) -> Self {
86-
let mut handle: NonZeroUsize = unsafe { std::mem::transmute(thread) };
85+
let mut handle = NonZeroUsize::new(Box::into_raw(Box::new(thread)) as usize).unwrap();
8786
handle |= 1;
8887
Blocker { handle }
8988
}
9089

9190
#[inline]
9291
fn into_coroutine(self) -> CoroutineImpl {
93-
let co: CoroutineImpl = unsafe { std::mem::transmute(self.handle) };
92+
let co = unsafe { CoroutineImpl::from_raw(self.handle.get() as *mut _) };
9493
std::mem::forget(self);
9594
co
9695
}
9796

9897
#[inline]
9998
fn into_thread(self) -> Thread {
100-
let thread: Thread = unsafe { std::mem::transmute(self.handle.get() & !1) };
99+
let thread: Box<Thread> = unsafe { Box::from_raw((self.handle.get() & !1) as *mut _) };
101100
std::mem::forget(self);
102-
thread
101+
*thread
103102
}
104103

105104
#[inline]
@@ -120,7 +119,7 @@ impl Drop for Blocker {
120119
// let co = self.into_coroutine();
121120
unreachable!()
122121
} else {
123-
let _thread: Thread = unsafe { std::mem::transmute(self.handle.get() & !1) };
122+
let _thread: Box<Thread> = unsafe { Box::from_raw((self.handle.get() & !1) as *mut _) };
124123
}
125124
}
126125
}

0 commit comments

Comments
 (0)