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


use std::ops::DerefMut;
use std::ops::Deref;
use FlockLock;
use InitFlockLock;
use std::fs::File;
use std::io;

///Only one process can retain exclusive lock of the file.
#[derive(Debug)]
pub struct ExclusiveLock(File);

impl ExclusiveLock {
     pub fn lock(f: File) -> Result<Self, io::Error> {
          crate::sys::lock_exclusive::<Self>(f)
     }

     pub fn try_lock(f: File) -> Result<Option<Self>, io::Error> {
          crate::sys::try_lock_exclusive::<Self>(f)
     }
}

impl InitFlockLock for ExclusiveLock {
     type Lock = Self;
     type Arg = File;

     fn new(f: Self::Arg) -> Self::Lock {
          ExclusiveLock(f)
     }
}


impl Deref for ExclusiveLock {
     type Target = File;

     #[inline(always)]
     fn deref(&self) -> &Self::Target {
          &self.0
     }
}

impl DerefMut for ExclusiveLock {
     #[inline(always)]
     fn deref_mut(&mut self) -> &mut Self::Target {
          &mut self.0
     }
}

impl AsRef<File> for ExclusiveLock {
     #[inline(always)]
     fn as_ref(&self) -> &File {
          &*self
     }
}

impl FlockLock for ExclusiveLock {}

impl Drop for ExclusiveLock {
     fn drop(&mut self) {
          let _e = crate::sys::unlock(&self.0);
     }
}