daicon_types/
header.rs

1use std::num::NonZeroU64;
2
3use bytemuck::{Pod, Zeroable};
4
5use crate::SIGNATURE;
6
7/// Header of a daicon table.
8///
9/// When creating a new table for writing, using the `Default` implementation will automatically
10/// fill the signature.
11#[derive(Pod, Zeroable, PartialEq, Hash, Debug, Clone, Copy)]
12#[repr(C)]
13pub struct Header {
14    signature: u32,
15    capacity: u16,
16    valid: u16,
17    offset: u64,
18    next: u64,
19}
20
21impl Header {
22    /// Get the magic signature field of this table.
23    pub fn signature(&self) -> u32 {
24        self.signature
25    }
26
27    /// Set `signature`.
28    pub fn set_signature(&mut self, value: u32) {
29        self.signature = value;
30    }
31
32    /// Get the amount of indices of allocated space available in this table.
33    pub fn capacity(&self) -> u16 {
34        self.capacity
35    }
36
37    /// Set `capacity`.
38    pub fn set_capacity(&mut self, value: u16) {
39        self.capacity = value;
40    }
41
42    /// Get the amount of indices that contain valid data in this table.
43    pub fn valid(&self) -> u16 {
44        self.valid
45    }
46
47    /// Set `valid`.
48    pub fn set_valid(&mut self, value: u16) {
49        self.valid = value;
50    }
51
52    /// Get the offset that all indices are relative to.
53    pub fn offset(&self) -> u64 {
54        self.offset
55    }
56
57    /// Set `offset`.
58    pub fn set_offset(&mut self, value: u64) {
59        self.offset = value;
60    }
61
62    /// Get the offset of the next table.
63    pub fn next(&self) -> Option<NonZeroU64> {
64        NonZeroU64::new(self.next)
65    }
66
67    /// Set `next`.
68    pub fn set_next(&mut self, value: Option<NonZeroU64>) {
69        self.next = value.map(|v| v.get()).unwrap_or(0);
70    }
71
72    /// Returns true if this header has a valid signature.
73    pub fn is_valid(&self) -> bool {
74        self.signature == SIGNATURE
75    }
76}
77
78impl Default for Header {
79    fn default() -> Self {
80        Self {
81            signature: SIGNATURE,
82            ..Self::zeroed()
83        }
84    }
85}