distributed_lock_core/
timeout.rs

1//! Timeout value helpers.
2
3use std::time::Duration;
4
5/// Represents a timeout duration for lock operations.
6///
7/// - `Some(duration)` - Wait up to this duration
8/// - `None` - Wait indefinitely
9pub type Timeout = Option<Duration>;
10
11/// Internal helper for timeout calculations.
12#[derive(Debug, Clone, Copy)]
13pub struct TimeoutValue {
14    millis: i64, // -1 for infinite
15}
16
17impl TimeoutValue {
18    pub const INFINITE: Self = Self { millis: -1 };
19    pub const ZERO: Self = Self { millis: 0 };
20
21    pub fn is_infinite(&self) -> bool {
22        self.millis < 0
23    }
24
25    pub fn is_zero(&self) -> bool {
26        self.millis == 0
27    }
28
29    pub fn as_duration(&self) -> Option<Duration> {
30        if self.is_infinite() {
31            None
32        } else {
33            Some(Duration::from_millis(self.millis as u64))
34        }
35    }
36}
37
38impl From<Option<Duration>> for TimeoutValue {
39    fn from(timeout: Option<Duration>) -> Self {
40        match timeout {
41            None => Self::INFINITE,
42            Some(d) => Self {
43                millis: d.as_millis() as i64,
44            },
45        }
46    }
47}