Skip to content

Commit 6d9b220

Browse files
committed
fmt
1 parent 199581e commit 6d9b220

12 files changed

Lines changed: 460 additions & 273 deletions

File tree

benches/basic.rs

Lines changed: 157 additions & 64 deletions
Large diffs are not rendered by default.

examples/select.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ fn main() {
88
let (blue_tx, blue_rx) = flume::unbounded();
99

1010
// Spawn two threads that each send a message into their respective channel
11-
std::thread::spawn(move || { let _ = red_tx.send("Red"); });
12-
std::thread::spawn(move || { let _ = blue_tx.send("Blue"); });
11+
std::thread::spawn(move || {
12+
let _ = red_tx.send("Red");
13+
});
14+
std::thread::spawn(move || {
15+
let _ = blue_tx.send("Blue");
16+
});
1317

1418
// Race them to see which one sends their message first
1519
let winner = Selector::new()

src/async.rs

Lines changed: 72 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
//! Futures and other types that allow asynchronous interaction with channels.
22
3+
use crate::*;
4+
use futures_core::{
5+
future::FusedFuture,
6+
stream::{FusedStream, Stream},
7+
};
8+
use futures_sink::Sink;
9+
use spin1::Mutex as Spinlock;
10+
use std::fmt::{Debug, Formatter};
311
use std::{
12+
any::Any,
413
future::Future,
14+
ops::Deref,
515
pin::Pin,
616
task::{Context, Poll, Waker},
7-
any::Any,
8-
ops::Deref,
917
};
10-
use std::fmt::{Debug, Formatter};
11-
use crate::*;
12-
use futures_core::{stream::{Stream, FusedStream}, future::FusedFuture};
13-
use futures_sink::Sink;
14-
use spin1::Mutex as Spinlock;
1518

1619
struct AsyncSignal {
1720
waker: Spinlock<Waker>,
@@ -36,8 +39,12 @@ impl Signal for AsyncSignal {
3639
self.stream
3740
}
3841

39-
fn as_any(&self) -> &(dyn Any + 'static) { self }
40-
fn as_ptr(&self) -> *const () { self as *const _ as *const () }
42+
fn as_any(&self) -> &(dyn Any + 'static) {
43+
self
44+
}
45+
fn as_ptr(&self) -> *const () {
46+
self as *const _ as *const ()
47+
}
4148
}
4249

4350
impl<T> Hook<T, AsyncSignal> {
@@ -156,9 +163,11 @@ impl<'a, T> SendFut<'a, T> {
156163
fn reset_hook(&mut self) {
157164
if let Some(SendState::QueuedItem(hook)) = self.hook.take() {
158165
let hook: Arc<Hook<T, dyn Signal>> = hook;
159-
wait_lock(&self.sender.shared.chan).sending
166+
wait_lock(&self.sender.shared.chan)
167+
.sending
160168
.as_mut()
161-
.unwrap().1
169+
.unwrap()
170+
.1
162171
.retain(|s| s.signal().as_ptr() != hook.signal().as_ptr());
163172
}
164173
}
@@ -195,7 +204,6 @@ impl<'a, T> Drop for SendFut<'a, T> {
195204
}
196205
}
197206

198-
199207
impl<'a, T> Future for SendFut<'a, T> {
200208
type Output = Result<(), SendError<T>>;
201209

@@ -218,24 +226,28 @@ impl<'a, T> Future for SendFut<'a, T> {
218226
let this = self.get_mut();
219227
let (shared, this_hook) = (&this.sender.shared, &mut this.hook);
220228

221-
shared.send(
222-
// item
223-
item,
224-
// should_block
225-
true,
226-
// make_signal
227-
|msg| Hook::slot(Some(msg), AsyncSignal::new(cx, false)),
228-
// do_block
229-
|hook| {
230-
*this_hook = Some(SendState::QueuedItem(hook));
231-
Poll::Pending
232-
},
233-
)
234-
.map(|r| r.map_err(|err| match err {
235-
TrySendTimeoutError::Disconnected(msg) => SendError(msg),
236-
_ => unreachable!(),
237-
}))
238-
} else { // Nothing to do
229+
shared
230+
.send(
231+
// item
232+
item,
233+
// should_block
234+
true,
235+
// make_signal
236+
|msg| Hook::slot(Some(msg), AsyncSignal::new(cx, false)),
237+
// do_block
238+
|hook| {
239+
*this_hook = Some(SendState::QueuedItem(hook));
240+
Poll::Pending
241+
},
242+
)
243+
.map(|r| {
244+
r.map_err(|err| match err {
245+
TrySendTimeoutError::Disconnected(msg) => SendError(msg),
246+
_ => unreachable!(),
247+
})
248+
})
249+
} else {
250+
// Nothing to do
239251
Poll::Ready(Ok(()))
240252
}
241253
}
@@ -378,8 +390,16 @@ impl<'a, T> RecvFut<'a, T> {
378390
let hook: Arc<Hook<T, dyn Signal>> = hook;
379391
let mut chan = wait_lock(&self.receiver.shared.chan);
380392
// We'd like to use `Arc::ptr_eq` here but it doesn't seem to work consistently with wide pointers?
381-
chan.waiting.retain(|s| s.signal().as_ptr() != hook.signal().as_ptr());
382-
if hook.signal().as_any().downcast_ref::<AsyncSignal>().unwrap().woken.load(Ordering::SeqCst) {
393+
chan.waiting
394+
.retain(|s| s.signal().as_ptr() != hook.signal().as_ptr());
395+
if hook
396+
.signal()
397+
.as_any()
398+
.downcast_ref::<AsyncSignal>()
399+
.unwrap()
400+
.woken
401+
.load(Ordering::SeqCst)
402+
{
383403
// If this signal has been fired, but we're being dropped (and so not listening to it),
384404
// pass the signal on to another receiver
385405
chan.try_wake_receiver_if_pending();
@@ -428,21 +448,24 @@ impl<'a, T> RecvFut<'a, T> {
428448
let mut_self = self.get_mut();
429449
let (shared, this_hook) = (&mut_self.receiver.shared, &mut mut_self.hook);
430450

431-
shared.recv(
432-
// should_block
433-
true,
434-
// make_signal
435-
|| Hook::trigger(AsyncSignal::new(cx, stream)),
436-
// do_block
437-
|hook| {
438-
*this_hook = Some(hook);
439-
Poll::Pending
440-
},
441-
)
442-
.map(|r| r.map_err(|err| match err {
443-
TryRecvTimeoutError::Disconnected => RecvError::Disconnected,
444-
_ => unreachable!(),
445-
}))
451+
shared
452+
.recv(
453+
// should_block
454+
true,
455+
// make_signal
456+
|| Hook::trigger(AsyncSignal::new(cx, stream)),
457+
// do_block
458+
|hook| {
459+
*this_hook = Some(hook);
460+
Poll::Pending
461+
},
462+
)
463+
.map(|r| {
464+
r.map_err(|err| match err {
465+
TryRecvTimeoutError::Disconnected => RecvError::Disconnected,
466+
_ => unreachable!(),
467+
})
468+
})
446469
}
447470
}
448471

@@ -551,7 +574,8 @@ impl<'a, T> Stream for RecvStream<'a, T> {
551574
type Item = T;
552575

553576
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
554-
match Pin::new(&mut self.0).poll_inner(cx, true) { // stream = true
577+
match Pin::new(&mut self.0).poll_inner(cx, true) {
578+
// stream = true
555579
Poll::Pending => Poll::Pending,
556580
Poll::Ready(item) => {
557581
self.0.reset_hook();

0 commit comments

Comments
 (0)