-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_struct_index_reader.rs
149 lines (131 loc) · 3.86 KB
/
dynamic_struct_index_reader.rs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use crate::{PointerType, Position, Read, StaticSize, Write, Writer};
use derive_deref::Deref;
use std::{convert::TryInto, marker::PhantomData, mem::size_of};
pub trait DynamicStructIndexType:
num::Integer
+ num::FromPrimitive
+ num::ToPrimitive
+ Into<PointerType>
+ Clone
+ Copy
+ for<'a> Read<'a, Output = Self>
+ Write<Output = Self>
{
}
impl DynamicStructIndexType for u8 {}
impl DynamicStructIndexType for u16 {}
#[derive(Clone, Copy, Debug)]
pub struct DynamicStructIndexReader<'a, I>
where
I: DynamicStructIndexType,
{
bytes: &'a [u8],
position: Position<Self>,
marker: PhantomData<I>,
}
impl<'a, I> DynamicStructIndexReader<'a, I>
where
I: DynamicStructIndexType,
{
pub fn new(bytes: &'a [u8], position: Position<Self>) -> DynamicStructIndexReader<'a, I> {
DynamicStructIndexReader {
bytes,
position,
marker: PhantomData,
}
}
pub fn field_count(&self) -> DynamicStructIndexFieldCount<I> {
let field_count_position = self.position.offset(0);
DynamicStructIndexFieldCount::read(self.bytes, field_count_position)
}
pub fn get_field_offset(
&self,
field_id: DynamicStructIndexFieldId<I>,
) -> DynamicStructIndexFieldOffset<I> {
let offset = size_of::<DynamicStructIndexFieldCount<I>>()
+ size_of::<DynamicStructIndexFieldOffset<I>>() * field_id.to_usize().unwrap();
let offset = offset.try_into().unwrap();
DynamicStructIndexFieldOffset::read(self.bytes, self.position.offset(offset))
}
}
/// A `DynamicStructIndexPointer` points from a struct to its index.
#[derive(Debug, Deref, Clone, Copy)]
pub struct DynamicStructIndexPointer(pub PointerType);
// A `DynamicStructIndexFieldCount` holds the number of fields in a struct's index.
#[derive(Debug, Deref, Clone, Copy)]
pub struct DynamicStructIndexFieldCount<I>(pub I)
where
I: DynamicStructIndexType;
/// A `FieldValueOffset` is the offset from the start of a struct to the value of a field.
#[derive(Debug, Deref, Clone, Copy, Hash, PartialEq, Eq)]
pub struct DynamicStructIndexFieldOffset<I>(pub I)
where
I: DynamicStructIndexType;
/// A `FieldId` is a unique identifier for each field.
#[derive(Debug, Deref, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct DynamicStructIndexFieldId<I>(pub I)
where
I: DynamicStructIndexType;
impl StaticSize for DynamicStructIndexPointer {
const STATIC_SIZE: PointerType = size_of::<PointerType>() as PointerType;
}
impl<'a> Read<'a> for DynamicStructIndexPointer {
type Output = Self;
fn read(bytes: &'a [u8], position: Position<Self>) -> Self {
DynamicStructIndexPointer(PointerType::read(bytes, position.cast()))
}
}
impl Write for DynamicStructIndexPointer {
type Output = bool;
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
writer.write(&self.0).cast()
}
}
impl<I> StaticSize for DynamicStructIndexFieldCount<I>
where
I: DynamicStructIndexType,
{
const STATIC_SIZE: PointerType = 2;
}
impl<'a, I> Read<'a> for DynamicStructIndexFieldCount<I>
where
I: DynamicStructIndexType,
{
type Output = Self;
fn read(bytes: &'a [u8], position: Position<Self>) -> Self {
DynamicStructIndexFieldCount(I::read(bytes, position.cast()))
}
}
impl<I> Write for DynamicStructIndexFieldCount<I>
where
I: DynamicStructIndexType,
{
type Output = Self;
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
writer.write(&self.0).cast()
}
}
impl<I> StaticSize for DynamicStructIndexFieldOffset<I>
where
I: DynamicStructIndexType,
{
const STATIC_SIZE: PointerType = 2;
}
impl<'a, I> Read<'a> for DynamicStructIndexFieldOffset<I>
where
I: DynamicStructIndexType,
{
type Output = Self;
fn read(bytes: &'a [u8], position: Position<Self>) -> Self::Output {
DynamicStructIndexFieldOffset(I::read(bytes, position.cast()))
}
}
impl<I> Write for DynamicStructIndexFieldOffset<I>
where
I: DynamicStructIndexType,
{
type Output = Self;
fn write(&self, writer: &mut Writer) -> Position<Self::Output> {
writer.write(&self.0).cast()
}
}