1
//! Solver adaptors.
2

            
3
use std::time::{Duration, Instant};
4

            
5
/// One wall-clock budget shared by every backend call made during a single solve.
6
#[derive(Clone, Copy, Debug)]
7
pub(crate) struct SolveTimeBudget {
8
    started: Instant,
9
    limit: Option<Duration>,
10
}
11

            
12
impl SolveTimeBudget {
13
1185077
    pub(crate) fn new(limit: Option<Duration>) -> Self {
14
1185077
        Self {
15
1185077
            started: Instant::now(),
16
1185077
            limit,
17
1185077
        }
18
1185077
    }
19

            
20
28420121
    pub(crate) fn remaining(self) -> Option<Duration> {
21
28420121
        self.limit
22
4
            .map(|limit| limit.saturating_sub(self.started.elapsed()))
23
28420121
    }
24

            
25
28067219
    pub(crate) fn expired(self) -> bool {
26
28067219
        self.remaining()
27
3
            .is_some_and(|remaining| remaining.is_zero())
28
28067219
    }
29
}
30

            
31
pub mod minion;
32
#[cfg(any(
33
    feature = "sat-batsat",
34
    all(feature = "sat-cadical", not(target_family = "wasm"))
35
))]
36
pub mod rustsat;
37

            
38
#[doc(inline)]
39
pub use minion::{Minion, MinionValueOrder, MinionVariableOrder};
40

            
41
#[cfg(any(
42
    feature = "sat-batsat",
43
    all(feature = "sat-cadical", not(target_family = "wasm"))
44
))]
45
#[doc(inline)]
46
pub use rustsat::{Sat, SatBackend};
47

            
48
#[cfg(feature = "z3")]
49
pub mod smt;
50

            
51
#[cfg(feature = "z3")]
52
#[doc(inline)]
53
pub use smt::Smt;
54

            
55
#[cfg(test)]
56
mod tests {
57
    use super::*;
58

            
59
    #[test]
60
1
    fn zero_solve_time_budget_is_immediately_expired() {
61
1
        assert!(SolveTimeBudget::new(Some(Duration::ZERO)).expired());
62
1
    }
63

            
64
    #[test]
65
1
    fn absent_solve_time_budget_never_expires() {
66
1
        let budget = SolveTimeBudget::new(None);
67
1
        assert_eq!(budget.remaining(), None);
68
1
        assert!(!budget.expired());
69
1
    }
70
}