-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from matheus-consoli/move-test-channel-to-utils
Move test channel to utils
- Loading branch information
Showing
4 changed files
with
81 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use std::{ | ||
cell::RefCell, | ||
collections::VecDeque, | ||
pin::Pin, | ||
rc::Rc, | ||
task::{Context, Poll, Waker}, | ||
}; | ||
|
||
use futures::Stream; | ||
|
||
pub(crate) struct LocalChannel<T> { | ||
queue: VecDeque<T>, | ||
waker: Option<Waker>, | ||
closed: bool, | ||
} | ||
|
||
pub(crate) struct LocalReceiver<T> { | ||
channel: Rc<RefCell<LocalChannel<T>>>, | ||
} | ||
|
||
impl<T> Stream for LocalReceiver<T> { | ||
type Item = T; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let mut channel = self.channel.borrow_mut(); | ||
|
||
match channel.queue.pop_front() { | ||
Some(item) => Poll::Ready(Some(item)), | ||
None => { | ||
if channel.closed { | ||
Poll::Ready(None) | ||
} else { | ||
channel.waker = Some(cx.waker().clone()); | ||
Poll::Pending | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub(crate) struct LocalSender<T> { | ||
channel: Rc<RefCell<LocalChannel<T>>>, | ||
} | ||
|
||
impl<T> LocalSender<T> { | ||
pub(crate) fn send(&self, item: T) { | ||
let mut channel = self.channel.borrow_mut(); | ||
|
||
channel.queue.push_back(item); | ||
|
||
let _ = channel.waker.take().map(Waker::wake); | ||
} | ||
} | ||
|
||
impl<T> Drop for LocalSender<T> { | ||
fn drop(&mut self) { | ||
let mut channel = self.channel.borrow_mut(); | ||
channel.closed = true; | ||
let _ = channel.waker.take().map(Waker::wake); | ||
} | ||
} | ||
|
||
pub(crate) fn local_channel<T>() -> (LocalSender<T>, LocalReceiver<T>) { | ||
let channel = Rc::new(RefCell::new(LocalChannel { | ||
queue: VecDeque::new(), | ||
waker: None, | ||
closed: false, | ||
})); | ||
|
||
( | ||
LocalSender { | ||
channel: channel.clone(), | ||
}, | ||
LocalReceiver { channel }, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters