-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage.rs
65 lines (54 loc) · 1.66 KB
/
usage.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
use std::rc::Rc;
use flatbuffers::FlatBufferBuilder;
use lean_buffer::{
macros::LeanBufferWrite,
macros::LeanBufferRaw,
traits::{AdapterExt, Factory, FactoryExt},
};
// Don't panic when you see this false positive warning:
// proc macro `LeanBufferWrite` not expanded: proc macro not found in the built dylib
// Just check if the generated file can be located.
#[derive(LeanBufferWrite, LeanBufferRaw)]
struct Entity {
t_u64: u64,
t_i64: i64,
t_u32: u32,
t_i32: i32,
t_char: char,
t_u16: u16,
t_i16: i16,
t_u8: u8,
t_i8: i8,
t_bool: bool,
t_string: String,
t_double: f64,
t_float: f32,
}
// Either copy this file from your project, or use the name convention
// `<struct name>_lb_gen.rs` to include the generated file.
include!(concat!(env!("OUT_DIR"), "/Entity_lb_gen.rs"));
fn main() {
let mut builder = FlatBufferBuilder::new();
let factory = Factory::<Entity> {
phantom_data: std::marker::PhantomData,
};
let f = Rc::new(factory) as Rc<dyn FactoryExt<Entity>>;
let mut e1 = f.new_object();
e1.t_i64 = 0x1337833F;
let e1_t_i64 = e1.t_i64;
let a1 = Box::new(e1) as Box<dyn AdapterExt>;
// flatten
a1.flatten(&mut builder);
let data = builder.finished_data();
// inflate
let first_offset: usize = data[0].into();
unsafe {
let mut table = flatbuffers::Table::new(data, first_offset);
let resurrected_e1 = f.inflate(&mut table);
if resurrected_e1.t_i64 == e1_t_i64 {
println!("Hello world! {}", resurrected_e1.t_i64);
} else {
println!("Goodbye cruel world! {}", resurrected_e1.t_i64);
}
}
}