#![doc = include_str!("../README.md")]
#![no_std]
#![deny(unsafe_code, missing_docs)]
#![allow(async_fn_in_trait)]
pub use device_register;
use device_register::{EditableRegister, ReadableRegister, Register, WritableRegister};
pub trait RegisterInterface<R, A>
where
R: Register<Address = A>,
{
type Error;
async fn read_register(&mut self) -> Result<R, Self::Error>;
async fn write_register(&mut self, register: &R) -> Result<(), Self::Error>;
}
pub trait ReadRegister<R, A>
where
R: ReadableRegister<Address = A>,
{
type Error;
async fn read(&mut self) -> Result<R, Self::Error>;
}
pub trait WriteRegister<R, A>
where
R: WritableRegister<Address = A>,
{
type Error;
async fn write(&mut self, register: R) -> Result<(), Self::Error>;
}
pub trait EditRegister<R, A>
where
for<'a> R: EditableRegister<Address = A> + 'a,
{
type Error;
async fn edit<F>(&mut self, f: F) -> Result<(), Self::Error>
where
for<'w> F: FnOnce(&'w mut R);
}
impl<I, R, A> ReadRegister<R, A> for I
where
for<'a> R: ReadableRegister<Address = A> + 'a,
I: RegisterInterface<R, A>,
for<'a> A: 'a,
{
type Error = I::Error;
async fn read(&mut self) -> Result<R, Self::Error> {
self.read_register().await
}
}
impl<I, R, A> WriteRegister<R, A> for I
where
for<'a> R: WritableRegister<Address = A> + 'a,
I: RegisterInterface<R, A>,
for<'a> A: 'a,
{
type Error = I::Error;
async fn write(&mut self, register: R) -> Result<(), Self::Error> {
self.write_register(®ister).await
}
}
impl<I, R, A> EditRegister<R, A> for I
where
for<'a> R: EditableRegister<Address = A> + 'a,
I: RegisterInterface<R, A>,
for<'a> A: 'a,
{
type Error = I::Error;
async fn edit<F>(&mut self, f: F) -> Result<(), Self::Error>
where
for<'w> F: FnOnce(&'w mut R),
{
let mut val = self.read_register().await?;
f(&mut val);
self.write_register(&val).await
}
}