Skip to content

adding loop_fn back to futures-rs, 0.3 intial implementation #2445

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion futures-channel/no_atomic_cas.rs
12 changes: 11 additions & 1 deletion futures-core/no_atomic_cas.rs
12 changes: 11 additions & 1 deletion futures-task/no_atomic_cas.rs
12 changes: 11 additions & 1 deletion futures-util/no_atomic_cas.rs
136 changes: 136 additions & 0 deletions futures-util/src/future/loop_fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@


//! Definition of the `LoopFn` combinator, implementing `Future` loops.

use core::future::Future;
use core::pin::Pin;
use core::task::{Poll,Context};
use futures_core::ready;
use pin_project_lite::pin_project;


/// An enum describing whether to `break` or `continue` a `loop_fn` loop.
#[derive(Debug)]
pub enum Loop<T, S> {
/// Indicates that the loop has completed with output `T`.
Break(T),

/// Indicates that the loop function should be called again with input
/// state `S`.
Continue(S),
}




pin_project! {

#[must_use = "futures do nothing unless polled"]
/// A future implementing a tail-recursive loop.
/// Created by the `loop_fn` function.
pub struct LoopFn<A, F> where A: Future {

#[pin]
future: A,
//unpin?
func: F,
}
}


/// Creates a new future implementing a tail-recursive loop.
///
/// The loop function is immediately called with `initial_state` and should
/// return a value that can be converted to a future. On successful completion,
/// this future should output a `Loop<T, S>` to indicate the status of the
/// loop.
///
/// `Loop::Break(T)` halts the loop and completes the future with output `T`.
///
/// `Loop::Continue(S)` reinvokes the loop function with state `S`. The returned
/// future will be subsequently polled for a new `Loop<T, S>` value.
///
/// # Examples
///
/// ```
/// # extern crate futures;
/// use futures::prelude::*;
/// use futures_util::future::{self, loop_fn, Loop, FutureResult};
/// use futures::never::Never;
///
/// struct Client {
/// ping_count: u8,
/// }
///
/// impl Client {
/// fn new() -> Self {
/// Client { ping_count: 0 }
/// }
///
/// fn send_ping(self) -> FutureResult<Self, Never> {
/// ok(Client { ping_count: self.ping_count + 1 })
/// }
///
/// fn receive_pong(self) -> FutureResult<(Self, bool), Never> {
/// let done = self.ping_count >= 5;
/// ok((self, done))
/// }
/// }
///
/// # fn main() {
/// let ping_til_done = loop_fn(Client::new(), |client| {
/// client.send_ping()
/// .and_then(|client| client.receive_pong())
/// .and_then(|(client, done)| {
/// if done {
/// Ok(Loop::Break(client))
/// } else {
/// Ok(Loop::Continue(client))
/// }
/// })
/// });
/// # }
/// ```
/// small note in the future this code should change A : intoFuture rather than
/// just future however because it isnt stable on std just use future now!
pub fn loop_fn<S, T, A, F>(initial_state: S, mut func: F) -> LoopFn<A, F>
where F: FnMut(S) -> A,
A: Future<Output = Loop<T, S>>,
{
LoopFn {
future: func(initial_state),
func: func,
}
}

impl<S, T, A :Future, F> Future for LoopFn<A, F>
where F: FnMut(S) -> A,
A: Future<Output = Loop<T, S>>,
{
type Output = T;

fn poll(mut self : Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {
let inner_future= self.as_mut().project().future;

match ready!(inner_future.poll(cx)) {
Loop::Break(x) => break Poll::Ready(x),
Loop::Continue(s) => {
//create one binding to then split up to beat borrow checker!
let mut proj = self.as_mut().project();
proj.future.set((proj.func)(s));

},
}
}
}
}


#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
3 changes: 3 additions & 0 deletions futures-util/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ pub use crate::abortable::{AbortHandle, AbortRegistration, Abortable, Aborted};
#[cfg(feature = "alloc")]
pub use abortable::abortable;

mod loop_fn;
pub use loop_fn::{LoopFn, loop_fn, Loop};

// Just a helper function to ensure the futures we're returning all have the
// right implementations.
pub(crate) fn assert_future<T, F>(future: F) -> F
Expand Down