@@ -158,6 +158,93 @@ impl<R> Volatile<R> {
158
158
}
159
159
}
160
160
161
+ /// Methods for references to `Copy` types
162
+ impl < R , T , A > Volatile < R , A >
163
+ where
164
+ R : Deref < Target = T > ,
165
+ T : Copy ,
166
+ {
167
+ /// Performs a volatile read of the contained value.
168
+ ///
169
+ /// Returns a copy of the read value. Volatile reads are guaranteed not to be optimized
170
+ /// away by the compiler, but by themselves do not have atomic ordering
171
+ /// guarantees. To also get atomicity, consider looking at the `Atomic` wrapper types of
172
+ /// the standard/`core` library.
173
+ ///
174
+ /// ## Examples
175
+ ///
176
+ /// ```rust
177
+ /// use volatile::Volatile;
178
+ ///
179
+ /// let value = 42;
180
+ /// let shared_reference = Volatile::new(&value);
181
+ /// assert_eq!(shared_reference.read(), 42);
182
+ ///
183
+ /// let mut value = 50;
184
+ /// let mut_reference = Volatile::new(&mut value);
185
+ /// assert_eq!(mut_reference.read(), 50);
186
+ /// ```
187
+ pub fn read ( & self ) -> T
188
+ where
189
+ A : Readable ,
190
+ {
191
+ // UNSAFE: Safe, as we know that our internal value exists.
192
+ unsafe { ptr:: read_volatile ( & * self . reference ) }
193
+ }
194
+
195
+ /// Performs a volatile write, setting the contained value to the given `value`.
196
+ ///
197
+ /// Volatile writes are guaranteed to not be optimized away by the compiler, but by
198
+ /// themselves do not have atomic ordering guarantees. To also get atomicity, consider
199
+ /// looking at the `Atomic` wrapper types of the standard/`core` library.
200
+ ///
201
+ /// ## Example
202
+ ///
203
+ /// ```rust
204
+ /// use volatile::Volatile;
205
+ ///
206
+ /// let mut value = 42;
207
+ /// let mut volatile = Volatile::new(&mut value);
208
+ /// volatile.write(50);
209
+ ///
210
+ /// assert_eq!(volatile.read(), 50);
211
+ /// ```
212
+ pub fn write ( & mut self , value : T )
213
+ where
214
+ A : Writable ,
215
+ R : DerefMut ,
216
+ {
217
+ // UNSAFE: Safe, as we know that our internal value exists.
218
+ unsafe { ptr:: write_volatile ( & mut * self . reference , value) } ;
219
+ }
220
+
221
+ /// Updates the contained value using the given closure and volatile instructions.
222
+ ///
223
+ /// Performs a volatile read of the contained value, passes a mutable reference to it to the
224
+ /// function `f`, and then performs a volatile write of the (potentially updated) value back to
225
+ /// the contained value.
226
+ ///
227
+ /// ```rust
228
+ /// use volatile::Volatile;
229
+ ///
230
+ /// let mut value = 42;
231
+ /// let mut volatile = Volatile::new(&mut value);
232
+ /// volatile.update(|val| *val += 1);
233
+ ///
234
+ /// assert_eq!(volatile.read(), 43);
235
+ /// ```
236
+ pub fn update < F > ( & mut self , f : F )
237
+ where
238
+ A : Readable + Writable ,
239
+ R : DerefMut ,
240
+ F : FnOnce ( & mut T ) ,
241
+ {
242
+ let mut value = self . read ( ) ;
243
+ f ( & mut value) ;
244
+ self . write ( value) ;
245
+ }
246
+ }
247
+
161
248
/// Method for extracting the wrapped value.
162
249
impl < R , A > Volatile < R , A > {
163
250
/// Extracts the inner value stored in the wrapper type.
@@ -299,93 +386,6 @@ where
299
386
}
300
387
}
301
388
302
- /// Methods for references to `Copy` types
303
- impl < R , T , A > Volatile < R , A >
304
- where
305
- R : Deref < Target = T > ,
306
- T : Copy ,
307
- {
308
- /// Performs a volatile read of the contained value.
309
- ///
310
- /// Returns a copy of the read value. Volatile reads are guaranteed not to be optimized
311
- /// away by the compiler, but by themselves do not have atomic ordering
312
- /// guarantees. To also get atomicity, consider looking at the `Atomic` wrapper types of
313
- /// the standard/`core` library.
314
- ///
315
- /// ## Examples
316
- ///
317
- /// ```rust
318
- /// use volatile::Volatile;
319
- ///
320
- /// let value = 42;
321
- /// let shared_reference = Volatile::new(&value);
322
- /// assert_eq!(shared_reference.read(), 42);
323
- ///
324
- /// let mut value = 50;
325
- /// let mut_reference = Volatile::new(&mut value);
326
- /// assert_eq!(mut_reference.read(), 50);
327
- /// ```
328
- pub fn read ( & self ) -> T
329
- where
330
- A : Readable ,
331
- {
332
- // UNSAFE: Safe, as we know that our internal value exists.
333
- unsafe { ptr:: read_volatile ( & * self . reference ) }
334
- }
335
-
336
- /// Performs a volatile write, setting the contained value to the given `value`.
337
- ///
338
- /// Volatile writes are guaranteed to not be optimized away by the compiler, but by
339
- /// themselves do not have atomic ordering guarantees. To also get atomicity, consider
340
- /// looking at the `Atomic` wrapper types of the standard/`core` library.
341
- ///
342
- /// ## Example
343
- ///
344
- /// ```rust
345
- /// use volatile::Volatile;
346
- ///
347
- /// let mut value = 42;
348
- /// let mut volatile = Volatile::new(&mut value);
349
- /// volatile.write(50);
350
- ///
351
- /// assert_eq!(volatile.read(), 50);
352
- /// ```
353
- pub fn write ( & mut self , value : T )
354
- where
355
- A : Writable ,
356
- R : DerefMut ,
357
- {
358
- // UNSAFE: Safe, as we know that our internal value exists.
359
- unsafe { ptr:: write_volatile ( & mut * self . reference , value) } ;
360
- }
361
-
362
- /// Updates the contained value using the given closure and volatile instructions.
363
- ///
364
- /// Performs a volatile read of the contained value, passes a mutable reference to it to the
365
- /// function `f`, and then performs a volatile write of the (potentially updated) value back to
366
- /// the contained value.
367
- ///
368
- /// ```rust
369
- /// use volatile::Volatile;
370
- ///
371
- /// let mut value = 42;
372
- /// let mut volatile = Volatile::new(&mut value);
373
- /// volatile.update(|val| *val += 1);
374
- ///
375
- /// assert_eq!(volatile.read(), 43);
376
- /// ```
377
- pub fn update < F > ( & mut self , f : F )
378
- where
379
- A : Readable + Writable ,
380
- R : DerefMut ,
381
- F : FnOnce ( & mut T ) ,
382
- {
383
- let mut value = self . read ( ) ;
384
- f ( & mut value) ;
385
- self . write ( value) ;
386
- }
387
- }
388
-
389
389
/// Methods for volatile slices
390
390
impl < T , R , A > Volatile < R , A >
391
391
where
0 commit comments