Skip to content

Commit 26af954

Browse files
committed
use crate::sync primitives to implement Promise
1 parent 53c753e commit 26af954

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/promise.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use std::{
22
future::Future,
33
mem,
44
pin::Pin,
5-
sync::{Arc, Condvar, Mutex},
5+
sync::Arc,
66
task::{Context, Poll, Waker},
77
};
88

9-
// FIXME: use locking primitives from `crate::sync`
9+
use crate::sync::{Condvar, Mutex};
1010

1111
/// Creates a connected pair of [`Promise`] and [`PromiseHandle`].
1212
#[doc(alias = "oneshot")]
@@ -51,7 +51,7 @@ impl<T> Drop for Promise<T> {
5151
return;
5252
}
5353

54-
*self.inner.state.lock().unwrap() = PromiseState::Dropped;
54+
*self.inner.state.lock() = PromiseState::Dropped;
5555
self.inner.condvar.notify_one();
5656
}
5757
}
@@ -68,7 +68,7 @@ impl<T> Promise<T> {
6868
// This ignores errors. The assumption is that the thread will exit once it tries to obtain
6969
// a new `Promise` to fulfill.
7070
self.fulfilled = true;
71-
let mut guard = self.inner.state.lock().unwrap();
71+
let mut guard = self.inner.state.lock();
7272
let wakers = match &mut *guard {
7373
PromiseState::Empty(wakers) => mem::take(wakers),
7474
_ => unreachable!(),
@@ -103,10 +103,10 @@ impl<T> PromiseHandle<T> {
103103
///
104104
/// [`Worker`]: crate::Worker
105105
pub fn block(self) -> Result<T, PromiseDropped> {
106-
let mut state = self.inner.state.lock().unwrap();
106+
let mut state = self.inner.state.lock();
107107
loop {
108108
match *state {
109-
PromiseState::Empty(_) => state = self.inner.condvar.wait(state).unwrap(),
109+
PromiseState::Empty(_) => state = self.inner.condvar.wait(state),
110110
PromiseState::Fulfilled(_) => {
111111
let fulfilled = mem::replace(&mut *state, PromiseState::Dropped);
112112
match fulfilled {
@@ -130,7 +130,7 @@ impl<T> PromiseHandle<T> {
130130
impl<T> Future for Waiter<T> {
131131
type Output = Result<T, PromiseDropped>;
132132
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
133-
let mut state = self.0.inner.state.lock().unwrap();
133+
let mut state = self.0.inner.state.lock();
134134
match &mut *state {
135135
PromiseState::Empty(wakers) => {
136136
wakers.push(cx.waker().clone());

0 commit comments

Comments
 (0)