# volatile - Rust
Provides wrapper types `Volatile`, `ReadOnly`, `WriteOnly`, `ReadWrite`, which wrap any copy-able type and allows for volatile memory access to wrapped value. Volatile memory accesses are never optimized away by the compiler, and are useful in many low-level systems programming and concurrent contexts.
Keywords: rust, rustlang, rust-lang, volatile

# Crate volatile

source · [−]
Expand description

Provides wrapper types `Volatile`, `ReadOnly`, `WriteOnly`, `ReadWrite`, which wrap any copy-able type and allows for volatile memory access to wrapped value. Volatile memory accesses are never optimized away by the compiler, and are useful in many low-level systems programming and concurrent contexts.

The wrapper types do not enforce any atomicity guarantees; to also get atomicity, consider looking at the `Atomic` wrapper type found in `libcore` or `libstd`.

These wrappers do not depend on the standard library and never panic.

## Dealing with Volatile Pointers

Frequently, one may have to deal with volatile pointers, eg, writes to specific memory locations. The canonical way to solve this is to cast the pointer to a volatile wrapper directly, eg:

    use volatile::Volatile;

    let mut_ptr = 0xFEE00000 as *mut u32;

    let volatile_ptr = mut_ptr as *mut Volatile<u32>;

and then perform operations on the pointer as usual in a volatile way. This method works as all of the volatile wrapper types are the same size as their contained values.

## Structs

ReadOnly

A volatile wrapper which only allows read operations.

Volatile

A wrapper type around a volatile variable, which allows for volatile reads and writes to the contained value. The stored type needs to be `Copy`, as volatile reads and writes take and return copies of the value.

WriteOnly

A volatile wrapper which only allows write operations.

## Type Definitions

ReadWrite

A volatile wrapper which allows both read and write operations; functionally equivalent to the `Volatile` type, as it is a type alias for it.