-
Notifications
You must be signed in to change notification settings - Fork 4
/
enum_builder.rs
86 lines (75 loc) · 2.29 KB
/
enum_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
//! 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;
#[repr(i8)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Color {
Red = 0,
Green = 1,
Blue = 2,
}
impl Default for Color {
fn default() -> Self {
Color::Red
}
}
impl Scalar for Color {
fn to_le(self) -> Self {
#[cfg(target_endian = "little")]
{
self
}
#[cfg(not(target_endian = "little"))]
{
unsafe { transmute((self as i8).swap_bytes()) }
}
}
fn from_le(x: Self) -> Self {
#[cfg(target_endian = "little")]
{
x
}
#[cfg(not(target_endian = "little"))]
{
unsafe { transmute((x as i8).swap_bytes()) }
}
}
}
#[derive(Default, Clone, Debug, PartialEq)]
pub struct Bag {
pub color: Color,
}
impl Bag {
const VT_COLOR: usize = 4;
const SIZE_COLOR: usize = 1;
const ALIGNMENT_COLOR: usize = 1;
const ALIGNMENT: usize = 1;
}
impl<'c> Component<'c> for Bag {
fn build(self: Box<Self>, builder: &mut Builder<'c>) -> usize {
let vtable_start = {
let mut vtable = builder.start_vtable();
if self.color != Color::Red {
vtable.add_field(Self::VT_COLOR, Self::SIZE_COLOR, Self::ALIGNMENT_COLOR);
}
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.color != Color::Red {
builder.align(Self::ALIGNMENT_COLOR);
builder.push_scalar(self.color);
}
table_start
}
}
}