Struct volatile::Volatile

source ·
#[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§

source§

impl<T: Copy> Volatile<T>

source

pub fn new(value: T) -> Volatile<T>

Construct a new volatile instance wrapping the given value.

use volatile::Volatile;

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

This method never panics.

source

pub fn read(&self) -> T

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.

source

pub fn write(&mut self, value: T)

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.

source

pub fn update<F>(&mut self, f: F)where F: FnOnce(&mut T),

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§

source§

impl<T: Copy> Clone for Volatile<T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug + Copy> Debug for Volatile<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default + Copy> Default for Volatile<T>

source§

fn default() -> Volatile<T>

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

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Volatile<T>where T: RefUnwindSafe,

§

impl<T> Send for Volatile<T>where T: Send,

§

impl<T> Sync for Volatile<T>where T: Sync,

§

impl<T> Unpin for Volatile<T>where T: Unpin,

§

impl<T> UnwindSafe for Volatile<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.