Function std::ptr::read_volatile 1.9.0[−][src]
pub unsafe fn read_volatile<T>(src: *const T) -> T
Performs a volatile read of the value from src
without moving it. This
leaves the memory in src
unchanged.
Volatile operations are intended to act on I/O memory, and are guaranteed to not be elided or reordered by the compiler across other volatile operations.
Memory read with read_volatile
should almost always be written to using
write_volatile
.
Notes
Rust does not currently have a rigorously and formally defined memory model, so the precise semantics of what "volatile" means here is subject to change over time. That being said, the semantics will almost always end up pretty similar to C11's definition of volatile.
The compiler shouldn't change the relative order or number of volatile
memory operations. However, volatile memory operations on zero-sized types
(e.g. if a zero-sized type is passed to read_volatile
) are no-ops
and may be ignored.
Safety
Behavior is undefined if any of the following conditions are violated:
-
src
must be valid. -
src
must be properly aligned.
Like read
, read_unaligned
creates a bitwise copy of T
, regardless of
whether T
is Copy
. If T
is not Copy
, using both the returned
value and the value at *src
can [violate memory safety][read-ownership].
However, storing non-Copy
types in volatile memory is almost certainly
incorrect.
Examples
Basic usage:
let x = 12; let y = &x as *const i32; unsafe { assert_eq!(std::ptr::read_volatile(y), 12); }Run