sel4_driver_interfaces/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// Copyright 2024, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

#![no_std]

use core::cell::{RefCell, RefMut};
use core::fmt;
use core::ops::{Deref, DerefMut};

use lock_api::{Mutex, RawMutex};

pub mod block;
pub mod net;
pub mod rtc;
pub mod serial;
pub mod timer;

pub trait HandleInterrupt {
    fn handle_interrupt(&mut self);
}

// // //

#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Default)]
pub struct WrappedRefCell<T>(pub T);

impl<T> WrappedRefCell<T> {
    pub(crate) fn try_borrow_mut<E, U>(&self) -> Result<RefMut<U>, WrappedRefCellError<E>>
    where
        T: Deref<Target = RefCell<U>>,
    {
        self.0
            .deref()
            .try_borrow_mut()
            .map_err(|_| WrappedRefCellError::Contention)
    }

    pub(crate) fn with_mut<E, U, V>(
        &self,
        f: impl FnOnce(&mut U) -> Result<V, E>,
    ) -> Result<V, WrappedRefCellError<E>>
    where
        T: Deref<Target = RefCell<U>>,
    {
        Ok(f(self.try_borrow_mut()?.deref_mut())?)
    }
}

#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub enum WrappedRefCellError<E> {
    Contention,
    Other(E),
}

impl<E> From<E> for WrappedRefCellError<E> {
    fn from(err: E) -> Self {
        Self::Other(err)
    }
}

impl<E: fmt::Display> fmt::Display for WrappedRefCellError<E> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Contention => write!(f, "contention"),
            Self::Other(err) => err.fmt(f),
        }
    }
}

#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Default)]
pub struct WrappedMutex<T>(pub T);

impl<T> WrappedMutex<T> {
    pub(crate) fn with_mut<R: RawMutex, E, U, V>(
        &self,
        f: impl FnOnce(&mut U) -> Result<V, E>,
    ) -> Result<V, E>
    where
        T: Deref<Target = Mutex<R, U>>,
    {
        f(self.0.lock().deref_mut())
    }
}