|
| 1 | +#![cfg_attr(feature="const_fn", feature(const_fn))] |
| 2 | + |
1 | 3 | //! Provides wrapper types `Volatile`, `ReadOnly`, `WriteOnly`, `ReadWrite`, which wrap any copy-able type and allows for
|
2 | 4 | //! volatile memory access to wrapped value. Volatile memory accesses are never optimized away by
|
3 | 5 | //! the compiler, and are useful in many low-level systems programming and concurrent contexts.
|
@@ -47,6 +49,23 @@ impl<T: Copy> Volatile<T> {
|
47 | 49 | /// # Panics
|
48 | 50 | ///
|
49 | 51 | /// This method never panics.
|
| 52 | + #[cfg(feature="const_fn")] |
| 53 | + pub const fn new(value: T) -> Volatile<T> { |
| 54 | + Volatile(value) |
| 55 | + } |
| 56 | + |
| 57 | + /// Construct a new volatile instance wrapping the given value. |
| 58 | + /// |
| 59 | + /// ```rust |
| 60 | + /// use volatile::Volatile; |
| 61 | + /// |
| 62 | + /// let value = Volatile::new(0u32); |
| 63 | + /// ``` |
| 64 | + /// |
| 65 | + /// # Panics |
| 66 | + /// |
| 67 | + /// This method never panics. |
| 68 | + #[cfg(not(feature="const_fn"))] |
50 | 69 | pub fn new(value: T) -> Volatile<T> {
|
51 | 70 | Volatile(value)
|
52 | 71 | }
|
@@ -139,8 +158,25 @@ impl<T: Copy> ReadOnly<T> {
|
139 | 158 | /// # Panics
|
140 | 159 | ///
|
141 | 160 | /// This function never panics.
|
| 161 | + #[cfg(feature = "const_fn")] |
| 162 | + pub const fn new(value: T) -> ReadOnly<T> { |
| 163 | + ReadOnly(Volatile::new(value)) |
| 164 | + } |
| 165 | + |
| 166 | + /// Construct a new read-only volatile wrapper wrapping the given value. |
| 167 | + /// |
| 168 | + /// ```rust |
| 169 | + /// use volatile::ReadOnly; |
| 170 | + /// |
| 171 | + /// let value = ReadOnly::new(42u32); |
| 172 | + /// ``` |
| 173 | + /// |
| 174 | + /// # Panics |
| 175 | + /// |
| 176 | + /// This function never panics. |
| 177 | + #[cfg(not(feature = "const_fn"))] |
142 | 178 | pub fn new(value: T) -> ReadOnly<T> {
|
143 |
| - ReadOnly(Volatile(value)) |
| 179 | + ReadOnly(Volatile::new(value)) |
144 | 180 | }
|
145 | 181 |
|
146 | 182 | /// Perform a volatile read of the contained value, returning a copy of the read value.
|
@@ -179,8 +215,25 @@ impl<T: Copy> WriteOnly<T> {
|
179 | 215 | /// # Panics
|
180 | 216 | ///
|
181 | 217 | /// This function never panics.
|
| 218 | + #[cfg(feature = "const_fn")] |
| 219 | + pub const fn new(value: T) -> WriteOnly<T> { |
| 220 | + WriteOnly(Volatile::new(value)) |
| 221 | + } |
| 222 | + |
| 223 | + /// Constructs a new write only volatile wrapper around the given value. |
| 224 | + /// |
| 225 | + /// ```rust |
| 226 | + /// use volatile::WriteOnly; |
| 227 | + /// |
| 228 | + /// let value = WriteOnly::new(0u32); |
| 229 | + /// ``` |
| 230 | + /// |
| 231 | + /// # Panics |
| 232 | + /// |
| 233 | + /// This function never panics. |
| 234 | + #[cfg(not(feature = "const_fn"))] |
182 | 235 | pub fn new(value: T) -> WriteOnly<T> {
|
183 |
| - WriteOnly(Volatile(value)) |
| 236 | + WriteOnly(Volatile::new(value)) |
184 | 237 | }
|
185 | 238 |
|
186 | 239 | /// Performs a volatile write of value `value` into the contained value. Functionally identical
|
|
0 commit comments