Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/idl_gen_rust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,15 @@ class RustGenerator : public BaseGenerator {
code_ += "pub use self::bitflags_{{ENUM_NAMESPACE}}::{{ENUM_TY}};";
code_ += "";

code_ += "impl {{ENUM_TY}} {";
code_ += " pub const ENUM_VALUES: &'static [Self] = &[";
ForAllEnumValues1(enum_def, [&](const EnumVal& ev) {
code_.SetValue("VARIANT", namer_.Variant(ev));
code_ += " Self::{{VARIANT}},";
});
code_ += " ];";
code_ += "}";

code_.SetValue("INTO_BASE", "self.bits()");
} else {
// Normal, c-modelled enums.
Expand Down
7 changes: 7 additions & 0 deletions tests/monster_test/my_game/example/color_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ mod bitflags_color {
}
pub use self::bitflags_color::Color;

impl Color {
pub const ENUM_VALUES: &'static [Self] = &[
Self::Red,
Self::Green,
Self::Blue,
];
}
impl<'a> ::flatbuffers::Follow<'a> for Color {
type Inner = Self;
#[inline]
Expand Down
7 changes: 7 additions & 0 deletions tests/monster_test/my_game/example/long_enum_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ mod bitflags_long_enum {
}
pub use self::bitflags_long_enum::LongEnum;

impl LongEnum {
pub const ENUM_VALUES: &'static [Self] = &[
Self::LongOne,
Self::LongTwo,
Self::LongBig,
];
}
impl<'a> ::flatbuffers::Follow<'a> for LongEnum {
type Inner = Self;
#[inline]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ mod bitflags_color {
}
pub use self::bitflags_color::Color;

impl Color {
pub const ENUM_VALUES: &'static [Self] = &[
Self::Red,
Self::Green,
Self::Blue,
];
}
impl Serialize for Color {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ mod bitflags_long_enum {
}
pub use self::bitflags_long_enum::LongEnum;

impl LongEnum {
pub const ENUM_VALUES: &'static [Self] = &[
Self::LongOne,
Self::LongTwo,
Self::LongBig,
];
}
impl Serialize for LongEnum {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down
Loading