-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add try_stream_fn * Deduplicate * Remove duplicate functions
- Loading branch information
Showing
7 changed files
with
133 additions
and
28 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
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,28 @@ | ||
use crate::yielder::Yielder; | ||
|
||
/// Handle to allow you to yield something from the stream | ||
pub struct TryYielder<Ok, Error> { | ||
yielder: Yielder<Result<Ok, Error>>, | ||
} | ||
|
||
impl<Ok, Error> TryYielder<Ok, Error> { | ||
/// Yield a success value from the stream | ||
#[inline] | ||
pub async fn yield_ok(&mut self, item: Ok) { | ||
self.yielder.yield_item(Ok(item)).await; | ||
} | ||
|
||
/// Yield an error value from the stream | ||
#[inline] | ||
pub async fn yield_error(&mut self, item: Error) { | ||
self.yielder.yield_item(Err(item)).await; | ||
} | ||
} | ||
|
||
#[doc(hidden)] | ||
impl<Ok, Error> From<Yielder<Result<Ok, Error>>> for TryYielder<Ok, Error> { | ||
#[inline] | ||
fn from(yielder: Yielder<Result<Ok, Error>>) -> Self { | ||
Self { yielder } | ||
} | ||
} |
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,20 @@ | ||
use futures_lite::stream; | ||
use std::pin::pin; | ||
|
||
#[test] | ||
fn exits_early() { | ||
let stream = pin!(asynk_strim::try_stream_fn(|mut yielder| async move { | ||
yielder.yield_ok(42).await; | ||
return Err("oh no"); | ||
|
||
#[allow(unreachable_code)] | ||
yielder.yield_ok(1337).await; | ||
|
||
Ok(()) | ||
})); | ||
|
||
let mut stream = stream::block_on(stream); | ||
assert_eq!(stream.next(), Some(Ok(42))); | ||
assert_eq!(stream.next(), Some(Err("oh no"))); | ||
assert_eq!(stream.next(), None); | ||
} |
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