Rework enums in rust. (#6098)

* Rework enums in rust.

They're now a unit struct, rather than an enum. This is a
backwards incompatible change but the previous version had UB
and was also backwards incompatible so...

* Update and test sample rust flatbuffers

* Use bitflags crate to properly support rust enums.

Previously, the bitflags attribute was just ignored. This is a breaking change
as the bitflgs API is not like a normal rust enum (duh).

* variant_name() -> Option<_>

* repr transparent

* Reexport bitflags from flatbuffers

* Make bitflags constants CamelCase, matching normal enums

* Deprecate c-style associated enum constants

Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
Casper
2020-10-19 11:40:03 -07:00
committed by GitHub
parent a402b3abae
commit 9fa1d27059
13 changed files with 724 additions and 628 deletions

View File

@@ -1,6 +1,7 @@
// automatically generated by the FlatBuffers compiler, do not modify
#![allow(unused_imports, dead_code)]
use crate::include_test2_generated::*;
use std::mem;

View File

@@ -1,6 +1,7 @@
// automatically generated by the FlatBuffers compiler, do not modify
#![allow(unused_imports, dead_code)]
use crate::include_test1_generated::*;
use std::mem;
@@ -28,37 +29,50 @@ pub mod other_name_space {
extern crate flatbuffers;
use self::flatbuffers::EndianScalar;
#[allow(non_camel_case_types)]
#[repr(i64)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum FromInclude {
IncludeVal = 0,
}
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MIN_FROM_INCLUDE: i64 = 0;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MAX_FROM_INCLUDE: i64 = 0;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_FROM_INCLUDE: [FromInclude; 1] = [
FromInclude::IncludeVal,
];
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct FromInclude(pub i64);
#[allow(non_upper_case_globals)]
impl FromInclude {
pub const IncludeVal: Self = Self(0);
pub const ENUM_MIN: i64 = 0;
pub const ENUM_MAX: i64 = 0;
pub const ENUM_VALUES: &'static [Self] = &[
Self::IncludeVal,
];
/// Returns the variant's name or "" if unknown.
pub fn variant_name(self) -> Option<&'static str> {
match self {
Self::IncludeVal => Some("IncludeVal"),
_ => None,
}
}
}
impl std::fmt::Debug for FromInclude {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(name) = self.variant_name() {
f.write_str(name)
} else {
f.write_fmt(format_args!("<UNKNOWN {:?}>", self.0))
}
}
}
impl<'a> flatbuffers::Follow<'a> for FromInclude {
type Inner = Self;
#[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
flatbuffers::read_scalar_at::<Self>(buf, loc)
}
}
impl flatbuffers::EndianScalar for FromInclude {
#[inline]
fn to_little_endian(self) -> Self {
let n = i64::to_le(self as i64);
let p = &n as *const i64 as *const FromInclude;
unsafe { *p }
}
#[inline]
fn from_little_endian(self) -> Self {
let n = i64::from_le(self as i64);
let p = &n as *const i64 as *const FromInclude;
unsafe { *p }
Self(flatbuffers::read_scalar_at::<i64>(buf, loc))
}
}
@@ -66,23 +80,19 @@ impl flatbuffers::Push for FromInclude {
type Output = FromInclude;
#[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<FromInclude>(dst, *self);
flatbuffers::emplace_scalar::<i64>(dst, self.0);
}
}
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_FROM_INCLUDE: [FromInclude; 1] = [
FromInclude::IncludeVal
];
#[allow(non_camel_case_types)]
pub const ENUM_NAMES_FROM_INCLUDE: [&str; 1] = [
"IncludeVal"
];
pub fn enum_name_from_include(e: FromInclude) -> &'static str {
let index = e as i64;
ENUM_NAMES_FROM_INCLUDE[index as usize]
impl flatbuffers::EndianScalar for FromInclude {
#[inline]
fn to_little_endian(self) -> Self {
Self(i64::to_le(self.0))
}
#[inline]
fn from_little_endian(self) -> Self {
Self(i64::from_le(self.0))
}
}
// struct Unused, aligned to 4

View File

