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
use std::ops::DerefMut;
use std::ops::Deref;
use FlockLock;
use InitFlockLock;
use std::fs::File;
use std::io;
#[derive(Debug)]
pub struct SharedLock(File);
impl SharedLock {
pub fn lock(f: File) -> Result<Self, io::Error> {
crate::sys::lock_shared::<Self>(f)
}
pub fn try_lock(f: File) -> Result<Option<Self>, io::Error> {
crate::sys::try_lock_shared::<Self>(f)
}
}
impl InitFlockLock for SharedLock {
type Lock = Self;
type Arg = File;
fn new(f: Self::Arg) -> Self::Lock {
SharedLock(f)
}
}
impl Deref for SharedLock {
type Target = File;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for SharedLock {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl AsRef<File> for SharedLock {
#[inline(always)]
fn as_ref(&self) -> &File {
&*self
}
}
impl FlockLock for SharedLock {}
impl Drop for SharedLock {
fn drop(&mut self) {
let _e = crate::sys::unlock(&self.0);
}
}