VolatilePtr should be testing safe now

This commit is contained in:
Lokathor 2018-11-14 19:47:56 -07:00
parent 8be6836e3e
commit 3789e206f3

View file

@ -5,7 +5,7 @@
/// ///
/// Accessing the GBA's IO registers and video ram and specific other places on /// Accessing the GBA's IO registers and video ram and specific other places on
/// **must** be done with volatile operations. Having this wrapper makes that /// **must** be done with volatile operations. Having this wrapper makes that
/// more clear for all the global const values into IO registers. /// more clear for all the const value IO registers.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)] #[repr(transparent)]
pub struct VolatilePtr<T>(pub *mut T); pub struct VolatilePtr<T>(pub *mut T);
@ -25,8 +25,15 @@ impl<T> VolatilePtr<T> {
/// This method adds absolutely no additional safety, so all safety concerns /// This method adds absolutely no additional safety, so all safety concerns
/// for a normal raw pointer volatile read apply. /// for a normal raw pointer volatile read apply.
pub unsafe fn read(&self) -> T { pub unsafe fn read(&self) -> T {
#[cfg(not(test))]
{
core::ptr::read_volatile(self.0) core::ptr::read_volatile(self.0)
} }
#[cfg(test)]
{
core::mem::zeroed::<T>()
}
}
/// Performs a volatile write. /// Performs a volatile write.
/// ///
@ -35,8 +42,15 @@ impl<T> VolatilePtr<T> {
/// This method adds absolutely no additional safety, so all safety concerns /// This method adds absolutely no additional safety, so all safety concerns
/// for a normal raw pointer volatile write apply. /// for a normal raw pointer volatile write apply.
pub unsafe fn write(&self, data: T) { pub unsafe fn write(&self, data: T) {
#[cfg(not(test))]
{
core::ptr::write_volatile(self.0, data); core::ptr::write_volatile(self.0, data);
} }
#[cfg(test)]
{
drop(data)
}
}
/// Offsets this address by the amount given. /// Offsets this address by the amount given.
/// ///
@ -45,6 +59,13 @@ impl<T> VolatilePtr<T> {
/// This is a standard offset, so all safety concerns of a normal raw pointer /// This is a standard offset, so all safety concerns of a normal raw pointer
/// offset apply. /// offset apply.
pub unsafe fn offset(self, count: isize) -> Self { pub unsafe fn offset(self, count: isize) -> Self {
#[cfg(not(test))]
{
VolatilePtr(self.0.offset(count)) VolatilePtr(self.0.offset(count))
} }
#[cfg(test)]
{
VolatilePtr(self.0.wrapping_offset(count))
}
}
} }