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