#![allow(clippy::upper_case_acronyms)]
#[cfg(feature = "bytemuck")]
use bytemuck_derive::{Pod, Zeroable};
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
#[repr(C)]
pub struct ArrayHeader {
pub count: u32,
pub base_offset: u32,
}
impl From<(u32, u32)> for ArrayHeader {
fn from((count, base_offset): (u32, u32)) -> Self {
Self { count, base_offset }
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
#[repr(C)]
pub struct FDBHeader {
pub tables: ArrayHeader,
}
impl FDBHeader {
#[inline]
pub const fn table_headers_byte_count(&self) -> usize {
self.tables.count as usize * std::mem::size_of::<FDBTableHeader>()
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
#[repr(C)]
pub struct FDBTableHeader {
pub table_def_header_addr: u32,
pub table_data_header_addr: u32,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
#[repr(C)]
pub struct FDBTableDefHeader {
pub column_count: u32,
pub table_name_addr: u32,
pub column_header_list_addr: u32,
}
impl From<(u32, u32, u32)> for FDBTableDefHeader {
fn from((column_count, table_name_addr, column_header_list_addr): (u32, u32, u32)) -> Self {
Self {
column_count,
table_name_addr,
column_header_list_addr,
}
}
}
impl From<[u32; 3]> for FDBTableDefHeader {
fn from([column_count, table_name_addr, column_header_list_addr]: [u32; 3]) -> Self {
Self {
column_count,
table_name_addr,
column_header_list_addr,
}
}
}
impl FDBTableDefHeader {
#[inline]
pub const fn column_header_list_byte_count(&self) -> usize {
self.column_count as usize * std::mem::size_of::<FDBColumnHeader>()
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
#[repr(C)]
pub struct FDBColumnHeader {
pub column_data_type: u32,
pub column_name_addr: u32,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
#[repr(C)]
pub struct FDBTableDataHeader {
pub buckets: ArrayHeader,
}
impl FDBTableDataHeader {
#[inline]
pub const fn bucket_header_list_byte_count(&self) -> usize {
self.buckets.count as usize * std::mem::size_of::<FDBBucketHeader>()
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
#[repr(C)]
pub struct FDBBucketHeader {
pub row_header_list_head_addr: u32,
}
impl From<u32> for FDBBucketHeader {
fn from(row_header_list_head_addr: u32) -> Self {
Self {
row_header_list_head_addr,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
#[repr(C)]
pub struct FDBRowHeaderListEntry {
pub row_header_addr: u32,
pub row_header_list_next_addr: u32,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
#[repr(C)]
pub struct FDBRowHeader {
pub fields: ArrayHeader,
}
impl FDBRowHeader {
#[inline]
pub const fn field_data_list_byte_count(&self) -> usize {
self.fields.count as usize * std::mem::size_of::<FDBFieldData>()
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "bytemuck", derive(Pod, Zeroable))]
#[repr(C)]
pub struct FDBFieldData {
pub data_type: u32,
pub value: [u8; 4],
}
#[cfg(test)]
mod tests {
use super::*;
use std::mem;
#[test]
fn test_align() {
assert_eq!(mem::align_of::<FDBHeader>(), 4);
assert_eq!(mem::align_of::<FDBTableHeader>(), 4);
assert_eq!(mem::align_of::<FDBTableDefHeader>(), 4);
assert_eq!(mem::align_of::<FDBColumnHeader>(), 4);
assert_eq!(mem::align_of::<FDBTableDataHeader>(), 4);
assert_eq!(mem::align_of::<FDBBucketHeader>(), 4);
assert_eq!(mem::align_of::<FDBRowHeaderListEntry>(), 4);
assert_eq!(mem::align_of::<FDBRowHeader>(), 4);
assert_eq!(mem::align_of::<FDBFieldData>(), 4);
}
#[test]
fn test_size_of() {
assert_eq!(mem::size_of::<FDBHeader>(), 8);
assert_eq!(mem::size_of::<FDBTableHeader>(), 8);
assert_eq!(mem::size_of::<FDBTableDefHeader>(), 12);
assert_eq!(mem::size_of::<FDBColumnHeader>(), 8);
assert_eq!(mem::size_of::<FDBTableDataHeader>(), 8);
assert_eq!(mem::size_of::<FDBBucketHeader>(), 4);
assert_eq!(mem::size_of::<FDBRowHeaderListEntry>(), 8);
assert_eq!(mem::size_of::<FDBRowHeader>(), 8);
assert_eq!(mem::size_of::<FDBFieldData>(), 8);
}
}