syscall/io/
io.rs

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