Debian Codegraph @

src/lib.rs

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #![cfg_attr(feature = "const_fn", feature(const_fn))]
2
3 //! Provides wrapper types `Volatile`, `ReadOnly`, `WriteOnly`, `ReadWrite`, which wrap any copy-able type and allows for
4 //! volatile memory access to wrapped value. Volatile memory accesses are never optimized away by
5 //! the compiler, and are useful in many low-level systems programming and concurrent contexts.
6 //!
7 //! The wrapper types *do not* enforce any atomicity guarantees; to also get atomicity, consider
8 //! looking at the `Atomic` wrapper type found in `libcore` or `libstd`.
9 //!
10 //! These wrappers do not depend on the standard library and never panic.
11 //!
12 //! # Dealing with Volatile Pointers
13 //!
14 //! Frequently, one may have to deal with volatile pointers, eg, writes to specific memory
15 //! locations. The canonical way to solve this is to cast the pointer to a volatile wrapper
16 //! directly, eg:
17 //!
18 //! ```rust
19 //! use volatile::Volatile;
20 //!
21 //! let mut_ptr = 0xFEE00000 as *mut u32;
22 //!
23 //! let volatile_ptr = mut_ptr as *mut Volatile<u32>;
24 //! ```
25 //!
26 //! and then perform operations on the pointer as usual in a volatile way. This method works as all
27 //! of the volatile wrapper types are the same size as their contained values.
28 #![no_std]
29
30 use core::ptr;
31
32 /// A wrapper type around a volatile variable, which allows for volatile reads and writes
33 /// to the contained value. The stored type needs to be `Copy`, as volatile reads and writes
34 /// take and return copies of the value.
35 ///
36 /// The size of this struct is the same as the size of the contained type.
37 #[derive(Debug, Default)]
38 #[repr(transparent)]
39 pub struct Volatile<T: Copy>(T);
40
41 impl<T: Copy> Volatile<T> {
42 /// Construct a new volatile instance wrapping the given value.
43 ///
44 /// ```rust
45 /// use volatile::Volatile;
46 ///
47 /// let value = Volatile::new(0u32);
48 /// ```
49 ///
50 /// # Panics
51 ///
52 /// This method never panics.
53 #[cfg(feature = "const_fn")]
54 pub const fn new(value: T) -> Volatile<T> {
55 Volatile(value)
56 }
57
58 /// Construct a new volatile instance wrapping the given value.
59 ///
60 /// ```rust
61 /// use volatile::Volatile;
62 ///
63 /// let value = Volatile::new(0u32);
64 /// ```
65 ///
66 /// # Panics
67 ///
68 /// This method never panics.
69 #[cfg(not(feature = "const_fn"))]
70 pub fn new(value: T) -> Volatile<T> {
71 Volatile(value)
72 }
73
74 /// Performs a volatile read of the contained value, returning a copy
75 /// of the read value. Volatile reads are guaranteed not to be optimized
76 /// away by the compiler, but by themselves do not have atomic ordering
77 /// guarantees. To also get atomicity, consider looking at the `Atomic` wrapper type.
78 ///
79 /// ```rust
80 /// use volatile::Volatile;
81 ///
82 /// let value = Volatile::new(42u32);
83 ///
84 /// assert_eq!(value.read(), 42u32);
85 /// ```
86 ///
87 /// # Panics
88 ///
89 /// This method never panics.
90 pub fn read(&self) -> T {
91 // UNSAFE: Safe, as we know that our internal value exists.
92 unsafe { ptr::read_volatile(&self.0) }
93 }
94
95 /// Performs a volatile write, setting the contained value to the given value `value`. Volatile
96 /// writes are guaranteed to not be optimized away by the compiler, but by themselves do not
97 /// have atomic ordering guarantees. To also get atomicity, consider looking at the `Atomic`
98 /// wrapper type.
99 ///
100 /// ```rust
101 /// use volatile::Volatile;
102 ///
103 /// let mut value = Volatile::new(0u32);
104 ///
105 /// value.write(42u32);
106 ///
107 /// assert_eq!(value.read(), 42u32);
108 /// ```
109 ///
110 /// # Panics
111 ///
112 /// This method never panics.
113 pub fn write(&mut self, value: T) {
114 // UNSAFE: Safe, as we know that our internal value exists.
115 unsafe { ptr::write_volatile(&mut self.0, value) };
116 }
117
118 /// Performs a volatile read of the contained value, passes a mutable reference to it to the
119 /// function `f`, and then performs a volatile write of the (potentially updated) value back to
120 /// the contained value.
121 ///
122 /// ```rust
123 /// use volatile::Volatile;
124 ///
125 /// let mut value = Volatile::new(21u32);
126 ///
127 /// value.update(|val_ref| *val_ref *= 2);
128 ///
129 /// assert_eq!(value.read(), 42u32);
130 /// ```
131 ///
132 /// # Panics
133 ///
134 /// Ths method never panics.
135 pub fn update<F>(&mut self, f: F)
136 where
137 F: FnOnce(&mut T),
138 {
139 let mut value = self.read();
140 f(&mut value);
141 self.write(value);
142 }
143 }
144
145 impl<T: Copy> Clone for Volatile<T> {
146 fn clone(&self) -> Self {
147 Volatile(self.read())
148 }
149 }
150
151 /// A volatile wrapper which only allows read operations.
152 ///
153 /// The size of this struct is the same as the contained type.
154 #[derive(Debug, Clone, Default)]
155 pub struct ReadOnly<T: Copy>(Volatile<T>);
156
157 impl<T: Copy> ReadOnly<T> {
158 /// Construct a new read-only volatile wrapper wrapping the given value.
159 ///
160 /// ```rust
161 /// use volatile::ReadOnly;
162 ///
163 /// let value = ReadOnly::new(42u32);
164 /// ```
165 ///
166 /// # Panics
167 ///
168 /// This function never panics.
169 #[cfg(feature = "const_fn")]
170 pub const fn new(value: T) -> ReadOnly<T> {
171 ReadOnly(Volatile::new(value))
172 }
173
174 /// Construct a new read-only volatile wrapper wrapping the given value.
175 ///
176 /// ```rust
177 /// use volatile::ReadOnly;
178 ///
179 /// let value = ReadOnly::new(42u32);
180 /// ```
181 ///
182 /// # Panics
183 ///
184 /// This function never panics.
185 #[cfg(not(feature = "const_fn"))]
186 pub fn new(value: T) -> ReadOnly<T> {
187 ReadOnly(Volatile::new(value))
188 }
189
190 /// Perform a volatile read of the contained value, returning a copy of the read value.
191 /// Functionally equivalent to `Volatile::read`.
192 ///
193 /// ```rust
194 /// use volatile::ReadOnly;
195 ///
196 /// let value = ReadOnly::new(42u32);
197 /// assert_eq!(value.read(), 42u32);
198 /// ```
199 ///
200 /// # Panics
201 ///
202 /// This function never panics.
203 pub fn read(&self) -> T {
204 self.0.read()
205 }
206 }
207
208 /// A volatile wrapper which only allows write operations.
209 ///
210 /// The size of this struct is the same as the contained type.
211 #[derive(Default)]
212 pub struct WriteOnly<T: Copy>(Volatile<T>);
213
214 impl<T: Copy> WriteOnly<T> {
215 /// Constructs a new write only volatile wrapper around the given value.
216 ///
217 /// ```rust
218 /// use volatile::WriteOnly;
219 ///
220 /// let value = WriteOnly::new(0u32);
221 /// ```
222 ///
223 /// # Panics
224 ///
225 /// This function never panics.
226 #[cfg(feature = "const_fn")]
227 pub const fn new(value: T) -> WriteOnly<T> {
228 WriteOnly(Volatile::new(value))
229 }
230
231 /// Constructs a new write only volatile wrapper around the given value.
232 ///
233 /// ```rust
234 /// use volatile::WriteOnly;
235 ///
236 /// let value = WriteOnly::new(0u32);
237 /// ```
238 ///
239 /// # Panics
240 ///
241 /// This function never panics.
242 #[cfg(not(feature = "const_fn"))]
243 pub fn new(value: T) -> WriteOnly<T> {
244 WriteOnly(Volatile::new(value))
245 }
246
247 /// Performs a volatile write of value `value` into the contained value. Functionally identical
248 /// to `Volatile::write`.
249 ///
250 /// ```rust
251 /// use volatile::WriteOnly;
252 ///
253 /// let mut value = WriteOnly::new(0u32);
254 ///
255 /// value.write(42u32);
256 /// ```
257 ///
258 /// # Panics
259 ///
260 /// This method never panics.
261 pub fn write(&mut self, value: T) {
262 self.0.write(value)
263 }
264 }
265
266 /// A volatile wrapper which allows both read and write operations;
267 /// functionally equivalent to the `Volatile` type, as it is a type
268 /// alias for it.
269 ///
270 /// The size of this struct is the same as the contained type.
271 pub type ReadWrite<T> = Volatile<T>;
272
273 #[cfg(test)]
274 mod tests {
275 use super::Volatile;
276
277 #[test]
278 fn test_read() {
279 assert_eq!(Volatile(42).read(), 42);
280 }
281
282 #[test]
283 fn test_write() {
284 let mut volatile = Volatile(42);
285 volatile.write(50);
286 assert_eq!(volatile.0, 50);
287 }
288
289 #[test]
290 fn test_update() {
291 let mut volatile = Volatile(42);
292 volatile.update(|v| *v += 1);
293 assert_eq!(volatile.0, 43);
294 }
295
296 #[test]
297 fn test_pointer_recast() {
298 let mut target_value = 0u32;
299
300 let target_ptr: *mut u32 = &mut target_value;
301 let volatile_ptr = target_ptr as *mut Volatile<u32>;
302
303 // UNSAFE: Safe, as we know the value exists on the stack.
304 unsafe {
305 (*volatile_ptr).write(42u32);
306 }
307
308 assert_eq!(target_value, 42u32);
309 }
310 }