base_address/
lib.rs

1//! Static and dynamic base address for peripheral buses.
2#![no_std]
3
4/// Types that would represent a base address of peripheral.
5pub trait BaseAddress {
6    /// Return pointer representation of this base address.
7    fn ptr(&self) -> *const ();
8}
9
10/// Address known on compile time.
11///
12/// This is a zero-sized type; structures with `Static<A>` as
13/// parameter would not take additional memory space.
14#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct Static<const A: usize>;
16
17impl<const A: usize> BaseAddress for Static<A> {
18    #[inline(always)]
19    fn ptr(&self) -> *const () {
20        A as *const ()
21    }
22}
23
24/// Address only known on runtime but not compile time.
25#[derive(Clone, Debug, PartialEq, Eq)]
26pub struct Dynamic {
27    base: usize,
28}
29
30impl Dynamic {
31    /// Create a dynamically known base address.
32    #[inline]
33    pub const fn new(base: usize) -> Self {
34        Dynamic { base }
35    }
36}
37
38impl BaseAddress for Dynamic {
39    #[inline(always)]
40    fn ptr(&self) -> *const () {
41        self.base as *const ()
42    }
43}