@@ -1,6 +1,7 @@
// automatically generated by the FlatBuffers compiler, do not modify
#![allow(unused_imports, dead_code)]
use crate::include_test1_generated::*;
use crate::include_test2_generated::*;
@@ -175,43 +176,28 @@ pub mod example {
extern crate flatbuffers;
use self::flatbuffers::EndianScalar;
/// Composite components of Monster color.
#[allow(non_camel_case_types)]
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Color {
Red = 1,
/// \brief color Green
/// Green is bit_flag with value (1u << 1)
Green = 2,
/// \brief color Blue (1u << 3)
Blue = 8,
#[allow(non_upper_case_globals)]
mod bitflags_color {
flatbuffers::bitflags::bitflags! {
/// Composite components of Monster color.
pub struct Color: u8 {
const Red = 1;
/// \brief color Green
/// Green is bit_flag with value (1u << 1)
const Green = 2;
/// \brief color Blue (1u << 3)
const Blue = 8;
}
}
}
pub const ENUM_MIN_COLOR: u8 = 1;
pub const ENUM_MAX_COLOR: u8 = 8;
pub use self::bitflags_color::Color;
impl<'a> flatbuffers::Follow<'a> for Color {
type Inner = Self;
#[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
flatbuffers::read_scalar_at::<Self>(buf, loc)
}
}
impl flatbuffers::EndianScalar for Color {
#[inline]
fn to_little_endian(self) -> Self {
let n = u8::to_le(self as u8);
let p = &n as *const u8 as *const Color;
unsafe { *p }
}
#[inline]
fn from_little_endian(self) -> Self {
let n = u8::from_le(self as u8);
let p = &n as *const u8 as *const Color;
unsafe { *p }
let bits = flatbuffers::read_scalar_at::<u8>(buf, loc);
unsafe { Self::from_bits_unchecked(bits) }
}
}
@@ -219,68 +205,79 @@ impl flatbuffers::Push for Color {
type Output = Color;
#[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<Color>(dst, *self);
flatbuffers::emplace_scalar::<u8>(dst, self.bits());
}
}
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_COLOR: [Color; 3] = [
Color::Red,
Color::Green,
Color::Blue
];
#[allow(non_camel_case_types)]
pub const ENUM_NAMES_COLOR: [&str; 8] = [
"Red",
"Green",
"",
"",
"",
"",
"",
"Blue"
];
pub fn enum_name_color(e: Color) -> &'static str {
let index = e as u8 - Color::Red as u8;
ENUM_NAMES_COLOR[index as usize]
}
#[allow(non_camel_case_types)]
#[repr(i8)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Race {
None = -1,
Human = 0,
Dwarf = 1,
Elf = 2,
impl flatbuffers::EndianScalar for Color {
#[inline]
fn to_little_endian(self) -> Self {
let bits = u8::to_le(self.bits());
unsafe { Self::from_bits_unchecked(bits) }
}
#[inline]
fn from_little_endian(self) -> Self {
let bits = u8::from_le(self.bits());
unsafe { Self::from_bits_unchecked(bits) }
}
}
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MIN_RACE: i8 = -1;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MAX_RACE: i8 = 2;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_RACE: [Race; 4] = [
Race::None,
Race::Human,
Race::Dwarf,
Race::Elf,
];
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Race(pub i8);
#[allow(non_upper_case_globals)]
impl Race {
pub const None: Self = Self(-1);
pub const Human: Self = Self(0);
pub const Dwarf: Self = Self(1);
pub const Elf: Self = Self(2);
pub const ENUM_MIN: i8 = -1;
pub const ENUM_MAX: i8 = 2;
pub const ENUM_VALUES: &'static [Self] = &[
Self::None,
Self::Human,
Self::Dwarf,
Self::Elf,
];
/// Returns the variant's name or "" if unknown.
pub fn variant_name(self) -> Option<&'static str> {
match self {
Self::None => Some("None"),
Self::Human => Some("Human"),
Self::Dwarf => Some("Dwarf"),
Self::Elf => Some("Elf"),
_ => None,
}
}
}
impl std::fmt::Debug for Race {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(name) = self.variant_name() {
f.write_str(name)
} else {
f.write_fmt(format_args!("<UNKNOWN {:?}>", self.0))
}
}
}
impl<'a> flatbuffers::Follow<'a> for Race {
type Inner = Self;
#[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
flatbuffers::read_scalar_at::<Self>(buf, loc)
}
}
impl flatbuffers::EndianScalar for Race {
#[inline]
fn to_little_endian(self) -> Self {
let n = i8::to_le(self as i8);
let p = &n as *const i8 as *const Race;
unsafe { *p }
}
#[inline]
fn from_little_endian(self) -> Self {
let n = i8::from_le(self as i8);
let p = &n as *const i8 as *const Race;
unsafe { *p }
Self(flatbuffers::read_scalar_at::<i8>(buf, loc))
}
}
@@ -288,65 +285,77 @@ impl flatbuffers::Push for Race {
type Output = Race;
#[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<Race>(dst, *self);
flatbuffers::emplace_scalar::<i8>(dst, self.0);
}
}
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_RACE: [Race; 4] = [
Race::None,
Race::Human,
Race::Dwarf,
Race::Elf
];
#[allow(non_camel_case_types)]
pub const ENUM_NAMES_RACE: [&str; 4] = [
"None",
"Human",
"Dwarf",
"Elf"
];
pub fn enum_name_race(e: Race) -> &'static str {
let index = e as i8 - Race::None as i8;
ENUM_NAMES_RACE[index as usize]
}
#[allow(non_camel_case_types)]
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Any {
NONE = 0,
Monster = 1,
TestSimpleTableWithEnum = 2,
MyGame_Example2_Monster = 3,
impl flatbuffers::EndianScalar for Race {
#[inline]
fn to_little_endian(self) -> Self {
Self(i8::to_le(self.0))
}
#[inline]
fn from_little_endian(self) -> Self {
Self(i8::from_le(self.0))
}
}
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MIN_ANY: u8 = 0;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MAX_ANY: u8 = 3;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_ANY: [Any; 4] = [
Any::NONE,
Any::Monster,
Any::TestSimpleTableWithEnum,
Any::MyGame_Example2_Monster,
];
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Any(pub u8);
#[allow(non_upper_case_globals)]
impl Any {
pub const NONE: Self = Self(0);
pub const Monster: Self = Self(1);
pub const TestSimpleTableWithEnum: Self = Self(2);
pub const MyGame_Example2_Monster: Self = Self(3);
pub const ENUM_MIN: u8 = 0;
pub const ENUM_MAX: u8 = 3;
pub const ENUM_VALUES: &'static [Self] = &[
Self::NONE,
Self::Monster,
Self::TestSimpleTableWithEnum,
Self::MyGame_Example2_Monster,
];
/// Returns the variant's name or "" if unknown.
pub fn variant_name(self) -> Option<&'static str> {
match self {
Self::NONE => Some("NONE"),
Self::Monster => Some("Monster"),
Self::TestSimpleTableWithEnum => Some("TestSimpleTableWithEnum"),
Self::MyGame_Example2_Monster => Some("MyGame_Example2_Monster"),
_ => None,
}
}
}
impl std::fmt::Debug for Any {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(name) = self.variant_name() {
f.write_str(name)
} else {
f.write_fmt(format_args!("<UNKNOWN {:?}>", self.0))
}
}
}
impl<'a> flatbuffers::Follow<'a> for Any {
type Inner = Self;
#[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
flatbuffers::read_scalar_at::<Self>(buf, loc)
}
}
impl flatbuffers::EndianScalar for Any {
#[inline]
fn to_little_endian(self) -> Self {
let n = u8::to_le(self as u8);
let p = &n as *const u8 as *const Any;
unsafe { *p }
}
#[inline]
fn from_little_endian(self) -> Self {
let n = u8::from_le(self as u8);
let p = &n as *const u8 as *const Any;
unsafe { *p }
Self(flatbuffers::read_scalar_at::<u8>(buf, loc))
}
}
@@ -354,66 +363,78 @@ impl flatbuffers::Push for Any {
type Output = Any;
#[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<Any>(dst, *self);
flatbuffers::emplace_scalar::<u8>(dst, self.0);
}
}
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_ANY: [Any; 4] = [
Any::NONE,
Any::Monster,
Any::TestSimpleTableWithEnum,
Any::MyGame_Example2_Monster
];
#[allow(non_camel_case_types)]
pub const ENUM_NAMES_ANY: [&str; 4] = [
"NONE",
"Monster",
"TestSimpleTableWithEnum",
"MyGame_Example2_Monster"
];
pub fn enum_name_any(e: Any) -> &'static str {
let index = e as u8;
ENUM_NAMES_ANY[index as usize]
impl flatbuffers::EndianScalar for Any {
#[inline]
fn to_little_endian(self) -> Self {
Self(u8::to_le(self.0))
}
#[inline]
fn from_little_endian(self) -> Self {
Self(u8::from_le(self.0))
}
}
pub struct AnyUnionTableOffset {}
#[allow(non_camel_case_types)]
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum AnyUniqueAliases {
NONE = 0,
M = 1,
TS = 2,
M2 = 3,
}
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MIN_ANY_UNIQUE_ALIASES: u8 = 0;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MAX_ANY_UNIQUE_ALIASES: u8 = 3;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_ANY_UNIQUE_ALIASES: [AnyUniqueAliases; 4] = [
AnyUniqueAliases::NONE,
AnyUniqueAliases::M,
AnyUniqueAliases::TS,
AnyUniqueAliases::M2,
];
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct AnyUniqueAliases(pub u8);
#[allow(non_upper_case_globals)]
impl AnyUniqueAliases {
pub const NONE: Self = Self(0);
pub const M: Self = Self(1);
pub const TS: Self = Self(2);
pub const M2: Self = Self(3);
pub const ENUM_MIN: u8 = 0;
pub const ENUM_MAX: u8 = 3;
pub const ENUM_VALUES: &'static [Self] = &[
Self::NONE,
Self::M,
Self::TS,
Self::M2,
];
/// Returns the variant's name or "" if unknown.
pub fn variant_name(self) -> Option<&'static str> {
match self {
Self::NONE => Some("NONE"),
Self::M => Some("M"),
Self::TS => Some("TS"),
Self::M2 => Some("M2"),
_ => None,
}
}
}
impl std::fmt::Debug for AnyUniqueAliases {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(name) = self.variant_name() {
f.write_str(name)
} else {
f.write_fmt(format_args!("<UNKNOWN {:?}>", self.0))
}
}
}
impl<'a> flatbuffers::Follow<'a> for AnyUniqueAliases {
type Inner = Self;
#[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
flatbuffers::read_scalar_at::<Self>(buf, loc)
}
}
impl flatbuffers::EndianScalar for AnyUniqueAliases {
#[inline]
fn to_little_endian(self) -> Self {
let n = u8::to_le(self as u8);
let p = &n as *const u8 as *const AnyUniqueAliases;
unsafe { *p }
}
#[inline]
fn from_little_endian(self) -> Self {
let n = u8::from_le(self as u8);
let p = &n as *const u8 as *const AnyUniqueAliases;
unsafe { *p }
Self(flatbuffers::read_scalar_at::<u8>(buf, loc))
}
}
@@ -421,66 +442,78 @@ impl flatbuffers::Push for AnyUniqueAliases {
type Output = AnyUniqueAliases;
#[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<AnyUniqueAliases>(dst, *self);
flatbuffers::emplace_scalar::<u8>(dst, self.0);
}
}
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_ANY_UNIQUE_ALIASES: [AnyUniqueAliases; 4] = [
AnyUniqueAliases::NONE,
AnyUniqueAliases::M,
AnyUniqueAliases::TS,
AnyUniqueAliases::M2
];
#[allow(non_camel_case_types)]
pub const ENUM_NAMES_ANY_UNIQUE_ALIASES: [&str; 4] = [
"NONE",
"M",
"TS",
"M2"
];
pub fn enum_name_any_unique_aliases(e: AnyUniqueAliases) -> &'static str {
let index = e as u8;
ENUM_NAMES_ANY_UNIQUE_ALIASES[index as usize]
impl flatbuffers::EndianScalar for AnyUniqueAliases {
#[inline]
fn to_little_endian(self) -> Self {
Self(u8::to_le(self.0))
}
#[inline]
fn from_little_endian(self) -> Self {
Self(u8::from_le(self.0))
}
}
pub struct AnyUniqueAliasesUnionTableOffset {}
#[allow(non_camel_case_types)]
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum AnyAmbiguousAliases {
NONE = 0,
M1 = 1,
M2 = 2,
M3 = 3,
}
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MIN_ANY_AMBIGUOUS_ALIASES: u8 = 0;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MAX_ANY_AMBIGUOUS_ALIASES: u8 = 3;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_ANY_AMBIGUOUS_ALIASES: [AnyAmbiguousAliases; 4] = [
AnyAmbiguousAliases::NONE,
AnyAmbiguousAliases::M1,
AnyAmbiguousAliases::M2,
AnyAmbiguousAliases::M3,
];
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct AnyAmbiguousAliases(pub u8);
#[allow(non_upper_case_globals)]
impl AnyAmbiguousAliases {
pub const NONE: Self = Self(0);
pub const M1: Self = Self(1);
pub const M2: Self = Self(2);
pub const M3: Self = Self(3);
pub const ENUM_MIN: u8 = 0;
pub const ENUM_MAX: u8 = 3;
pub const ENUM_VALUES: &'static [Self] = &[
Self::NONE,
Self::M1,
Self::M2,
Self::M3,
];
/// Returns the variant's name or "" if unknown.
pub fn variant_name(self) -> Option<&'static str> {
match self {
Self::NONE => Some("NONE"),
Self::M1 => Some("M1"),
Self::M2 => Some("M2"),
Self::M3 => Some("M3"),
_ => None,
}
}
}
impl std::fmt::Debug for AnyAmbiguousAliases {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(name) = self.variant_name() {
f.write_str(name)
} else {
f.write_fmt(format_args!("<UNKNOWN {:?}>", self.0))
}
}
}
impl<'a> flatbuffers::Follow<'a> for AnyAmbiguousAliases {
type Inner = Self;
#[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
flatbuffers::read_scalar_at::<Self>(buf, loc)
}
}
impl flatbuffers::EndianScalar for AnyAmbiguousAliases {
#[inline]
fn to_little_endian(self) -> Self {
let n = u8::to_le(self as u8);
let p = &n as *const u8 as *const AnyAmbiguousAliases;
unsafe { *p }
}
#[inline]
fn from_little_endian(self) -> Self {
let n = u8::from_le(self as u8);
let p = &n as *const u8 as *const AnyAmbiguousAliases;
unsafe { *p }
Self(flatbuffers::read_scalar_at::<u8>(buf, loc))
}
}
@@ -488,29 +521,19 @@ impl flatbuffers::Push for AnyAmbiguousAliases {
type Output = AnyAmbiguousAliases;
#[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<AnyAmbiguousAliases>(dst, *self);
flatbuffers::emplace_scalar::<u8>(dst, self.0);
}
}
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_ANY_AMBIGUOUS_ALIASES: [AnyAmbiguousAliases; 4] = [
AnyAmbiguousAliases::NONE,
AnyAmbiguousAliases::M1,
AnyAmbiguousAliases::M2,
AnyAmbiguousAliases::M3
];
#[allow(non_camel_case_types)]
pub const ENUM_NAMES_ANY_AMBIGUOUS_ALIASES: [&str; 4] = [
"NONE",
"M1",
"M2",
"M3"
];
pub fn enum_name_any_ambiguous_aliases(e: AnyAmbiguousAliases) -> &'static str {
let index = e as u8;
ENUM_NAMES_ANY_AMBIGUOUS_ALIASES[index as usize]
impl flatbuffers::EndianScalar for AnyAmbiguousAliases {
#[inline]
fn to_little_endian(self) -> Self {
Self(u8::to_le(self.0))
}
#[inline]
fn from_little_endian(self) -> Self {
Self(u8::from_le(self.0))
}
}
pub struct AnyAmbiguousAliasesUnionTableOffset {}

