@@ -30,13 +30,13 @@ pub mod access;
30
30
/// TODO: read/write permissions
31
31
#[ derive( Debug , Default , Clone ) ]
32
32
#[ repr( transparent) ]
33
- pub struct Volatile < T , A = ReadWrite > {
34
- value : T ,
33
+ pub struct Volatile < R , A = ReadWrite > {
34
+ reference : R ,
35
35
access : PhantomData < A > ,
36
36
}
37
37
38
- impl < T > Volatile < T > {
39
- /// Construct a new volatile instance wrapping the given value reference.
38
+ impl < R > Volatile < R > {
39
+ /// Construct a new volatile instance wrapping the given reference.
40
40
///
41
41
/// ## Example
42
42
///
@@ -48,15 +48,32 @@ impl<T> Volatile<T> {
48
48
/// let volatile = Volatile::new(&value);
49
49
/// assert_eq!(volatile.read(), 0);
50
50
/// ```
51
- pub const fn new ( value : T ) -> Volatile < T > {
51
+ pub const fn new ( reference : R ) -> Volatile < R > {
52
52
Volatile {
53
- value,
53
+ reference,
54
+ access : PhantomData ,
55
+ }
56
+ }
57
+
58
+ pub const fn new_read_only ( reference : R ) -> Volatile < R , ReadOnly > {
59
+ Volatile {
60
+ reference,
61
+ access : PhantomData ,
62
+ }
63
+ }
64
+
65
+ pub const fn new_write_only ( reference : R ) -> Volatile < R , WriteOnly > {
66
+ Volatile {
67
+ reference,
54
68
access : PhantomData ,
55
69
}
56
70
}
57
71
}
58
72
59
- impl < T : Copy , A > Volatile < & T , A > {
73
+ impl < T , A > Volatile < & T , A >
74
+ where
75
+ T : Copy ,
76
+ {
60
77
/// Performs a volatile read of the contained value.
61
78
///
62
79
/// Returns a copy of the read value. Volatile reads are guaranteed not to be optimized
@@ -78,7 +95,7 @@ impl<T: Copy, A> Volatile<&T, A> {
78
95
A : Readable ,
79
96
{
80
97
// UNSAFE: Safe, as we know that our internal value exists.
81
- unsafe { ptr:: read_volatile ( self . value ) }
98
+ unsafe { ptr:: read_volatile ( self . reference ) }
82
99
}
83
100
}
84
101
@@ -94,17 +111,17 @@ impl<T: Copy, A> Volatile<&mut T, A> {
94
111
/// ```rust
95
112
/// use volatile::Volatile;
96
113
///
97
- /// let mut value = 42 ;
114
+ /// let mut value = 10 ;
98
115
/// let volatile = Volatile::new(&mut value);
99
116
///
100
- /// assert_eq!(volatile.read(), 42 );
117
+ /// assert_eq!(volatile.read(), 10 );
101
118
/// ```
102
119
pub fn read ( & self ) -> T
103
120
where
104
121
A : Readable ,
105
122
{
106
123
// UNSAFE: Safe, as we know that our internal value exists.
107
- unsafe { ptr:: read_volatile ( self . value ) }
124
+ unsafe { ptr:: read_volatile ( self . reference ) }
108
125
}
109
126
110
127
/// Performs a volatile write, setting the contained value to the given `value`.
@@ -129,7 +146,7 @@ impl<T: Copy, A> Volatile<&mut T, A> {
129
146
A : Writable ,
130
147
{
131
148
// UNSAFE: Safe, as we know that our internal value exists.
132
- unsafe { ptr:: write_volatile ( self . value , value) } ;
149
+ unsafe { ptr:: write_volatile ( self . reference , value) } ;
133
150
}
134
151
135
152
/// Updates the contained value using the given closure and volatile instructions.
@@ -158,25 +175,25 @@ impl<T: Copy, A> Volatile<&mut T, A> {
158
175
}
159
176
}
160
177
161
- impl < T : Copy , A > Volatile < & [ T ] , A > {
178
+ impl < T , A > Volatile < & [ T ] , A > {
162
179
pub fn index < I > ( & self , index : I ) -> Volatile < & I :: Output , A >
163
180
where
164
181
I : SliceIndex < [ T ] > ,
165
182
{
166
183
Volatile {
167
- value : self . value . index ( index) ,
184
+ reference : self . reference . index ( index) ,
168
185
access : self . access ,
169
186
}
170
187
}
171
188
}
172
189
173
- impl < T : Copy , A > Volatile < & mut [ T ] , A > {
190
+ impl < T , A > Volatile < & mut [ T ] , A > {
174
191
pub fn index < I > ( & self , index : I ) -> Volatile < & I :: Output , A >
175
192
where
176
193
I : SliceIndex < [ T ] > ,
177
194
{
178
195
Volatile {
179
- value : self . value . index ( index) ,
196
+ reference : self . reference . index ( index) ,
180
197
access : self . access ,
181
198
}
182
199
}
@@ -186,7 +203,7 @@ impl<T: Copy, A> Volatile<&mut [T], A> {
186
203
I : SliceIndex < [ T ] > ,
187
204
{
188
205
Volatile {
189
- value : self . value . index_mut ( index) ,
206
+ reference : self . reference . index_mut ( index) ,
190
207
access : self . access ,
191
208
}
192
209
}
0 commit comments