flatbuffers_rust/flatbuffers/
flatbuffer.rs1use std::cell::RefCell;
2use std::rc::Rc;
3static SEPARATOR: u8 = 0xff;
16
17#[derive(Debug, Clone)]
18pub struct FlatBuffer {
20 pub vtable: Option<Rc<RefCell<Vec<u32>>>>, pivot: Option<u8>, pub data: Option<Rc<RefCell<Vec<u8>>>> }
24
25impl FlatBuffer {
26 pub fn new() -> FlatBuffer {
27 FlatBuffer {
28 vtable: None,
29 pivot: None,
30 data: None
31 }
32 }
33 pub fn with_pivot(num: u8) -> FlatBuffer {
34 let mut vtable = Vec::with_capacity(num as usize);
35 for _i in 0..num {
36 vtable.push(0u32);
37 }
38 FlatBuffer {
39 vtable: Some(Rc::new(RefCell::new(vtable))),
40 pivot: Some(num),
41 data: None,
42 }
43 }
44 pub fn with_primitive_type(data:&mut Vec<u8>) -> FlatBuffer {
45 let len = data.len();
47 let mut flatdata = Vec::with_capacity(len + 1);
48 flatdata.push(SEPARATOR);
49 for i in 0..len {
50 flatdata.push(data[i]);
51 }
52 FlatBuffer {
53 vtable: None,
54 pivot: None,
55 data: Some(Rc::new(RefCell::new(flatdata))),
56 }
57 }
58 pub fn bytes(&mut self) -> (Option<Vec<u8>>, usize) {
60 let mut bytes = Vec::new();
62 let mut root = 0; match self.pivot {
65 None => (),
66 Some(pivot) => { bytes.push(SEPARATOR); root = 1;},
67 };
68 match self.vtable_to_bytes() {
70 None => (),
71 Some(mut vec) => bytes.append(&mut vec),
72 };
73 match self.pivot {
75 None => (),
76 Some(pivot) => { bytes.push(pivot); root = bytes.len() - 1;},
77 };
78 match self.data {
80 None => (),
81 Some(ref mut data) => bytes.append(&mut data.borrow_mut().clone()),
83 };
84 if bytes.len() == 0 {
85 (None, root)
86 } else {
87 (Some(bytes), root)
88 }
89 }
90 fn vtable_to_bytes(&mut self) -> Option<Vec<u8>> {
95 let vtable = self.vtable.clone();
96 let table = match vtable {
97 None => return None,
98 Some(table) => table,
99 };
100 let mut bytes = Vec::new();
101 let slots_num = table.borrow().len();
102 for i in 0..slots_num {
103 let offset = table.borrow_mut()[i];
104 let b0 : u8 = ((offset >> 24) & 0xff) as u8;
105 let b1 : u8 = ((offset >> 16) & 0xff) as u8;
106 let b2 : u8 = ((offset >> 8) & 0xff) as u8;
107 let b3 : u8 = (offset & 0xff) as u8;
108 bytes.push(b3);
109 bytes.push(b2);
110 bytes.push(b1);
111 bytes.push(b0);
112 }
113 Some(bytes)
114 }
115}