[][src]Trait ticktock::Attempt

pub trait Attempt<O> {
    fn attempt(self) -> Option<O>;
}

Iterator attempt

Given an iterator of outcomes, iterates returning either

  • the first successful outcome
  • the last unsuccessful outcome
  • None if the iterator was empty

Result is often used as an outcome, e.g. when trying to reconnect multiple times:

use std::net::TcpStream;
use std::time::Duration;
use ticktock::delay::Delay;
use ticktock::Attempt;

const RETRY_DELAY: Duration = Duration::from_millis(250);

// attempt to connect to localhost:12348 three times, before giving up.
// in total, 500 ms of delay will be inserted
let conn = Delay::new(RETRY_DELAY)
    .map(|_| TcpStream::connect("localhost:12348"))
    .take(3)
    .attempt()
    .unwrap();

Option is also a valid outcome:

This example is not tested
let credentials = vec![("Bob", "secret"), ("Jeff", "hunter2"), ("John", "swordfish")];

fn try_login(username: &str, password: &str) -> Option<(String, String)> { ... }

// brute-force our way in
let valid_credentials: Option<(String, String)> = credentials
                                                      .map(|(u, p)| try_login(u, p))
                                                      .attempt()
                                                      .unwrap();

Required methods

fn attempt(self) -> Option<O>

Consumes until the successful outcome is encountered. In case of failure, returns the last unsuccessful outcome.

Loading content...

Implementors

impl<T, E, I> Attempt<Result<T, E>> for I where
    I: Iterator<Item = Result<T, E>>, 
[src]

impl<T, I> Attempt<Option<T>> for I where
    I: Iterator<Item = Option<T>>, 
[src]

Loading content...