Skip to content

Commit

Permalink
return drive_forever future rather than spawning it
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaeroxe committed Jan 29, 2023
1 parent dc0f296 commit 4883191
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,14 @@ impl<
T: Serialize + DeserializeOwned + Unpin + Send + 'static,
> AsyncReadConverse<R, W, T>
{
/// Spawns a future onto the tokio runtime that will drive the receive mechanism.
/// This allows you to receive replies to your messages, while completely ignoring any non-reply messages you get.
/// Returns a future that will drive the receive mechanism. It's recommended to spawn this onto an `async`
/// runtime, such as `tokio`. This allows you to receive replies to your messages, while completely
/// ignoring any non-reply messages you get.
///
/// If instead you'd like to see the non-reply messages then you'll need to drive the `Stream` implementation
/// for `AsyncReadConverse`.
pub fn drive_forever(mut self) {
tokio::spawn(async move { while StreamExt::next(&mut self).await.is_some() {} });
pub async fn drive_forever(mut self) {
while StreamExt::next(&mut self).await.is_some() {}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ async fn basic_dialogue() {
new_duplex_connection(ChecksumEnabled::Yes, server_read, server_write);
let (mut client_read, _client_write) =
new_duplex_connection(ChecksumEnabled::Yes, client_read, client_write);
server_read.drive_forever();
tokio::spawn(server_read.drive_forever());
tokio::spawn(async move {
while let Some(message) = client_read.next().await {
let mut received_message = message.unwrap();
Expand Down Expand Up @@ -278,7 +278,7 @@ async fn timeout_check() {
new_duplex_connection(ChecksumEnabled::Yes, server_read, server_write);
let (mut client_read, _client_write) =
new_duplex_connection(ChecksumEnabled::Yes, client_read, client_write);
server_read.drive_forever();
tokio::spawn(server_read.drive_forever());
tokio::spawn(async move {
while let Some(message) = client_read.next().await {
let mut received_message = message.unwrap();
Expand Down

0 comments on commit 4883191

Please sign in to comment.