summaryrefslogtreecommitdiff
path: root/samples/sample_binary.rs
blob: 6fb987eb681b2ab311aa08cc7a9b713fa94ac2e0 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright 2018 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// import the flatbuffers runtime library
extern crate flatbuffers;

// import the generated code
#[path = "./monster_generated.rs"]
mod monster_generated;
pub use monster_generated::my_game::sample::{get_root_as_monster,
                                             Color, Equipment,
                                             Monster, MonsterArgs,
                                             Vec3,
                                             Weapon, WeaponArgs};


// Example how to use FlatBuffers to create and read binary buffers.

fn main() {
  // Build up a serialized buffer algorithmically.
  // Initialize it with a capacity of 1024 bytes.
  let mut builder = flatbuffers::FlatBufferBuilder::new_with_capacity(1024);

  // Serialize some weapons for the Monster: A 'sword' and an 'axe'.
  let weapon_one_name = builder.create_string("Sword");
  let weapon_two_name = builder.create_string("Axe");

  // Use the `Weapon::create` shortcut to create Weapons with named field
  // arguments.
  let sword = Weapon::create(&mut builder, &WeaponArgs{
      name: Some(weapon_one_name),
      damage: 3,
  });
  let axe = Weapon::create(&mut builder, &WeaponArgs{
      name: Some(weapon_two_name),
      damage: 5,
  });

  // Name of the Monster.
  let name = builder.create_string("Orc");

  // Inventory.
  let inventory = builder.create_vector(&[0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9]);

  // Create a FlatBuffer `vector` that contains offsets to the sword and axe
  // we created above.
  let weapons = builder.create_vector(&[sword, axe]);

  // Create the path vector of Vec3 objects:
  //let x = Vec3::new(1.0, 2.0, 3.0);
  //let y = Vec3::new(4.0, 5.0, 6.0);
  //let path = builder.create_vector(&[x, y]);

  // Note that, for convenience, it is also valid to create a vector of
  // references to structs, like this:
  // let path = builder.create_vector(&[&x, &y]);

  // Create the monster using the `Monster::create` helper function. This
  // function accepts a `MonsterArgs` struct, which supplies all of the data
  // needed to build a `Monster`. To supply empty/default fields, just use the
  // Rust built-in `Default::default()` function, as demononstrated below.
  let orc = Monster::create(&mut builder, &MonsterArgs{
      pos: Some(&Vec3::new(1.0f32, 2.0f32, 3.0f32)),
      mana: 150,
      hp: 80,
      name: Some(name),
      inventory: Some(inventory),
      color: Color::Red,
      weapons: Some(weapons),
      equipped_type: Equipment::Weapon,
      equipped: Some(axe.as_union_value()),
      //path: Some(path),
      ..Default::default()
  });

  // Serialize the root of the object, without providing a file identifier.
  builder.finish(orc, None);

  // We now have a FlatBuffer we can store on disk or send over a network.

  // ** file/network code goes here :) **

  // Instead, we're going to access it right away (as if we just received it).
  // This must be called after `finish()`.
  let buf = builder.finished_data(); // Of type `&[u8]`

  // Get access to the root:
  let monster = get_root_as_monster(buf);

  // Get and test some scalar types from the FlatBuffer.
  let hp = monster.hp();
  let mana = monster.mana();
  let name = monster.name();

  assert_eq!(hp, 80);
  assert_eq!(mana, 150);  // default
  assert_eq!(name, Some("Orc"));

  // Get and test a field of the FlatBuffer's `struct`.
  assert!(monster.pos().is_some());
  let pos = monster.pos().unwrap();
  let x = pos.x();
  let y = pos.y();
  let z = pos.z();
  assert_eq!(x, 1.0f32);
  assert_eq!(y, 2.0f32);
  assert_eq!(z, 3.0f32);

  // Get an element from the `inventory` FlatBuffer's `vector`.
  assert!(monster.inventory().is_some());
  let inv = monster.inventory().unwrap();

  // Note that this vector is returned as a slice, because direct access for
  // this type, a u8 vector, is safe on all platforms:
  let third_item = inv[2];
  assert_eq!(third_item, 2);

  // Get and test the `weapons` FlatBuffers's `vector`.
  assert!(monster.weapons().is_some());
  let weps = monster.weapons().unwrap();
  //let weps_len = weps.len();
  let wep2 = weps.get(1);
  let second_weapon_name = wep2.name();
  let second_weapon_damage = wep2.damage();
  assert_eq!(second_weapon_name, Some("Axe"));
  assert_eq!(second_weapon_damage, 5);

  // Get and test the `Equipment` union (`equipped` field).
  assert_eq!(monster.equipped_type(), Equipment::Weapon);
  let equipped = monster.equipped_as_weapon().unwrap();
  let weapon_name = equipped.name();
  let weapon_damage = equipped.damage();
  assert_eq!(weapon_name, Some("Axe"));
  assert_eq!(weapon_damage, 5);

  // Get and test the `path` FlatBuffers's `vector`.
  //assert_eq!(monster.path().unwrap().len(), 2);
  //assert_eq!(monster.path().unwrap()[0].x(), 1.0);
  //assert_eq!(monster.path().unwrap()[1].x(), 4.0);

  println!("The FlatBuffer was successfully created and accessed!");
  dbg!(monster);
}

#[cfg(test)]
#[test]
fn test_main() {
    main()
}