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
use alloc::alloc::Layout;
use core::convert::TryInto;
use core::fmt;
use core::marker::PhantomData;
pub struct PageHeader<H> {
pub header: H,
pub desc: PageDesc,
}
impl<H> fmt::Debug for PageHeader<H> {
#[inline(always)]
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "PageHeader[{}]", self.desc.items)
}
}
#[derive(Clone,Copy)]
pub struct PageDesc {
pub items: u32,
pub data: u32,
}
impl fmt::Debug for PageDesc {
#[inline(always)]
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "PageDesc[{}]", self.items)
}
}
impl<H, T> From<PageLayout<H, T>> for PageDesc {
#[inline(always)]
fn from(layout: PageLayout<H, T>) -> Self { layout.desc }
}
pub struct PageLayout<H, T> {
pub(crate) desc: PageDesc,
layout: Layout,
_phantom: PhantomData<(H, T)>,
}
impl<H, T> PageLayout<H, T> {
#[inline(always)]
pub fn with_capacity(items: u32) -> Self {
assert!(items > 0);
let header = Layout::new::<PageHeader<H>>();
let array = Layout::array::<T>(items as usize).unwrap();
let (layout, data) = header.extend(array).unwrap();
let layout = layout.pad_to_align();
let desc = PageDesc { items, data: data.try_into().unwrap() };
Self { desc, layout, _phantom: PhantomData }
}
#[inline(always)]
pub fn layout(self) -> Layout { self.layout }
}
impl<H, T> Clone for PageLayout<H, T> {
#[inline(always)]
fn clone(&self) -> Self {
PageLayout { desc: self.desc, layout: self.layout, _phantom: self._phantom }
}
}
impl<H, T> Copy for PageLayout<H, T> {}