hwio/
io.rs

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