Simplify the mailbox code

This commit is contained in:
numzero 2025-01-11 00:25:19 +03:00
parent 8007922721
commit 644b200086

View File

@ -28,22 +28,18 @@ impl<'a, T> Future for MailboxPut<'a, T> {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut mb = self.0 .0.lock().unwrap(); let mut mb = self.0 .0.lock().unwrap();
match mb.value { let None = mb.value else {
None => { mb.waker = Some(cx.waker().clone());
let waker = mb.waker.take(); return Poll::Pending;
};
mb.value = self.1.take(); mb.value = self.1.take();
let waker = mb.waker.take();
drop(mb); drop(mb);
if let Some(waker) = waker { if let Some(waker) = waker {
waker.wake(); waker.wake();
} }
Poll::Ready(()) Poll::Ready(())
} }
Some(_) => {
mb.waker = Some(cx.waker().clone());
Poll::Pending
}
}
}
} }
struct MailboxGet<'a, T>(&'a Mailbox<T>); struct MailboxGet<'a, T>(&'a Mailbox<T>);
@ -52,12 +48,10 @@ impl<'a, T> Future for MailboxGet<'a, T> {
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut mb = self.0 .0.lock().unwrap(); let mut mb = self.0 .0.lock().unwrap();
match mb.value.take() { let Some(value) = mb.value.take() else {
None => {
mb.waker = Some(cx.waker().clone()); mb.waker = Some(cx.waker().clone());
Poll::Pending return Poll::Pending;
} };
Some(value) => {
let waker = mb.waker.take(); let waker = mb.waker.take();
drop(mb); drop(mb);
if let Some(waker) = waker { if let Some(waker) = waker {
@ -65,8 +59,6 @@ impl<'a, T> Future for MailboxGet<'a, T> {
} }
Poll::Ready(value) Poll::Ready(value)
} }
}
}
} }
impl<T> Mailbox<T> { impl<T> Mailbox<T> {