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 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;