Module pendulum::future [] [src]

Futures based runtime for a Pendulum.

Heartbeat Example:

extern crate pendulum;
extern crate futures;
 
use std::time::Duration;
 
use futures::Stream;
use futures::sync::mpsc;
 
use pendulum::HashedWheelBuilder;
use pendulum::future::{TimerBuilder, TimedOut};
 
#[derive(Debug, PartialEq, Eq)]
enum PeerMessage {
    KeepAlive,
    DoSomething
}
 
impl From<TimedOut> for PeerMessage {
    fn from(_: TimedOut) -> PeerMessage {
        PeerMessage::KeepAlive
    }
}
 
fn main() {
    // Create a timer with the default configuration
    let timer = TimerBuilder::default()
        .build(HashedWheelBuilder::default().build());
 
    // Assume some other part of the application sends messages to some peer
    let (send, recv) = mpsc::unbounded();
 
    // Application sent the peer a single message
    send.unbounded_send(PeerMessage::DoSomething)
        .unwrap();
 
    // Wrap the receiver portion (a `Stream`), in a `Heartbeat` stream
    let mut heartbeat = timer.heartbeat(Duration::from_millis(100), recv)
        .unwrap()
        .wait();
 
    // Should receive the applications message
    assert_eq!(PeerMessage::DoSomething, heartbeat.next().unwrap().unwrap());
 
    // Application only sent one message, timer will continuously send keep alives
    // if 100 ms goes by without the original receiver receiving any messages
    assert_eq!(PeerMessage::KeepAlive, heartbeat.next().unwrap().unwrap());
    assert_eq!(PeerMessage::KeepAlive, heartbeat.next().unwrap().unwrap());
}

Structs

Heartbeat

Heartbeat that will ensure an item is yielded after at least Duration.

Sleep

Sleep future that will wait Duration before yielding.

SleepStream

SleepStream that will wait Duration before each yield.

TimedOut

Indicates a timeout has occurred.

Timeout

Timeout future that will ensure an item or error is yielded after at least Duration.

TimeoutStream

TimeoutStream that will ensure an item or error is yielded after at least Duration.

Timer

Timer for which different futures based timers can be created.

TimerBuilder

Builder for configuring and constructing instances of Timer.

TimerItem

Item that the Timer will place inside the Pendulum.

Enums

TimeoutStatus

Convenience enum for mapping an Item/Error to something implementing From<TimedOut>.