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
// TODO: turn this into a builder pattern?

#[derive(Copy, Clone, Debug)]
pub struct LockConfig {
    /// When attempting to lock, if we are unable to immediately
    /// acquire the lock, what should we do? If `true', we will
    /// hang indefinitely until we are able to acquire the lock.
    /// If `false', we will return with an error "AlreadyLocked".
    hang_lock: bool, // default 'true'

    /// When attempting to unlock, if we fail, how should we react?
    /// If `true' we will continue silently. It is up to the caller
    /// to confirm the lock status with Lock::is_locked. If `false',
    /// we will return with an error "UnableToUnlock(...)" with an
    /// embedded error as the reason.
    silent_fail: bool, // default 'false'
}

impl LockConfig {
    ///
    pub fn is_hang_lock(&self) -> bool {
        self.hang_lock
    }

    ///
    pub fn is_silent_fail(&self) -> bool {
        self.silent_fail
    }

    /// Override the current setting for unlock behaviour.
    pub fn set_silent_fail(&mut self, silent_fail: bool) {
        self.silent_fail = silent_fail;
    }

    ///
    pub fn set_hang_lock(&mut self, hang_lock: bool) {
        self.hang_lock = hang_lock;
    }

    ///
    pub fn with_silent_fail(mut self, silent_fail: bool) -> Self {
        self.set_silent_fail(silent_fail);
        self
    }

    ///
    pub fn with_hang_lock(mut self, hang_lock: bool) -> Self {
        self.set_hang_lock(hang_lock);
        self
    }
}

const DEFAULT_HANG_LOCK: bool = true;
const DEFAULT_SILENT_FAIL: bool = false;

impl Default for LockConfig {
    fn default() -> Self {
        LockConfig {
            hang_lock: DEFAULT_HANG_LOCK,
            silent_fail: DEFAULT_SILENT_FAIL,
        }
    }
}