View File

@@ -1,6 +1,7 @@
// automatically generated by the FlatBuffers compiler, do not modify
#![allow(unused_imports, dead_code)]
use std::mem;
use std::cmp::Ordering;
@@ -25,39 +26,58 @@ pub mod namespace_b {
extern crate flatbuffers;
use self::flatbuffers::EndianScalar;
#[allow(non_camel_case_types)]
#[repr(i8)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum EnumInNestedNS {
A = 0,
B = 1,
C = 2,
}
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MIN_ENUM_IN_NESTED_NS: i8 = 0;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MAX_ENUM_IN_NESTED_NS: i8 = 2;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_ENUM_IN_NESTED_NS: [EnumInNestedNS; 3] = [
EnumInNestedNS::A,
EnumInNestedNS::B,
EnumInNestedNS::C,
];
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct EnumInNestedNS(pub i8);
#[allow(non_upper_case_globals)]
impl EnumInNestedNS {
pub const A: Self = Self(0);
pub const B: Self = Self(1);
pub const C: Self = Self(2);
pub const ENUM_MIN: i8 = 0;
pub const ENUM_MAX: i8 = 2;
pub const ENUM_VALUES: &'static [Self] = &[
Self::A,
Self::B,
Self::C,
];
/// Returns the variant's name or "" if unknown.
pub fn variant_name(self) -> Option<&'static str> {
match self {
Self::A => Some("A"),
Self::B => Some("B"),
Self::C => Some("C"),
_ => None,
}
}
}
impl std::fmt::Debug for EnumInNestedNS {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(name) = self.variant_name() {
f.write_str(name)
} else {
f.write_fmt(format_args!("<UNKNOWN {:?}>", self.0))
}
}
}
impl<'a> flatbuffers::Follow<'a> for EnumInNestedNS {
type Inner = Self;
#[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
flatbuffers::read_scalar_at::<Self>(buf, loc)
}
}
impl flatbuffers::EndianScalar for EnumInNestedNS {
#[inline]
fn to_little_endian(self) -> Self {
let n = i8::to_le(self as i8);
let p = &n as *const i8 as *const EnumInNestedNS;
unsafe { *p }
}
#[inline]
fn from_little_endian(self) -> Self {
let n = i8::from_le(self as i8);
let p = &n as *const i8 as *const EnumInNestedNS;
unsafe { *p }
Self(flatbuffers::read_scalar_at::<i8>(buf, loc))
}
}
@@ -65,27 +85,19 @@ impl flatbuffers::Push for EnumInNestedNS {
type Output = EnumInNestedNS;
#[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<EnumInNestedNS>(dst, *self);
flatbuffers::emplace_scalar::<i8>(dst, self.0);
}
}
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_ENUM_IN_NESTED_NS: [EnumInNestedNS; 3] = [
EnumInNestedNS::A,
EnumInNestedNS::B,
EnumInNestedNS::C
];
#[allow(non_camel_case_types)]
pub const ENUM_NAMES_ENUM_IN_NESTED_NS: [&str; 3] = [
"A",
"B",
"C"
];
pub fn enum_name_enum_in_nested_ns(e: EnumInNestedNS) -> &'static str {
let index = e as i8;
ENUM_NAMES_ENUM_IN_NESTED_NS[index as usize]
impl flatbuffers::EndianScalar for EnumInNestedNS {
#[inline]
fn to_little_endian(self) -> Self {
Self(i8::to_le(self.0))
}
#[inline]
fn from_little_endian(self) -> Self {
Self(i8::from_le(self.0))
}
}
// struct StructInNestedNS, aligned to 4

