1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! A Contract is a structure that can be invalidated or expired, on expiration the execute
//! method is called, depending on the contract it could produce a value or not. If the contract is
//! not valid at the time of the check it will be voided and could produce a value depending on the
//! contract.
//!
//! Contracts are valid futures that can be run to completion on a reactor or awaited in an async
//! block.

#![deny(clippy::all)]

/// Contract Trait
pub trait Contract: ::futures::future::Future {
    /// Check wether the contract is still valid. Always true by default.
    fn is_valid(&self) -> bool {
        true
    }

    /// Check wether the contract has expired. Always false by default.
    fn is_expired(&self) -> bool {
        false
    }

    /// Produce a status of the contract on expiration.
    fn execute(&self) -> Self::Output;

    /// Produce a status of the contract on cancel.
    fn void(&self) -> Self::Output;
}

/// Extention trait for Contracts.
pub trait ContractExt: Contract {
    type Context;
    /// Get a thread-safe handle to a ContractContext.
    fn get_context(&self) -> Self::Context;
}

/// Status on completion/invalidation of a contract.
pub enum Status<R> {
    /// Contract has successfully produced a value.
    Completed(R),

    /// Contract has ended and did not produce a value.
    Terminated,
}

mod contracts;

/// Time utilities
pub mod time;

/// Implementation of contexes to put in a contract.
pub mod context;

/// Contains contract wakers.
pub mod sync;

/// Trait that defines a valid context for a contract.
pub use context::ContractContext;

/// Duration based contract produces a value at a point in the future using the available context if it
/// has not been voided before.
pub use crate::contracts::FuturesContract;

/// Permanent contract that produces a value when it is voided by it's context.
pub use crate::contracts::OnKillContract;

/// Duration based contract produces a value at a point in the future if it has not been voided and
/// secondary context has been realized.
pub use crate::contracts::OptionContract;