Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // import the flatbuffers runtime library |
| 18 | extern crate flatbuffers; |
| 19 | |
| 20 | // import the generated code |
| 21 | #[path = "./monster_generated.rs"] |
| 22 | mod monster_generated; |
| 23 | pub use monster_generated::my_game::sample::{get_root_as_monster, |
| 24 | Color, Equipment, |
| 25 | Monster, MonsterArgs, |
| 26 | Vec3, |
| 27 | Weapon, WeaponArgs}; |
| 28 | |
| 29 | |
| 30 | // Example how to use FlatBuffers to create and read binary buffers. |
| 31 | |
| 32 | fn main() { |
| 33 | // Build up a serialized buffer algorithmically. |
| 34 | // Initialize it with a capacity of 1024 bytes. |
| 35 | let mut builder = flatbuffers::FlatBufferBuilder::new_with_capacity(1024); |
| 36 | |
| 37 | // Serialize some weapons for the Monster: A 'sword' and an 'axe'. |
| 38 | let weapon_one_name = builder.create_string("Sword"); |
| 39 | let weapon_two_name = builder.create_string("Axe"); |
| 40 | |
| 41 | // Use the `Weapon::create` shortcut to create Weapons with named field |
| 42 | // arguments. |
| 43 | let sword = Weapon::create(&mut builder, &WeaponArgs{ |
| 44 | name: Some(weapon_one_name), |
| 45 | damage: 3, |
| 46 | }); |
| 47 | let axe = Weapon::create(&mut builder, &WeaponArgs{ |
| 48 | name: Some(weapon_two_name), |
| 49 | damage: 5, |
| 50 | }); |
| 51 | |
| 52 | // Name of the Monster. |
| 53 | let name = builder.create_string("Orc"); |
| 54 | |
| 55 | // Inventory. |
| 56 | let inventory = builder.create_vector(&[0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
| 57 | |
| 58 | // Create a FlatBuffer `vector` that contains offsets to the sword and axe |
| 59 | // we created above. |
| 60 | let weapons = builder.create_vector(&[sword, axe]); |
| 61 | |
| 62 | // Create the path vector of Vec3 objects: |
| 63 | //let x = Vec3::new(1.0, 2.0, 3.0); |
| 64 | //let y = Vec3::new(4.0, 5.0, 6.0); |
| 65 | //let path = builder.create_vector(&[x, y]); |
| 66 | |
| 67 | // Note that, for convenience, it is also valid to create a vector of |
| 68 | // references to structs, like this: |
| 69 | // let path = builder.create_vector(&[&x, &y]); |
| 70 | |
| 71 | // Create the monster using the `Monster::create` helper function. This |
| 72 | // function accepts a `MonsterArgs` struct, which supplies all of the data |
| 73 | // needed to build a `Monster`. To supply empty/default fields, just use the |
| 74 | // Rust built-in `Default::default()` function, as demononstrated below. |
| 75 | let orc = Monster::create(&mut builder, &MonsterArgs{ |
| 76 | pos: Some(&Vec3::new(1.0f32, 2.0f32, 3.0f32)), |
| 77 | mana: 150, |
| 78 | hp: 80, |
| 79 | name: Some(name), |
| 80 | inventory: Some(inventory), |
| 81 | color: Color::Red, |
| 82 | weapons: Some(weapons), |
| 83 | equipped_type: Equipment::Weapon, |
| 84 | equipped: Some(axe.as_union_value()), |
| 85 | //path: Some(path), |
| 86 | ..Default::default() |
| 87 | }); |
| 88 | |
| 89 | // Serialize the root of the object, without providing a file identifier. |
| 90 | builder.finish(orc, None); |
| 91 | |
| 92 | // We now have a FlatBuffer we can store on disk or send over a network. |
| 93 | |
| 94 | // ** file/network code goes here :) ** |
| 95 | |
| 96 | // Instead, we're going to access it right away (as if we just received it). |
| 97 | // This must be called after `finish()`. |
| 98 | let buf = builder.finished_data(); // Of type `&[u8]` |
| 99 | |
| 100 | // Get access to the root: |
| 101 | let monster = get_root_as_monster(buf); |
| 102 | |
| 103 | // Get and test some scalar types from the FlatBuffer. |
| 104 | let hp = monster.hp(); |
| 105 | let mana = monster.mana(); |
| 106 | let name = monster.name(); |
| 107 | |
| 108 | assert_eq!(hp, 80); |
| 109 | assert_eq!(mana, 150); // default |
| 110 | assert_eq!(name, Some("Orc")); |
| 111 | |
| 112 | // Get and test a field of the FlatBuffer's `struct`. |
| 113 | assert!(monster.pos().is_some()); |
| 114 | let pos = monster.pos().unwrap(); |
| 115 | let x = pos.x(); |
| 116 | let y = pos.y(); |
| 117 | let z = pos.z(); |
| 118 | assert_eq!(x, 1.0f32); |
| 119 | assert_eq!(y, 2.0f32); |
| 120 | assert_eq!(z, 3.0f32); |
| 121 | |
| 122 | // Get an element from the `inventory` FlatBuffer's `vector`. |
| 123 | assert!(monster.inventory().is_some()); |
| 124 | let inv = monster.inventory().unwrap(); |
| 125 | |
| 126 | // Note that this vector is returned as a slice, because direct access for |
| 127 | // this type, a u8 vector, is safe on all platforms: |
| 128 | let third_item = inv[2]; |
| 129 | assert_eq!(third_item, 2); |
| 130 | |
| 131 | // Get and test the `weapons` FlatBuffers's `vector`. |
| 132 | assert!(monster.weapons().is_some()); |
| 133 | let weps = monster.weapons().unwrap(); |
| 134 | //let weps_len = weps.len(); |
| 135 | let wep2 = weps.get(1); |
| 136 | let second_weapon_name = wep2.name(); |
| 137 | let second_weapon_damage = wep2.damage(); |
| 138 | assert_eq!(second_weapon_name, Some("Axe")); |
| 139 | assert_eq!(second_weapon_damage, 5); |
| 140 | |
| 141 | // Get and test the `Equipment` union (`equipped` field). |
| 142 | assert_eq!(monster.equipped_type(), Equipment::Weapon); |
| 143 | let equipped = monster.equipped_as_weapon().unwrap(); |
| 144 | let weapon_name = equipped.name(); |
| 145 | let weapon_damage = equipped.damage(); |
| 146 | assert_eq!(weapon_name, Some("Axe")); |
| 147 | assert_eq!(weapon_damage, 5); |
| 148 | |
| 149 | // Get and test the `path` FlatBuffers's `vector`. |
| 150 | //assert_eq!(monster.path().unwrap().len(), 2); |
| 151 | //assert_eq!(monster.path().unwrap()[0].x(), 1.0); |
| 152 | //assert_eq!(monster.path().unwrap()[1].x(), 4.0); |
| 153 | |
| 154 | println!("The FlatBuffer was successfully created and accessed!"); |
| 155 | } |