Add implementation of set bits to save bit twiddling

This commit is contained in:
Gwilym Kuiper 2021-04-15 23:05:53 +01:00 committed by Corwin
parent 5543f1a7b0
commit 404be633c1

View file

@ -1,3 +1,5 @@
use core::ops;
pub struct MemoryMapped<T> { pub struct MemoryMapped<T> {
address: *mut T, address: *mut T,
} }
@ -18,6 +20,24 @@ impl<T> MemoryMapped<T> {
} }
} }
impl<T> MemoryMapped<T>
where
T: From<u8>
+ Copy
+ ops::Shl<Output = T>
+ ops::BitAnd<Output = T>
+ ops::Sub<Output = T>
+ ops::BitOr<Output = T>
+ ops::Not<Output = T>,
{
pub fn set_bits(&self, value: T, length: T, shift: T) {
let one: T = 1u8.into();
let mask: T = (one << length) - one;
let current_val = self.get();
self.set((current_val & !(mask << shift)) | ((value & mask) << shift));
}
}
pub struct MemoryMapped1DArray<T, const N: usize> { pub struct MemoryMapped1DArray<T, const N: usize> {
array: *mut [T; N], array: *mut [T; N],
} }