#[repr(transparent)]
pub struct Volatile<T: Copy>(_);
Expand description

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.

The size of this struct is the same as the size of the contained type.

Implementations

Construct a new volatile instance wrapping the given value.

use volatile::Volatile;

let value = Volatile::new(0u32);
Panics

This method never panics.

Performs a volatile read of the contained value, returning a copy of the read value. Volatile reads are guaranteed not to be optimized away by the compiler, but by themselves do not have atomic ordering guarantees. To also get atomicity, consider looking at the Atomic wrapper type.

use volatile::Volatile;

let value = Volatile::new(42u32);

assert_eq!(value.read(), 42u32);
Panics

This method never panics.

Performs a volatile write, setting the contained value to the given value value. Volatile writes are guaranteed to not be optimized away by the compiler, but by themselves do not have atomic ordering guarantees. To also get atomicity, consider looking at the Atomic wrapper type.

use volatile::Volatile;

let mut value = Volatile::new(0u32);

value.write(42u32);

assert_eq!(value.read(), 42u32);
Panics

This method never panics.

Performs a volatile read of the contained value, passes a mutable reference to it to the function f, and then performs a volatile write of the (potentially updated) value back to the contained value.

use volatile::Volatile;

let mut value = Volatile::new(21u32);

value.update(|val_ref| *val_ref *= 2);

assert_eq!(value.read(), 42u32);
Panics

Ths method never panics.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.