mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-25 11:18:38 +00:00
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:
@@ -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,45 @@ pub mod sample {
|
||||
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 Color {
|
||||
Red = 0,
|
||||
Green = 1,
|
||||
Blue = 2,
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Color(pub i8);
|
||||
#[allow(non_upper_case_globals)]
|
||||
impl Color {
|
||||
pub const ENUM_MIN: i8 = 0;
|
||||
pub const ENUM_MAX: i8 = 2;
|
||||
pub const Red: Self = Self(0);
|
||||
pub const Green: Self = Self(1);
|
||||
pub const Blue: Self = Self(2);
|
||||
pub const ENUM_VALUES: &'static [Self] = &[
|
||||
Self::Red,
|
||||
Self::Green,
|
||||
Self::Blue,
|
||||
];
|
||||
/// Returns the variant's name or "" if unknown.
|
||||
pub fn variant_name(self) -> &'static str {
|
||||
match self {
|
||||
Self::Red => "Red",
|
||||
Self::Green => "Green",
|
||||
Self::Blue => "Blue",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
}
|
||||
impl std::fmt::Debug for Color {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let name = self.variant_name();
|
||||
if name.is_empty() {
|
||||
f.write_fmt(format_args!("<UNKNOWN {:?}>", self.0))
|
||||
} else {
|
||||
f.write_str(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const ENUM_MIN_COLOR: i8 = 0;
|
||||
pub const ENUM_MAX_COLOR: i8 = 2;
|
||||
|
||||
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 = i8::to_le(self as i8);
|
||||
let p = &n as *const i8 as *const Color;
|
||||
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 Color;
|
||||
unsafe { *p }
|
||||
Self(flatbuffers::read_scalar_at::<i8>(buf, loc))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,10 +72,21 @@ 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::<i8>(dst, self.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl flatbuffers::EndianScalar for Color {
|
||||
#[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))
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
pub const ENUM_VALUES_COLOR: [Color; 3] = [
|
||||
Color::Red,
|
||||
@@ -76,50 +94,42 @@ pub const ENUM_VALUES_COLOR: [Color; 3] = [
|
||||
Color::Blue
|
||||
];
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
pub const ENUM_NAMES_COLOR: [&str; 3] = [
|
||||
"Red",
|
||||
"Green",
|
||||
"Blue"
|
||||
];
|
||||
|
||||
pub fn enum_name_color(e: Color) -> &'static str {
|
||||
let index = e as i8;
|
||||
ENUM_NAMES_COLOR[index as usize]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Equipment(pub u8);
|
||||
#[allow(non_upper_case_globals)]
|
||||
impl Equipment {
|
||||
pub const ENUM_MIN: u8 = 0;
|
||||
pub const ENUM_MAX: u8 = 1;
|
||||
pub const NONE: Self = Self(0);
|
||||
pub const Weapon: Self = Self(1);
|
||||
pub const ENUM_VALUES: &'static [Self] = &[
|
||||
Self::NONE,
|
||||
Self::Weapon,
|
||||
];
|
||||
/// Returns the variant's name or "" if unknown.
|
||||
pub fn variant_name(self) -> &'static str {
|
||||
match self {
|
||||
Self::NONE => "NONE",
|
||||
Self::Weapon => "Weapon",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[repr(u8)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
||||
pub enum Equipment {
|
||||
NONE = 0,
|
||||
Weapon = 1,
|
||||
|
||||
impl std::fmt::Debug for Equipment {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let name = self.variant_name();
|
||||
if name.is_empty() {
|
||||
f.write_fmt(format_args!("<UNKNOWN {:?}>", self.0))
|
||||
} else {
|
||||
f.write_str(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const ENUM_MIN_EQUIPMENT: u8 = 0;
|
||||
pub const ENUM_MAX_EQUIPMENT: u8 = 1;
|
||||
|
||||
impl<'a> flatbuffers::Follow<'a> for Equipment {
|
||||
type Inner = Self;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
flatbuffers::read_scalar_at::<Self>(buf, loc)
|
||||
}
|
||||
}
|
||||
|
||||
impl flatbuffers::EndianScalar for Equipment {
|
||||
#[inline]
|
||||
fn to_little_endian(self) -> Self {
|
||||
let n = u8::to_le(self as u8);
|
||||
let p = &n as *const u8 as *const Equipment;
|
||||
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 Equipment;
|
||||
unsafe { *p }
|
||||
Self(flatbuffers::read_scalar_at::<u8>(buf, loc))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,27 +137,27 @@ impl flatbuffers::Push for Equipment {
|
||||
type Output = Equipment;
|
||||
#[inline]
|
||||
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
|
||||
flatbuffers::emplace_scalar::<Equipment>(dst, *self);
|
||||
flatbuffers::emplace_scalar::<u8>(dst, self.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl flatbuffers::EndianScalar for Equipment {
|
||||
#[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))
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
pub const ENUM_VALUES_EQUIPMENT: [Equipment; 2] = [
|
||||
Equipment::NONE,
|
||||
Equipment::Weapon
|
||||
];
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
pub const ENUM_NAMES_EQUIPMENT: [&str; 2] = [
|
||||
"NONE",
|
||||
"Weapon"
|
||||
];
|
||||
|
||||
pub fn enum_name_equipment(e: Equipment) -> &'static str {
|
||||
let index = e as u8;
|
||||
ENUM_NAMES_EQUIPMENT[index as usize]
|
||||
}
|
||||
|
||||
pub struct EquipmentUnionTableOffset {}
|
||||
// struct Vec3, aligned to 4
|
||||
#[repr(C, align(4))]
|
||||
|
||||
@@ -153,3 +153,9 @@ fn main() {
|
||||
|
||||
println!("The FlatBuffer was successfully created and accessed!");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[test]
|
||||
fn test_main() {
|
||||
main()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user