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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#![deny(missing_docs)]
#![deny(warnings)]
#![feature(optin_builtin_traits)]
#![no_std]
extern crate static_ref;
use core::u8;
pub use static_ref::Static;
pub unsafe trait Resource {
type Data: Send;
fn borrow<'cs>(&'cs self, t: &'cs Threshold) -> &'cs Static<Self::Data>;
fn borrow_mut<'cs>(
&'cs mut self,
t: &'cs Threshold,
) -> &'cs mut Static<Self::Data>;
fn claim<R, F>(&self, t: &mut Threshold, f: F) -> R
where
F: FnOnce(&Static<Self::Data>, &mut Threshold) -> R;
fn claim_mut<R, F>(&mut self, t: &mut Threshold, f: F) -> R
where
F: FnOnce(&mut Static<Self::Data>, &mut Threshold) -> R;
}
unsafe impl<T> Resource for Static<T>
where
T: Send,
{
type Data = T;
fn borrow<'cs>(&'cs self, _cs: &'cs Threshold) -> &'cs Static<T> {
self
}
fn borrow_mut<'cs>(
&'cs mut self,
_cs: &'cs Threshold,
) -> &'cs mut Static<T> {
self
}
fn claim<R, F>(&self, t: &mut Threshold, f: F) -> R
where
F: FnOnce(&Static<Self::Data>, &mut Threshold) -> R,
{
f(self, t)
}
fn claim_mut<R, F>(&mut self, t: &mut Threshold, f: F) -> R
where
F: FnOnce(&mut Static<Self::Data>, &mut Threshold) -> R,
{
f(self, t)
}
}
pub struct Threshold {
value: u8,
}
impl Threshold {
pub unsafe fn new(value: u8) -> Self {
Threshold { value }
}
pub unsafe fn max() -> Self {
Self::new(u8::MAX)
}
pub fn value(&self) -> u8 {
self.value
}
}
impl !Send for Threshold {}