Struct tangle::Future [] [src]

pub struct Future<T, E = ()> {
    // some fields omitted
}

A value that will be resolved sometime into the future, asynchronously. Futures use an internal threadpool to handle asynchronous tasks.

Methods

impl<T, E = ()> Future<T, E> where T: Send + 'static, E: Send + 'static

fn new<F>(f: F) -> Future<T, E> where F: FnOnce() -> Async<T, E> + Send + 'static

use tangle::{Future, Async};

Future::new(|| if true { Async::Ok(123) } else { Async::Err("Foobar") });

fn from_async_channel(receiver: Receiver<Async<T, E>>) -> Future<T, E>

fn channel() -> (Sender<T>, Future<T, E>)

fn from_channel(receiver: Receiver<T>) -> Future<T, E>

Create a new future from the receiving end of a native channel.

use tangle::{Future, Async};
use std::thread;
use std::sync::mpsc::channel;

let (tx, rx) = channel();
Future::<u32>::from_channel(rx).and_then(|v| {
    assert_eq!(v, 1235);
    Async::Ok(())
});
tx.send(1235);

fn and_then<F, S>(self, f: F) -> Future<S, E> where F: FnOnce(T) -> Async<S, E> + Send + 'static, S: Send + 'static

use tangle::{Future, Async};

let f: Future<usize> = Future::unit(1);

let purchase: Future<String> = f.and_then(|num| Async::Ok(num.to_string()));

match purchase.recv() {
    Ok(val) => assert_eq!(val, "1".to_string()),
    _ => panic!("unexpected")
}

fn map<F, S>(self, f: F) -> Future<S, E> where F: FnOnce(T) -> S + Send + 'static, S: Send + 'static

use tangle::{Future, Async};

let f: Future<usize> = Future::unit(1);

let purchase: Future<String> = f.map(|num| num.to_string());

match purchase.recv() {
    Ok(val) => assert_eq!(val, "1".to_string()),
    _ => panic!("unexpected")
}

fn recv(self) -> Result<T, E>

fn unit(val: T) -> Future<T, E>

Wrap a value into a Future that completes right away.

Usage

use tangle::Future;

let _: Future<usize> = Future::unit(5);

fn err(err: E) -> Future<T, E>

use tangle::Future;

let _: Future<usize, &str> = Future::err("foobar");

Trait Implementations

Derived Implementations

impl<T: Debug, E: Debug> Debug for Future<T, E>

fn fmt(&self, __arg_0: &mut Formatter) -> Result