Skip to content

Commit 4ee7bcb

Browse files
committed
Update rust generator for no_std
Also re-run generator on sample.
1 parent 53c4d1d commit 4ee7bcb

File tree

2 files changed

+63
-38
lines changed

2 files changed

+63
-38
lines changed

samples/monster_generated.rs

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
// automatically generated by the FlatBuffers compiler, do not modify
22

33

4+
5+
extern crate core;
6+
7+
use core::mem;
8+
use core::cmp::Ordering;
9+
10+
extern crate flatbuffers;
11+
use self::flatbuffers::EndianScalar;
12+
13+
#[allow(unused_imports, dead_code)]
414
pub mod my_game {
5-
#![allow(dead_code)]
6-
#![allow(unused_imports)]
715

8-
use std::mem;
9-
use std::marker::PhantomData;
10-
use std::cmp::Ordering;
16+
extern crate core;
17+
18+
use core::mem;
19+
use core::cmp::Ordering;
1120

1221
extern crate flatbuffers;
1322
use self::flatbuffers::EndianScalar;
23+
#[allow(unused_imports, dead_code)]
1424
pub mod sample {
15-
#![allow(dead_code)]
16-
#![allow(unused_imports)]
1725

18-
use std::mem;
19-
use std::marker::PhantomData;
20-
use std::cmp::Ordering;
26+
extern crate core;
27+
28+
use core::mem;
29+
use core::cmp::Ordering;
2130

2231
extern crate flatbuffers;
2332
use self::flatbuffers::EndianScalar;
2433

2534
#[allow(non_camel_case_types)]
2635
#[repr(i8)]
27-
#[derive(Clone, Copy, PartialEq, Debug)]
36+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
2837
pub enum Color {
2938
Red = 0,
3039
Green = 1,
31-
Blue = 2
40+
Blue = 2,
41+
3242
}
3343

3444
const ENUM_MIN_COLOR: i8 = 0;
@@ -80,16 +90,17 @@ const ENUM_NAMES_COLOR:[&'static str; 3] = [
8090
];
8191

8292
pub fn enum_name_color(e: Color) -> &'static str {
83-
let index: usize = e as usize;
84-
ENUM_NAMES_COLOR[index]
93+
let index = e as i8;
94+
ENUM_NAMES_COLOR[index as usize]
8595
}
8696

8797
#[allow(non_camel_case_types)]
8898
#[repr(u8)]
89-
#[derive(Clone, Copy, PartialEq, Debug)]
99+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
90100
pub enum Equipment {
91101
NONE = 0,
92-
Weapon = 1
102+
Weapon = 1,
103+
93104
}
94105

95106
const ENUM_MIN_EQUIPMENT: u8 = 0;
@@ -139,8 +150,8 @@ const ENUM_NAMES_EQUIPMENT:[&'static str; 2] = [
139150
];
140151

141152
pub fn enum_name_equipment(e: Equipment) -> &'static str {
142-
let index: usize = e as usize;
143-
ENUM_NAMES_EQUIPMENT[index]
153+
let index = e as u8;
154+
ENUM_NAMES_EQUIPMENT[index as usize]
144155
}
145156

