Skip to main content

catalejo_sys/
id.rs

1//! Monotonic identifiers for kernel-managed state.
2
3use core::mem;
4use core::num::NonZero;
5use core::ops::{Deref, DerefMut};
6
7use crate::ffi::binding::mirilla_id_t;
8
9/// A type alias to the identifier type for a engaged target.
10pub type TargetId = Id;
11
12/// A type alias to the identifier type for a peephole.
13pub type PeepholeId = Id;
14
15/// An identifier managed by the module.
16///
17/// This is used to refer to any contextual structure held in the kernel for `mirilla`.
18#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
19#[repr(transparent)]
20pub struct Id(pub NonZero<mirilla_id_t>);
21
22impl Deref for Id {
23    type Target = NonZero<mirilla_id_t>;
24
25    #[inline]
26    fn deref(&self) -> &Self::Target {
27        let Self(target_value) = self;
28
29        target_value
30    }
31}
32
33impl DerefMut for Id {
34    #[inline]
35    fn deref_mut(&mut self) -> &mut Self::Target {
36        let Self(target_value) = self;
37
38        target_value
39    }
40}
41
42// NOTE: Assert that such `Id` type is valid to be layed out as an `mirilla` identifier.
43const _: () = const {
44    use crate::ffi::binding;
45
46    assert!(mem::size_of::<Option<Id>>() == mem::size_of::<binding::mirilla_id_t>());
47    assert!(mem::align_of::<Option<Id>>() == mem::align_of::<binding::mirilla_id_t>());
48
49    // NOTE: Bogus, but better to be safe than sorry..
50
51    assert!(
52        mem::size_of::<Option<TargetId>>() == mem::size_of::<binding::mirilla_map_target_id_t>()
53    );
54    assert!(
55        mem::align_of::<Option<TargetId>>() == mem::align_of::<binding::mirilla_map_target_id_t>()
56    );
57
58    assert!(
59        mem::size_of::<Option<PeepholeId>>()
60            == mem::size_of::<binding::mirilla_map_peephole_id_t>()
61    );
62    assert!(
63        mem::align_of::<Option<PeepholeId>>()
64            == mem::align_of::<binding::mirilla_map_peephole_id_t>()
65    );
66};