reg_map/
access.rs

1//! Helper types and traits to define read/write permissions on registers.
2
3use core::fmt::Debug;
4use core::hash::Hash;
5
6/// A zero-sized type indicating that a register provides only read access.
7///
8/// Implements the [`Readable`] trait.
9#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct ReadOnly {}
11
12/// A zero-sized type indicating that a register provides only write access.
13///
14/// Implements the [`Writable`] trait.
15#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
16pub struct WriteOnly {}
17
18/// A zero-sized type indicating that a register provides both read and write access.
19///
20/// Implements the [`Readable`] and [`Writable`] traits.
21#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub struct ReadWrite {}
23
24/// Marker trait required by traits [`Readable`] and [`Writable`];
25///
26/// ⚠️ This trait is sealed and cannot be implemented for types outside of this crate.
27pub trait Access:
28    Debug + Default + Copy + Eq + Ord + Hash + Sized + Send + Sync + 'static + private::Sealed
29{
30}
31
32/// Marker trait for readable registers implemented by types [`ReadOnly`] and [`ReadWrite`].
33///
34/// ⚠️ This trait is sealed and cannot be implemented for types outside of this crate.
35#[diagnostic::on_unimplemented(
36    message = "cannot read from a write-only register",
37    label = "method cannot be called on write-only registers",
38    note = "the register is write only because it was annotated with the attribute
39  `#[reg(WO)]` in the register-map definition"
40)]
41pub trait Readable: Access {}
42
43/// Marker trait for writable registers implemented by types [`WriteOnly`] and [`ReadWrite`].
44///
45/// ⚠️ This trait is sealed and cannot be implemented for types outside of this crate.
46#[diagnostic::on_unimplemented(
47    message = "cannot write to a read-only register",
48    label = "method cannot be called on read-only registers",
49    note = "the register is read only because it was annotated with the attribute
50  `#[reg(RO)]` in the register-map definition"
51)]
52pub trait Writable: Access {}
53
54impl Access for ReadOnly {}
55impl Access for WriteOnly {}
56impl Access for ReadWrite {}
57impl Readable for ReadOnly {}
58impl Readable for ReadWrite {}
59impl Writable for WriteOnly {}
60impl Writable for ReadWrite {}
61
62mod private {
63    pub trait Sealed {}
64    impl Sealed for super::ReadOnly {}
65    impl Sealed for super::WriteOnly {}
66    impl Sealed for super::ReadWrite {}
67}