View File

@@ -1,6 +1,7 @@
// automatically generated by the FlatBuffers compiler, do not modify
#![allow(unused_imports, dead_code)]
use crate::namespace_test1_generated::*;
use std::mem;

View File

@@ -1,6 +1,7 @@
// automatically generated by the FlatBuffers compiler, do not modify
#![allow(unused_imports, dead_code)]
use std::mem;
use std::cmp::Ordering;
@@ -17,39 +18,58 @@ pub mod optional_scalars {
extern crate flatbuffers;
use self::flatbuffers::EndianScalar;
#[allow(non_camel_case_types)]
#[repr(i8)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum OptionalByte {
None = 0,
One = 1,
Two = 2,
}
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MIN_OPTIONAL_BYTE: i8 = 0;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MAX_OPTIONAL_BYTE: i8 = 2;
#[deprecated(since = "1.13", note = "Use associated constants instead. This will no longer be generated in 2021.")]
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_OPTIONAL_BYTE: [OptionalByte; 3] = [
OptionalByte::None,
OptionalByte::One,
OptionalByte::Two,
];
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct OptionalByte(pub i8);
#[allow(non_upper_case_globals)]
impl OptionalByte {
pub const None: Self = Self(0);
pub const One: Self = Self(1);
pub const Two: Self = Self(2);
pub const ENUM_MIN: i8 = 0;
pub const ENUM_MAX: i8 = 2;
pub const ENUM_VALUES: &'static [Self] = &[
Self::None,
Self::One,
Self::Two,
];
/// Returns the variant's name or "" if unknown.
pub fn variant_name(self) -> Option<&'static str> {
match self {
Self::None => Some("None"),
Self::One => Some("One"),
Self::Two => Some("Two"),
_ => None,
}
}
}
impl std::fmt::Debug for OptionalByte {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(name) = self.variant_name() {
f.write_str(name)
} else {
f.write_fmt(format_args!("<UNKNOWN {:?}>", self.0))
}
}
}
impl<'a> flatbuffers::Follow<'a> for OptionalByte {
type Inner = Self;
#[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
flatbuffers::read_scalar_at::<Self>(buf, loc)
}
}
impl flatbuffers::EndianScalar for OptionalByte {
#[inline]
fn to_little_endian(self) -> Self {
let n = i8::to_le(self as i8);
let p = &n as *const i8 as *const OptionalByte;
unsafe { *p }
}
#[inline]
fn from_little_endian(self) -> Self {
let n = i8::from_le(self as i8);
let p = &n as *const i8 as *const OptionalByte;
unsafe { *p }
Self(flatbuffers::read_scalar_at::<i8>(buf, loc))
}
}
@@ -57,27 +77,19 @@ impl flatbuffers::Push for OptionalByte {
type Output = OptionalByte;
#[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
flatbuffers::emplace_scalar::<OptionalByte>(dst, *self);
flatbuffers::emplace_scalar::<i8>(dst, self.0);
}
}
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_OPTIONAL_BYTE: [OptionalByte; 3] = [
OptionalByte::None,
OptionalByte::One,
OptionalByte::Two
];
#[allow(non_camel_case_types)]
pub const ENUM_NAMES_OPTIONAL_BYTE: [&str; 3] = [
"None",
"One",
"Two"
];
pub fn enum_name_optional_byte(e: OptionalByte) -> &'static str {
let index = e as i8;
ENUM_NAMES_OPTIONAL_BYTE[index as usize]
impl flatbuffers::EndianScalar for OptionalByte {
#[inline]
fn to_little_endian(self) -> Self {
Self(i8::to_le(self.0))
}
#[inline]
fn from_little_endian(self) -> Self {
Self(i8::from_le(self.0))
}
}
pub enum ScalarStuffOffset {}

