-
-
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.
- Loading branch information
Showing
6 changed files
with
222 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use crate::{try_yielder::TryYielder, yielder::Yielder}; | ||
use core::{ | ||
future::Future, | ||
hint::unreachable_unchecked, | ||
marker::PhantomData, | ||
pin::Pin, | ||
ptr, | ||
task::{self, Poll}, | ||
}; | ||
use futures_core::{FusedStream, Stream}; | ||
use pin_project_lite::pin_project; | ||
|
||
#[inline] | ||
pub fn init<F, Fut, Ok, Error>(func: F) -> impl Stream<Item = Result<Ok, Error>> | ||
where | ||
F: FnOnce(TryYielder<Ok, Error>) -> Fut, | ||
Fut: Future<Output = Result<(), Error>>, | ||
{ | ||
AsynkStrim::Initial { func: Some(func) } | ||
} | ||
|
||
pin_project! { | ||
/// IMPORTANT: Never EVER EVER create this stream in the state `Initial` with the `func` parameter set to `None` | ||
/// Doing this will trigger undefined behaviour. | ||
/// | ||
/// IMPORTANT: Never EVER EVER construct a stream in the `MarkerStuff` state. | ||
/// Doing this will trigger undefined behaviour. | ||
#[project = AsynkStrimProj] | ||
#[project(!Unpin)] | ||
enum AsynkStrim<F, Fut, Item> { | ||
Initial { | ||
func: Option<F>, | ||
}, | ||
Progress { | ||
#[pin] | ||
fut: Fut, | ||
}, | ||
Done, | ||
MarkerStuff { | ||
_item: PhantomData<Item>, | ||
} | ||
} | ||
} | ||
|
||
impl<F, Fut, Ok, Error> Stream for AsynkStrim<F, Fut, Result<Ok, Error>> | ||
where | ||
F: FnOnce(TryYielder<Ok, Error>) -> Fut, | ||
Fut: Future<Output = Result<(), Error>>, | ||
{ | ||
type Item = Result<Ok, Error>; | ||
|
||
#[inline] | ||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> { | ||
let stream_address = ptr::from_ref(self.as_ref().get_ref()) as usize; | ||
loop { | ||
match self.as_mut().project() { | ||
AsynkStrimProj::Initial { func } => { | ||
// at the end of the function we transition into the progress state. | ||
// this state is never initialized with `func` set to `None`. | ||
// | ||
// we actually only do this to be able to use `.take()` to remove the function from the future. | ||
#[allow(unsafe_code)] | ||
let func = unsafe { func.take().unwrap_unchecked() }; | ||
let fut = func(TryYielder::new(Yielder::new(stream_address))); | ||
|
||
self.set(Self::Progress { fut }); | ||
} | ||
AsynkStrimProj::Progress { fut, .. } => { | ||
let mut out = None; | ||
let poll_output = | ||
crate::waker::with_context(cx.waker(), stream_address, &mut out, |cx| { | ||
fut.poll(cx) | ||
}); | ||
|
||
match (poll_output, out) { | ||
(Poll::Ready(result), ..) => { | ||
self.set(AsynkStrim::Done); | ||
if let Err(err) = result { | ||
break Poll::Ready(Some(Err(err))); | ||
} | ||
} | ||
(Poll::Pending, Some(item)) => break Poll::Ready(Some(item)), | ||
(Poll::Pending, None) => break Poll::Pending, | ||
} | ||
} | ||
AsynkStrimProj::Done => break Poll::Ready(None), | ||
AsynkStrimProj::MarkerStuff { .. } => { | ||
// the state machine will never enter this state. | ||
// documented on the state machine level. | ||
#[allow(unsafe_code)] | ||
unsafe { | ||
unreachable_unchecked() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<F, Fut, Ok, Error> FusedStream for AsynkStrim<F, Fut, Result<Ok, Error>> | ||
where | ||
F: FnOnce(TryYielder<Ok, Error>) -> Fut, | ||
Fut: Future<Output = Result<(), Error>>, | ||
{ | ||
#[inline] | ||
fn is_terminated(&self) -> bool { | ||
matches!(self, Self::Done) | ||
} | ||
} |
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,31 @@ | ||
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> { | ||
#[inline] | ||
pub(crate) fn new(yielder: Yielder<Result<Ok, Error>>) -> Self { | ||
Self { yielder } | ||
} | ||
|
||
/// Yield a result from the stream | ||
#[inline] | ||
pub async fn yield_result(&mut self, item: Result<Ok, Error>) { | ||
self.yielder.yield_item(item).await; | ||
} | ||
|
||
/// Yield a success value from the stream | ||
#[inline] | ||
pub async fn yield_ok(&mut self, item: Ok) { | ||
self.yield_result(Ok(item)).await; | ||
} | ||
|
||
/// Yield an error value from the stream | ||
#[inline] | ||
pub async fn yield_error(&mut self, item: Error) { | ||
self.yield_result(Err(item)).await; | ||
} | ||
} |
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