Commit c329d6fa authored by Matt Mastracci's avatar Matt Mastracci Committed by Wouter van Oortmerssen

Ensure we don't subtract with underflow getting enum names (#5246)

* Ensure we don't subtract with underflow getting enum names

* Yep - forgot to run this
parent 249f3b37
......@@ -698,14 +698,14 @@ class RustGenerator : public BaseGenerator {
code_ += "pub fn enum_name_{{ENUM_NAME_SNAKE}}(e: {{ENUM_NAME}}) -> "
"&'static str {";
code_ += " let index: usize = e as usize\\";
code_ += " let index = e as {{BASE_TYPE}}\\";
if (enum_def.vals.vec.front()->value) {
auto vals = GetEnumValUse(enum_def, *enum_def.vals.vec.front());
code_ += " - " + vals + " as usize\\";
code_ += " - " + vals + " as {{BASE_TYPE}}\\";
}
code_ += ";";
code_ += " ENUM_NAMES_{{ENUM_NAME_CAPS}}[index]";
code_ += " ENUM_NAMES_{{ENUM_NAME_CAPS}}[index as usize]";
code_ += "}";
code_ += "";
}
......
......@@ -227,8 +227,8 @@ const ENUM_NAMES_COLOR:[&'static str; 8] = [
];
pub fn enum_name_color(e: Color) -> &'static str {
let index: usize = e as usize - Color::Red as usize;
ENUM_NAMES_COLOR[index]
let index = e as i8 - Color::Red as i8;
ENUM_NAMES_COLOR[index as usize]
}
#[allow(non_camel_case_types)]
......@@ -293,8 +293,8 @@ const ENUM_NAMES_ANY:[&'static str; 4] = [
];
pub fn enum_name_any(e: Any) -> &'static str {
let index: usize = e as usize;
ENUM_NAMES_ANY[index]
let index = e as u8;
ENUM_NAMES_ANY[index as usize]
}
pub struct AnyUnionTableOffset {}
......@@ -360,8 +360,8 @@ const ENUM_NAMES_ANY_UNIQUE_ALIASES:[&'static str; 4] = [
];
pub fn enum_name_any_unique_aliases(e: AnyUniqueAliases) -> &'static str {
let index: usize = e as usize;
ENUM_NAMES_ANY_UNIQUE_ALIASES[index]
let index = e as u8;
ENUM_NAMES_ANY_UNIQUE_ALIASES[index as usize]
}
pub struct AnyUniqueAliasesUnionTableOffset {}
......@@ -427,8 +427,8 @@ const ENUM_NAMES_ANY_AMBIGUOUS_ALIASES:[&'static str; 4] = [
];
pub fn enum_name_any_ambiguous_aliases(e: AnyAmbiguousAliases) -> &'static str {
let index: usize = e as usize;
ENUM_NAMES_ANY_AMBIGUOUS_ALIASES[index]
let index = e as u8;
ENUM_NAMES_ANY_AMBIGUOUS_ALIASES[index as usize]
}
pub struct AnyAmbiguousAliasesUnionTableOffset {}
......
......@@ -84,8 +84,8 @@ const ENUM_NAMES_ENUM_IN_NESTED_NS:[&'static str; 3] = [
];
pub fn enum_name_enum_in_nested_ns(e: EnumInNestedNS) -> &'static str {
let index: usize = e as usize;
ENUM_NAMES_ENUM_IN_NESTED_NS[index]
let index = e as i8;
ENUM_NAMES_ENUM_IN_NESTED_NS[index as usize]
}
// struct StructInNestedNS, aligned to 4
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment