#[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
sourceimpl<T: Copy> Volatile<T>
impl<T: Copy> Volatile<T>
sourcepub fn new(value: T) -> Volatile<T>
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.
sourcepub fn read(&self) -> T
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.
sourcepub fn write(&mut self, value: T)
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.
sourcepub fn update<F>(&mut self, f: F) where
F: FnOnce(&mut T),
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
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more