Volatile

Struct Volatile 

Source
pub struct Volatile<T>(/* private fields */)
where
    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> Volatile<T>
where T: Copy,

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> Clone for Volatile<T>
where T: Copy,

Source§

fn clone(&self) -> Volatile<T>

Returns a duplicate 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 for Volatile<T>
where T: Debug + Copy,

Source§

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

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

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

Source§

fn default() -> Volatile<T>

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

Auto Trait Implementations§

§

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

§

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 T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.