#![cfg_attr(feature = "const_fn", feature(const_fn))]
#![no_std]
use core::ptr;
#[derive(Debug, Default)]
#[repr(transparent)]
pub struct Volatile<T: Copy>(T);
impl<T: Copy> Volatile<T> {
#[cfg(feature = "const_fn")]
pub const fn new(value: T) -> Volatile<T> {
Volatile(value)
}
#[cfg(not(feature = "const_fn"))]
pub fn new(value: T) -> Volatile<T> {
Volatile(value)
}
pub fn read(&self) -> T {
unsafe { ptr::read_volatile(&self.0) }
}
pub fn write(&mut self, value: T) {
unsafe { ptr::write_volatile(&mut self.0, value) };
}
pub fn update<F>(&mut self, f: F)
where
F: FnOnce(&mut T),
{
let mut value = self.read();
f(&mut value);
self.write(value);
}
}
impl<T: Copy> Clone for Volatile<T> {
fn clone(&self) -> Self {
Volatile(self.read())
}
}
pub type ReadWrite<T> = Volatile<T>;