jrinx_apex/
basic.rs

1use core::fmt::{Debug, Display};
2
3pub const APEX_NAME_MAX_LEN: usize = 32;
4pub const APEX_CORE_AFFINITY_NO_PREFERENCE: ApexProcessorCoreId = -1;
5
6pub const APEX_PRIORITY_MIN: ApexInteger = 0;
7pub const APEX_PRIORITY_MAX: ApexInteger = 255;
8
9pub const APEX_LOCK_LEVEL_MIN: ApexInteger = 0;
10pub const APEX_LOCK_LEVEL_MAX: ApexInteger = 16;
11
12pub type ApexByte = u8;
13pub type ApexInteger = i32;
14pub type ApexUnsigned = u32;
15pub type ApexLongInteger = i64;
16pub type ApexMessageSize = ApexUnsigned;
17pub type ApexMessageRange = ApexUnsigned;
18pub type ApexWaitingRange = ApexInteger;
19pub type ApexProcessorCoreId = ApexInteger;
20pub type ApexNumCores = ApexUnsigned;
21pub type ApexStackSize = ApexUnsigned;
22pub type ApexPriority = ApexInteger;
23pub type ApexLockLevel = ApexInteger;
24
25#[repr(transparent)]
26#[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
27pub struct ApexName([u8; APEX_NAME_MAX_LEN]);
28
29impl Display for ApexName {
30    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
31        write!(f, "{:?}", self)
32    }
33}
34
35impl Debug for ApexName {
36    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
37        let len = self
38            .0
39            .iter()
40            .position(|&c| c == 0)
41            .unwrap_or(APEX_NAME_MAX_LEN);
42        let s = core::str::from_utf8(&self.0[..len]).unwrap_or("<invalid utf8>");
43        write!(f, "{}", s)
44    }
45}
46
47impl<'a> TryFrom<&'a str> for ApexName {
48    type Error = &'a str;
49
50    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
51        if value.len() > APEX_NAME_MAX_LEN {
52            return Err(value);
53        }
54
55        let mut name = [0; APEX_NAME_MAX_LEN];
56        name[..value.len()].copy_from_slice(value.as_bytes());
57
58        Ok(Self(name))
59    }
60}
61
62impl From<ApexName> for [u8; APEX_NAME_MAX_LEN] {
63    fn from(name: ApexName) -> Self {
64        name.0
65    }
66}
67
68#[repr(transparent)]
69#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
70pub struct ApexSystemAddress(usize);
71
72impl From<ApexSystemAddress> for usize {
73    fn from(addr: ApexSystemAddress) -> Self {
74        addr.0
75    }
76}
77
78impl ApexSystemAddress {
79    pub fn of<R>(f: extern "C" fn() -> R) -> Self {
80        Self(f as usize)
81    }
82}
83
84#[repr(u32)]
85#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
86pub enum ApexReturnCode {
87    #[default]
88    NoError = 0,
89    NoAction = 1,
90    NotAvailable = 2,
91    InvalidParam = 3,
92    InvalidConfig = 4,
93    InvalidMode = 5,
94    TimedOut = 6,
95}
96
97impl From<usize> for ApexReturnCode {
98    fn from(value: usize) -> Self {
99        match value {
100            0 => Self::NoError,
101            1 => Self::NoAction,
102            2 => Self::NotAvailable,
103            3 => Self::InvalidParam,
104            4 => Self::InvalidConfig,
105            5 => Self::InvalidMode,
106            6 => Self::TimedOut,
107            _ => unimplemented!("Unknown return code: {}", value),
108        }
109    }
110}
111
112impl From<ApexReturnCode> for Result<(), ApexReturnCode> {
113    fn from(value: ApexReturnCode) -> Self {
114        value.as_result(())
115    }
116}
117
118impl ApexReturnCode {
119    pub fn as_result<T>(self, ok: T) -> Result<T, ApexReturnCode> {
120        match self {
121            Self::NoError => Ok(ok),
122            err => Err(err),
123        }
124    }
125}
126
127#[repr(u32)]
128#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
129pub enum ApexDeadline {
130    #[default]
131    Soft = 0,
132    Hard = 1,
133}
134
135#[repr(u32)]
136#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
137pub enum ApexPortDirection {
138    #[default]
139    Source = 0,
140    Destination = 1,
141}
142
143impl TryFrom<ApexUnsigned> for ApexPortDirection {
144    type Error = ApexUnsigned;
145
146    fn try_from(value: ApexUnsigned) -> Result<Self, Self::Error> {
147        match value {
148            0 => Ok(Self::Source),
149            1 => Ok(Self::Destination),
150            _ => Err(value),
151        }
152    }
153}
154
155#[repr(u32)]
156#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
157pub enum ApexQueueDiscipline {
158    #[default]
159    Fifo = 0,
160    Priority = 1,
161}
162
163impl TryFrom<ApexUnsigned> for ApexQueueDiscipline {
164    type Error = ApexUnsigned;
165
166    fn try_from(value: ApexUnsigned) -> Result<Self, Self::Error> {
167        match value {
168            0 => Ok(Self::Fifo),
169            1 => Ok(Self::Priority),
170            _ => Err(value),
171        }
172    }
173}