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> {
26     inner: I
27 }
28 
29 impl<I> 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 
37 impl<I: Io> ReadOnly<I> {
38     #[inline(always)]
read(&self) -> I::Value39     pub fn read(&self) -> I::Value {
40         self.inner.read()
41     }
42 
43     #[inline(always)]
readf(&self, flags: I::Value) -> bool44     pub fn readf(&self, flags: I::Value) -> bool {
45         self.inner.readf(flags)
46     }
47 }
48 
49 pub struct WriteOnly<I> {
50     inner: I
51 }
52 
53 impl<I> WriteOnly<I> {
new(inner: I) -> WriteOnly<I>54     pub const fn new(inner: I) -> WriteOnly<I> {
55         WriteOnly {
56             inner: inner
57         }
58     }
59 }
60 
61 impl<I: Io> WriteOnly<I> {
62     #[inline(always)]
write(&mut self, value: I::Value)63     pub fn write(&mut self, value: I::Value) {
64         self.inner.write(value)
65     }
66 
67     #[inline(always)]
writef(&mut self, flags: I::Value, value: bool)68     pub fn writef(&mut self, flags: I::Value, value: bool) {
69         self.inner.writef(flags, value)
70     }
71 }
72