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
//! 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 poll_valid(&self) -> bool {
        true
    }

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

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

/// Extention trait for Contracts.
pub trait ContractExt: Contract {
    /// Return type of `get_context`
    type Context;

    /// Get a thread-safe handle to a ContractContext.
    fn get_context(&self) -> Result<Self::Context, ContextError>;
}

/// 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;

/// ContractContext implementations.
pub mod context;

/// Parkable waker threads.
pub mod park;

/// Trait that defines a valid context for a contract.
pub use context::{ContextError, 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;