1 use core::cmp::PartialEq; 2 use core::ops::{BitAnd, BitOr, Not}; 3 4 pub trait Io { 5 type Value: Copy + PartialEq + BitAnd<Output = Self::Value> + BitOr<Output = Self::Value> + Not<Output = Self::Value>; 6 read(&self) -> Self::Value7 fn read(&self) -> Self::Value; write(&mut self, value: Self::Value)8 fn write(&mut self, value: Self::Value); 9 10 #[inline(always)] readf(&self, flags: Self::Value) -> bool11 fn readf(&self, flags: Self::Value) -> bool { 12 (self.read() & flags) as Self::Value == flags 13 } 14 15 #[inline(always)] writef(&mut self, flags: Self::Value, value: bool)16 fn writef(&mut self, flags: Self::Value, value: bool) { 17 let tmp: Self::Value = match value { 18 true => self.read() | flags, 19 false => self.read() & !flags, 20 }; 21 self.write(tmp); 22 } 23 } 24 25 pub struct ReadOnly<I: Io> { 26 inner: I 27 } 28 29 impl<I: Io> ReadOnly<I> { new(inner: I) -> ReadOnly<I>30 pub const fn new(inner: I) -> ReadOnly<I> { 31 ReadOnly { 32 inner: inner 33 } 34 } 35 36 #[inline(always)] read(&self) -> I::Value37 pub fn read(&self) -> I::Value { 38 self.inner.read() 39 } 40 41 #[inline(always)] readf(&self, flags: I::Value) -> bool42 pub fn readf(&self, flags: I::Value) -> bool { 43 self.inner.readf(flags) 44 } 45 } 46 47 pub struct WriteOnly<I: Io> { 48 inner: I 49 } 50 51 impl<I: Io> WriteOnly<I> { new(inner: I) -> WriteOnly<I>52 pub const fn new(inner: I) -> WriteOnly<I> { 53 WriteOnly { 54 inner: inner 55 } 56 } 57 58 #[inline(always)] write(&mut self, value: I::Value)59 pub fn write(&mut self, value: I::Value) { 60 self.inner.write(value) 61 } 62 63 #[inline(always)] writef(&mut self, flags: I::Value, value: bool)64 pub fn writef(&mut self, flags: I::Value, value: bool) { 65 self.inner.writef(flags, value) 66 } 67 } 68