-
Notifications
You must be signed in to change notification settings - Fork 339
Async successors #363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Async successors #363
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fe3c9ef
First attempt at successor
felipesere 02b261d
It compiles! Store the future and poll it instead of creating multipl…
felipesere 95a3e53
Only use the Option of the future to decide to construct a new one
felipesere 8b662b6
Run rustfmt
felipesere 554d5cf
Slight renamings
felipesere 2667548
Rename the module to 'successors'
felipesere 8d97e0f
Only produes empty value if next is ever a 'None'
felipesere af92816
Got further! Thx Josh!
felipesere a257b70
Rename some variables to match iter
felipesere 243cdd7
Slight miss-merge
felipesere 4c09cdb
Mark successors as unstable
felipesere bfb42b4
Rearrange docs to match 'repeat'
felipesere 7677e9a
Make the closure take a borrow to the value
felipesere f14b37f
Remoe the T: Copy bound on the item
felipesere 786a52a
Slight miss-merge
felipesere 64216b8
Take a normal closure, not an async one
felipesere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,82 @@ | ||
use std::pin::Pin; | ||
use std::mem; | ||
|
||
use crate::stream::Stream; | ||
use crate::task::{Context, Poll}; | ||
|
||
use pin_project_lite::pin_project; | ||
|
||
/// Creates a new stream where to produce each new element a closure is called with the previous | ||
/// value. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # fn main() { async_std::task::block_on(async { | ||
/// # | ||
/// use async_std::prelude::*; | ||
/// use async_std::stream; | ||
/// | ||
/// let s = stream::successors(Some(22), |&val| Some(val + 1) ); | ||
/// | ||
/// pin_utils::pin_mut!(s); | ||
felipesere marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// assert_eq!(s.next().await, Some(22)); | ||
/// assert_eq!(s.next().await, Some(23)); | ||
/// assert_eq!(s.next().await, Some(24)); | ||
/// assert_eq!(s.next().await, Some(25)); | ||
/// | ||
/// # | ||
/// # }) } | ||
/// | ||
/// ``` | ||
#[cfg(feature = "unstable")] | ||
#[cfg_attr(feature = "docs", doc(cfg(unstable)))] | ||
pub fn successors<F, T>(first: Option<T>, succ: F) -> Successors<F, T> | ||
where | ||
F: FnMut(&T) -> Option<T>, | ||
{ | ||
Successors { | ||
succ, | ||
slot: first, | ||
} | ||
} | ||
|
||
pin_project! { | ||
/// A stream that yields elements by calling an async closure with the previous value as an | ||
/// argument | ||
/// | ||
/// This stream is constructed by [`successors`] function | ||
/// | ||
/// [`successors`]: fn.succssors.html | ||
#[cfg(feature = "unstable")] | ||
#[cfg_attr(feature = "docs", doc(cfg(unstable)))] | ||
#[derive(Debug)] | ||
pub struct Successors<F, T> | ||
where | ||
F: FnMut(&T) -> Option<T> | ||
{ | ||
succ: F, | ||
slot: Option<T>, | ||
} | ||
} | ||
|
||
impl<F, T> Stream for Successors<F, T> | ||
where | ||
F: FnMut(&T) -> Option<T>, | ||
{ | ||
type Item = T; | ||
|
||
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let this = self.project(); | ||
|
||
if this.slot.is_none() { | ||
return Poll::Ready(None); | ||
} | ||
|
||
let mut next = (this.succ)(&this.slot.as_ref().unwrap()); | ||
|
||
// 'swapping' here means 'slot' will hold the next value and next will be th one from the previous iteration | ||
mem::swap(this.slot, &mut next); | ||
Poll::Ready(next) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.