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
use super::LendingLibrary;
use std::{fmt::{Debug, Error as FmtError, Formatter},
hash::Hash,
ops::{Deref, DerefMut},
thread};
pub struct Loan<K, V>
where
K: Hash,
{
pub(super) owner: *mut LendingLibrary<K, V>,
pub(super) key: u64,
pub(super) inner: Option<V>,
}
impl<K, V> Debug for Loan<K, V>
where
K: Hash,
V: Debug,
{
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
<V as Debug>::fmt(self, f)
}
}
impl<K, V> PartialEq for Loan<K, V>
where
K: Hash,
V: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}
impl<K, V> Drop for Loan<K, V>
where
K: Hash,
{
fn drop(&mut self) {
if self.inner.is_some() && !thread::panicking() {
unsafe {
(*self.owner).checkin(self.key, self.inner.take().unwrap());
}
}
}
}
impl<K, V> Deref for Loan<K, V>
where
K: Hash,
{
type Target = V;
fn deref(&self) -> &V {
self.inner.as_ref().unwrap()
}
}
impl<K, V> DerefMut for Loan<K, V>
where
K: Hash,
{
fn deref_mut(&mut self) -> &mut V {
self.inner.as_mut().unwrap()
}
}