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

use {Async, Future, IntoFuture, Poll};

/// The status of a `loop_fn` loop.
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),
}

/// A future implementing a tail-recursive loop.
///
/// Created by the `loop_fn` function.
pub struct LoopFn<A, F> {
    future: A,
    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
///
/// ```
/// use futures::future::{ok, loop_fn, Future, Ok, Loop};
/// use std::io::Error;
///
/// struct Client {
///     ping_count: u8,
/// }
///
/// impl Client {
///     fn new() -> Self {
///         Client { ping_count: 0 }
///     }
///
///     fn send_ping(self) -> Ok<Self, Error> {
///         ok(Client { ping_count: self.ping_count + 1 })
///     }
///
///     fn receive_pong(self) -> Ok<(Self, bool), Error> {
///         let done = self.ping_count >= 5;
///         ok((self, done))
///     }
/// }
///
/// 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))
///             }
///         })
/// });
/// ```
pub fn loop_fn<S, T, I, A, F>(initial_state: S, mut func: F) -> LoopFn<A, F>
    where F: FnMut(S) -> I,
          A: Future<Item = Loop<T, S>>,
          I: IntoFuture<Future = A, Item = A::Item, Error = A::Error>
{
    LoopFn {
        future: func(initial_state).into_future(),
        func: func,
    }
}

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

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        loop {
            let status = try_ready!(self.future.poll());
            match status {
                Loop::Break(x) => return Ok(Async::Ready(x)),
                Loop::Continue(s) => self.future = (self.func)(s).into_future(),
            }
        }
    }
}
<commit_msg>Tweak loop_fn function bounds<commit_after>//! Definition of the `LoopFn` combinator, implementing `Future` loops.

use {Async, Future, IntoFuture, Poll};

/// The status of a `loop_fn` loop.
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),
}

/// A future implementing a tail-recursive loop.
///
/// Created by the `loop_fn` function.
pub struct LoopFn<A, F> where A: IntoFuture {
    future: A::Future,
    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
///
/// ```
/// use futures::future::{ok, loop_fn, Future, Ok, Loop};
/// use std::io::Error;
///
/// struct Client {
///     ping_count: u8,
/// }
///
/// impl Client {
///     fn new() -> Self {
///         Client { ping_count: 0 }
///     }
///
///     fn send_ping(self) -> Ok<Self, Error> {
///         ok(Client { ping_count: self.ping_count + 1 })
///     }
///
///     fn receive_pong(self) -> Ok<(Self, bool), Error> {
///         let done = self.ping_count >= 5;
///         ok((self, done))
///     }
/// }
///
/// 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))
///             }
///         })
/// });
/// ```
pub fn loop_fn<S, T, A, F>(initial_state: S, mut func: F) -> LoopFn<A, F>
    where F: FnMut(S) -> A,
          A: IntoFuture<Item = Loop<T, S>>,
{
    LoopFn {
        future: func(initial_state).into_future(),
        func: func,
    }
}

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

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        loop {
            match try_ready!(self.future.poll()) {
                Loop::Break(x) => return Ok(Async::Ready(x)),
                Loop::Continue(s) => self.future = (self.func)(s).into_future(),
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.1871543264942016, 'fraction_numerical': 0.0014272970561998216, 'mean_word_length': 1.9803296119085594, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5357195', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1799, 'n_words': 543}