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::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 ExclusiveSliceLock<'a>(&'a File);

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

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

impl<'a> InitFlockLock for ExclusiveSliceLock<'a> {
     type Lock = Self;
     type Arg = &'a File;

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


impl<'a> Deref for ExclusiveSliceLock<'a> {
     type Target = File;

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


impl<'a> FlockLock for ExclusiveSliceLock<'a> {}

impl<'a> Into<&'a File> for ExclusiveSliceLock<'a> {
     #[inline(always)]
     fn into(self) -> &'a File {
          self.0
     }
}

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

impl<'a> Drop for ExclusiveSliceLock<'a> {
     fn drop(&mut self) {
          let _e = crate::sys::unlock(self.0);
     }
}