146157
pub struct EquipmentUnionTableOffset {}
@@ -158,7 +169,6 @@ impl<'a> flatbuffers::Follow<'a> for Vec3 {
158169
#[inline]
159170
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
160171
<&'a Vec3>::follow(buf, loc)
161-
//flatbuffers::follow_cast_ref::<Vec3>(buf, loc)
162172
}
163173
}
164174
impl<'a> flatbuffers::Follow<'a> for &'a Vec3 {
@@ -173,7 +183,7 @@ impl<'b> flatbuffers::Push for Vec3 {
173183
#[inline]
174184
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
175185
let src = unsafe {
176-
::std::slice::from_raw_parts(self as *const Vec3 as *const u8, Self::size())
186+
::core::slice::from_raw_parts(self as *const Vec3 as *const u8, Self::size())
177187
};
178188
dst.copy_from_slice(src);
179189
}
@@ -184,7 +194,7 @@ impl<'b> flatbuffers::Push for &'b Vec3 {
184194
#[inline]
185195
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
186196
let src = unsafe {
187-
::std::slice::from_raw_parts(*self as *const Vec3 as *const u8, Self::size())
197+
::core::slice::from_raw_parts(*self as *const Vec3 as *const u8, Self::size())
188198
};
189199
dst.copy_from_slice(src);
190200
}
@@ -240,6 +250,7 @@ impl<'a> Monster<'a> {
240250
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
241251
args: &'args MonsterArgs<'args>) -> flatbuffers::WIPOffset<Monster<'bldr>> {
242252
let mut builder = MonsterBuilder::new(_fbb);
253+
if let Some(x) = args.path { builder.add_path(x); }
243254
if let Some(x) = args.equipped { builder.add_equipped(x); }
244255
if let Some(x) = args.weapons { builder.add_weapons(x); }
245256
if let Some(x) = args.inventory { builder.add_inventory(x); }
@@ -261,46 +272,51 @@ impl<'a> Monster<'a> {
261272
pub const VT_WEAPONS: flatbuffers::VOffsetT = 18;
262273
pub const VT_EQUIPPED_TYPE: flatbuffers::VOffsetT = 20;
263274
pub const VT_EQUIPPED: flatbuffers::VOffsetT = 22;
275+
pub const VT_PATH: flatbuffers::VOffsetT = 24;
264276

265277
#[inline]
266-
pub fn pos(&'a self) -> Option<&'a Vec3> {
278+
pub fn pos(&self) -> Option<&'a Vec3> {
267279
self._tab.get::<Vec3>(Monster::VT_POS, None)
268280
}
269281
#[inline]
270-
pub fn mana(&'a self) -> i16 {
282+
pub fn mana(&self) -> i16 {
271283
self._tab.get::<i16>(Monster::VT_MANA, Some(150)).unwrap()
272284
}
273285
#[inline]
274-
pub fn hp(&'a self) -> i16 {
286+
pub fn hp(&self) -> i16 {
275287
self._tab.get::<i16>(Monster::VT_HP, Some(100)).unwrap()
276288
}
277289
#[inline]
278-
pub fn name(&'a self) -> Option<&'a str> {
290+
pub fn name(&self) -> Option<&'a str> {
279291
self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(Monster::VT_NAME, None)
280292
}
281293
#[inline]
282-
pub fn inventory(&'a self) -> Option<&'a [u8]> {
294+
pub fn inventory(&self) -> Option<&'a [u8]> {
283295
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'a, u8>>>(Monster::VT_INVENTORY, None).map(|v| v.safe_slice())
284296
}
285297
#[inline]
286-
pub fn color(&'a self) -> Color {
298+
pub fn color(&self) -> Color {
287299
self._tab.get::<Color>(Monster::VT_COLOR, Some(Color::Blue)).unwrap()
288300
}
289301
#[inline]
290-
pub fn weapons(&'a self) -> Option<flatbuffers::Vector<flatbuffers::ForwardsUOffset<Weapon<'a>>>> {
302+
pub fn weapons(&self) -> Option<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<Weapon<'a>>>> {
291303
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<flatbuffers::ForwardsUOffset<Weapon<'a>>>>>(Monster::VT_WEAPONS, None)
292304
}
293305
#[inline]
294-
pub fn equipped_type(&'a self) -> Equipment {
306+
pub fn equipped_type(&self) -> Equipment {
295307
self._tab.get::<Equipment>(Monster::VT_EQUIPPED_TYPE, Some(Equipment::NONE)).unwrap()
296308
}
297309
#[inline]
298-
pub fn equipped(&'a self) -> Option<flatbuffers::Table<'a>> {
310+
pub fn equipped(&self) -> Option<flatbuffers::Table<'a>> {
299311
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Table<'a>>>(Monster::VT_EQUIPPED, None)
300312
}
301313
#[inline]
314+
pub fn path(&self) -> Option<&'a [Vec3]> {
315+
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<Vec3>>>(Monster::VT_PATH, None).map(|v| v.safe_slice() )
316+
}
317+
#[inline]
302318
#[allow(non_snake_case)]
303-
pub fn equipped_as_weapon(&'a self) -> Option<Weapon> {
319+
pub fn equipped_as_weapon(&self) -> Option<Weapon<'a>> {
304320
if self.equipped_type() == Equipment::Weapon {
305321
self.equipped().map(|u| Weapon::init_from_table(u))
306322
} else {
@@ -320,6 +336,7 @@ pub struct MonsterArgs<'a> {
320336
pub weapons: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , flatbuffers::ForwardsUOffset<Weapon<'a >>>>>,
321337
pub equipped_type: Equipment,
322338
pub equipped: Option<flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>>,
339+
pub path: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , Vec3>>>,
323340
}
324341
impl<'a> Default for MonsterArgs<'a> {
325342
#[inline]
@@ -334,6 +351,7 @@ impl<'a> Default for MonsterArgs<'a> {
334351
weapons: None,
335352
equipped_type: Equipment::NONE,
336353
equipped: None,
354+
path: None,
337355
}
338356
}
339357
}
@@ -379,6 +397,10 @@ impl<'a: 'b, 'b> MonsterBuilder<'a, 'b> {
379397
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_EQUIPPED, equipped);
380398
}
381399
#[inline]
400+
pub fn add_path(&mut self, path: flatbuffers::WIPOffset<flatbuffers::Vector<'b , Vec3>>) {
401+
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Monster::VT_PATH, path);
402+
}
403+
#[inline]
382404
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> MonsterBuilder<'a, 'b> {
383405
let start = _fbb.start_table();
384406
MonsterBuilder {
@@ -431,11 +453,11 @@ impl<'a> Weapon<'a> {
431453
pub const VT_DAMAGE: flatbuffers::VOffsetT = 6;
432454

433455
#[inline]
434-
pub fn name(&'a self) -> Option<&'a str> {
456+
pub fn name(&self) -> Option<&'a str> {
435457
self._tab.get::<flatbuffers::ForwardsUOffset<&str>>(Weapon::VT_NAME, None)
436458
}
437459
#[inline]
438-
pub fn damage(&'a self) -> i16 {
460+
pub fn damage(&self) -> i16 {
439461
self._tab.get::<i16>(Weapon::VT_DAMAGE, Some(0)).unwrap()
440462
}
441463
}

src/idl_gen_rust.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ class RustGenerator : public BaseGenerator {
256256
"yield",
257257

258258
// other rust terms we should not use
259+
"core",
259260
"std",
260261
"usize",
261262
"isize",
@@ -1477,7 +1478,7 @@ class RustGenerator : public BaseGenerator {
14771478
code_ += "";
14781479
code_ += " #[inline]";
14791480
code_ += " pub fn key_compare_with_value(&self, val: {{KEY_TYPE}}) -> "
1480-
" ::std::cmp::Ordering {";
1481+
" ::core::cmp::Ordering {";
14811482
code_ += " let key = self.{{FIELD_NAME}}();";
14821483
code_ += " key.cmp(&val)";
14831484
code_ += " }";
@@ -1648,7 +1649,7 @@ class RustGenerator : public BaseGenerator {
16481649
code_ += " #[inline]";
16491650
code_ += " fn push(&self, dst: &mut [u8], _rest: &[u8]) {";
16501651
code_ += " let src = unsafe {";
1651-
code_ += " ::std::slice::from_raw_parts("
1652+
code_ += " ::core::slice::from_raw_parts("
16521653
"self as *const {{STRUCT_NAME}} as *const u8, Self::size())";
16531654
code_ += " };";
16541655
code_ += " dst.copy_from_slice(src);";
@@ -1660,7 +1661,7 @@ class RustGenerator : public BaseGenerator {
16601661
code_ += " #[inline]";
16611662
code_ += " fn push(&self, dst: &mut [u8], _rest: &[u8]) {";
16621663
code_ += " let src = unsafe {";
1663-
code_ += " ::std::slice::from_raw_parts("
1664+
code_ += " ::core::slice::from_raw_parts("
16641665
"*self as *const {{STRUCT_NAME}} as *const u8, Self::size())";
16651666
code_ += " };";
16661667
code_ += " dst.copy_from_slice(src);";
@@ -1746,8 +1747,10 @@ class RustGenerator : public BaseGenerator {
17461747
void GenNamespaceImports(const int white_spaces) {
17471748
std::string indent = std::string(white_spaces, ' ');
17481749
code_ += "";
1749-
code_ += indent + "use std::mem;";
1750-
code_ += indent + "use std::cmp::Ordering;";
1750+
code_ += indent + "extern crate core;";
1751+
code_ += "";
1752+
code_ += indent + "use core::mem;";
1753+
code_ += indent + "use core::cmp::Ordering;";
17511754
code_ += "";
17521755
code_ += indent + "extern crate flatbuffers;";
17531756
code_ += indent + "use self::flatbuffers::EndianScalar;";

0 commit comments

Comments
 (0)