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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::*;
use ;
/// Simple utility function for wrapping a closure with a lock.
///
/// This will handle panics safely as long as they are [`UnwindSafe`]. The
/// panic will be caught, the lock will be unlocked, and then the panic is
/// released. This assumes the operation provided is transactional and will
/// handle its own clean-up.
///
/// # Example
/// ```
/// # use simplelock::*;
/// # fn main() -> SimpleLockResult<()> {
/// let mut lock = default_lock("MyAppName")?;
/// let result = lock_until_finished(
/// &mut lock,
/// || {
/// // do something needing a lock.
/// # 42
/// }
/// );
/// # assert_eq!(42, result.unwrap(), "Should have been Ok(()).");
/// # Ok(())
/// # }
/// ```
///
/// [`UnwindSafe`]: https://doc.rust-lang.org/std/panic/trait.UnwindSafe.html
/// Get a lock with default configuration based on the feature enabled.
///
/// Two calls to this function, with the same argument, will provide unique objects but
/// will describe the same lock. For example, if the `file` feature is enabled, this will
/// return a unique [`FileLock`]. However, they will both point to the same file, and thus
/// lock operations on these objects will be in competition.
///
/// Note: If multiple features are enabled at once, we make no guarantees on the type of
/// lock returned. Internally though, we try to return the ones we have the strongest
/// guarantees around based on the OS. If you want consistency, either enable a single
/// feature or call the constructor needed directly.
///
/// # Examples
///
/// To enable `FileLock` by default, add this to your Cargo.toml:
/// ```toml
/// [dependencies]
/// simplelock = { version = "*", features = [ "file" ] }
/// ```
///
/// Then you can create a file lock in a general way like so:
///
/// ```rust
/// # use simplelock::*;
/// # fn main() -> SimpleLockResult<()> {
/// let mut lock = default_lock("MyAppName")?;
/// // Do something with the lock!
/// # Ok(())
/// # }
/// ```