mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-25 03:16:11 +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;
|
||||
@@ -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 {}
|
||||
|
||||
Reference in New Issue
Block a user