-
Notifications
You must be signed in to change notification settings - Fork 4
/
table_field_builder.rs
88 lines (75 loc) · 2.68 KB
/
table_field_builder.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
//! This file is auto-generated by cfbc.
pub mod example {
#![allow(unused_imports, clippy::large_enum_variant)]
use cfb::builder::{
Builder, Component, DesignatedComponent, NestedBufferComponent, ReferenceVectorComponent,
ScalarVectorComponent, StringComponent,
};
use cfb::scalar::Scalar;
use cfb::types::{SOffset, SIZE_OF_SOFFSET};
#[cfg(not(target_endian = "little"))]
use std::mem::transmute;
#[derive(Default, Clone, Debug, PartialEq)]
pub struct Hero {
pub stat: Option<Stat>,
}
impl Hero {
const VT_STAT: usize = 4;
const SIZE_STAT: usize = 4;
const ALIGNMENT_STAT: usize = 4;
const ALIGNMENT: usize = 4;
}
impl<'c> Component<'c> for Hero {
fn build(self: Box<Self>, builder: &mut Builder<'c>) -> usize {
let vtable_start = {
let mut vtable = builder.start_vtable();
if self.stat.is_some() {
vtable.add_field(Self::VT_STAT, Self::SIZE_STAT, Self::ALIGNMENT_STAT);
}
vtable.finish()
};
builder.align_after(SIZE_OF_SOFFSET, Self::ALIGNMENT);
let table_start = builder.tell();
builder.push_scalar((table_start - vtable_start) as SOffset);
if let Some(f) = self.stat {
builder.align(Self::ALIGNMENT_STAT);
let offset_position = builder.tell();
builder.pad(Self::SIZE_STAT);
builder.push_component(DesignatedComponent::new(
offset_position,
Box::new(f),
));
}
table_start
}
}
#[derive(Default, Clone, Debug, PartialEq)]
pub struct Stat {
pub hp: u32,
}
impl Stat {
const VT_HP: usize = 4;
const SIZE_HP: usize = 4;
const ALIGNMENT_HP: usize = 4;
const ALIGNMENT: usize = 4;
}
impl<'c> Component<'c> for Stat {
fn build(self: Box<Self>, builder: &mut Builder<'c>) -> usize {
let vtable_start = {
let mut vtable = builder.start_vtable();
if self.hp != 0u32 {
vtable.add_field(Self::VT_HP, Self::SIZE_HP, Self::ALIGNMENT_HP);
}
vtable.finish()
};
builder.align_after(SIZE_OF_SOFFSET, Self::ALIGNMENT);
let table_start = builder.tell();
builder.push_scalar((table_start - vtable_start) as SOffset);
if self.hp != 0u32 {
builder.align(Self::ALIGNMENT_HP);
builder.push_scalar(self.hp);
}
table_start
}
}
}