View File

@@ -32,6 +32,11 @@ path = "../../samples/sample_flexbuffers.rs"
name = "sample_flexbuffers_serde"
path = "../../samples/sample_flexbuffers_serde.rs"
[[bin]]
name = "sample_flatbuffers"
path = "../../samples/sample_binary.rs"
[dev-dependencies]
quickcheck = "0.6"
# TODO(rw): look into moving to criterion.rs

View File

@@ -29,15 +29,12 @@ extern crate quickcheck_derive;
mod flexbuffers_tests;
mod optional_scalars_test;
#[allow(dead_code, unused_imports)]
#[path = "../../include_test/include_test1_generated.rs"]
pub mod include_test1_generated;
#[allow(dead_code, unused_imports)]
#[path = "../../include_test/sub/include_test2_generated.rs"]
pub mod include_test2_generated;
#[allow(dead_code, unused_imports)]
#[path = "../../monster_test_generated.rs"]
mod monster_test_generated;
pub use monster_test_generated::my_game;
@@ -265,83 +262,41 @@ mod generated_constants {
#[test]
fn enum_constants_are_public() {
assert_eq!(1, my_game::example::ENUM_MIN_COLOR);
assert_eq!(8, my_game::example::ENUM_MAX_COLOR);
assert_eq!(my_game::example::ENUM_VALUES_COLOR, [
my_game::example::Color::Red,
my_game::example::Color::Green,
my_game::example::Color::Blue,
]);
assert_eq!(my_game::example::ENUM_NAMES_COLOR, [
"Red",
"Green",
"",
"",
"",
"",
"",
"Blue"
]);
assert_eq!(-1, my_game::example::ENUM_MIN_RACE);
assert_eq!(2, my_game::example::ENUM_MAX_RACE);
assert_eq!(my_game::example::ENUM_VALUES_RACE, [
assert_eq!(-1, my_game::example::Race::ENUM_MIN);
assert_eq!(2, my_game::example::Race::ENUM_MAX);
assert_eq!(my_game::example::Race::ENUM_VALUES, [
my_game::example::Race::None,
my_game::example::Race::Human,
my_game::example::Race::Dwarf,
my_game::example::Race::Elf,
]);
assert_eq!(my_game::example::ENUM_NAMES_RACE, [
"None",
"Human",
"Dwarf",
"Elf"
]);
assert_eq!(0, my_game::example::ENUM_MIN_ANY);
assert_eq!(3, my_game::example::ENUM_MAX_ANY);
assert_eq!(my_game::example::ENUM_VALUES_ANY, [
assert_eq!(0, my_game::example::Any::ENUM_MIN);
assert_eq!(3, my_game::example::Any::ENUM_MAX);
assert_eq!(my_game::example::Any::ENUM_VALUES, [
my_game::example::Any::NONE,
my_game::example::Any::Monster,
my_game::example::Any::TestSimpleTableWithEnum,
my_game::example::Any::MyGame_Example2_Monster,
]);
assert_eq!(my_game::example::ENUM_NAMES_ANY, [
"NONE",
"Monster",
"TestSimpleTableWithEnum",
"MyGame_Example2_Monster"
]);
assert_eq!(0, my_game::example::ENUM_MIN_ANY_UNIQUE_ALIASES);
assert_eq!(3, my_game::example::ENUM_MAX_ANY_UNIQUE_ALIASES);
assert_eq!(my_game::example::ENUM_VALUES_ANY_UNIQUE_ALIASES, [
assert_eq!(0, my_game::example::AnyUniqueAliases::ENUM_MIN);
assert_eq!(3, my_game::example::AnyUniqueAliases::ENUM_MAX);
assert_eq!(my_game::example::AnyUniqueAliases::ENUM_VALUES, [
my_game::example::AnyUniqueAliases::NONE,
my_game::example::AnyUniqueAliases::M,
my_game::example::AnyUniqueAliases::TS,
my_game::example::AnyUniqueAliases::M2,
]);
assert_eq!(my_game::example::ENUM_NAMES_ANY_UNIQUE_ALIASES, [
"NONE",
"M",
"TS",
"M2"
]);
assert_eq!(0, my_game::example::ENUM_MIN_ANY_AMBIGUOUS_ALIASES);
assert_eq!(3, my_game::example::ENUM_MAX_ANY_AMBIGUOUS_ALIASES);
assert_eq!(my_game::example::ENUM_VALUES_ANY_AMBIGUOUS_ALIASES, [
assert_eq!(0, my_game::example::AnyAmbiguousAliases::ENUM_MIN);
assert_eq!(3, my_game::example::AnyAmbiguousAliases::ENUM_MAX);
assert_eq!(my_game::example::AnyAmbiguousAliases::ENUM_VALUES, [
my_game::example::AnyAmbiguousAliases::NONE,
my_game::example::AnyAmbiguousAliases::M1,
my_game::example::AnyAmbiguousAliases::M2,
my_game::example::AnyAmbiguousAliases::M3,
]);
assert_eq!(my_game::example::ENUM_NAMES_ANY_AMBIGUOUS_ALIASES, [
"NONE",
"M1",
"M2",
"M3"
]);
}
}
@@ -732,19 +687,18 @@ mod roundtrip_generated_code {
test4: Some(v), ..Default::default()});
assert_eq!(m.test4().unwrap(), &[my_game::example::Test::new(127, -128), my_game::example::Test::new(3, 123), my_game::example::Test::new(100, 101)][..]);
}
// TODO(rw) this passes, but I don't want to change the monster test schema right now
// #[test]
// fn vector_of_enum_store() {
// let mut b = flatbuffers::FlatBufferBuilder::new();
// let v = b.create_vector::<my_game::example::Color>(&[my_game::example::Color::Red, my_game::example::Color::Green][..]);
// let name = b.create_string("foo");
// let m = build_mon(&mut b, &my_game::example::MonsterArgs{
// name: Some(name),
// vector_of_enum: Some(v), ..Default::default()});
// assert_eq!(m.vector_of_enum().unwrap().len(), 2);
// assert_eq!(m.vector_of_enum().unwrap().get(0), my_game::example::Color::Red);
// assert_eq!(m.vector_of_enum().unwrap().get(1), my_game::example::Color::Green);
// }
#[test]
fn vector_of_enums_store() {
let mut b = flatbuffers::FlatBufferBuilder::new();
let v = b.create_vector::<my_game::example::Color>(&[my_game::example::Color::Red, my_game::example::Color::Green][..]);
let name = b.create_string("foo");
let m = build_mon(&mut b, &my_game::example::MonsterArgs{
name: Some(name),
vector_of_enums: Some(v), ..Default::default()});
assert_eq!(m.vector_of_enums().unwrap().len(), 2);
assert_eq!(m.vector_of_enums().unwrap().get(0), my_game::example::Color::Red);
assert_eq!(m.vector_of_enums().unwrap().get(1), my_game::example::Color::Green);
}
#[test]
fn vector_of_table_store() {
let b = &mut flatbuffers::FlatBufferBuilder::new();