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
577 changes: 299 additions & 278 deletions goldens/rust/basic_generated.rs

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions samples/rust_generated/my_game/sample/color_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// @generated
extern crate alloc;
use super::*;

#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MIN_COLOR: i8 = 0;

#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MAX_COLOR: i8 = 2;

#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")]
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_COLOR: [Color; 3] = [
Expand All @@ -17,6 +20,7 @@ pub const ENUM_VALUES_COLOR: [Color; 3] = [
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[repr(transparent)]
pub struct Color(pub i8);

#[allow(non_upper_case_globals)]
impl Color {
pub const Red: Self = Self(0);
Expand All @@ -30,6 +34,7 @@ impl Color {
Self::Green,
Self::Blue,
];

/// Returns the variant's name or "" if unknown.
pub fn variant_name(self) -> Option<&'static str> {
match self {
Expand All @@ -40,6 +45,7 @@ impl Color {
}
}
}

impl ::core::fmt::Debug for Color {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
if let Some(name) = self.variant_name() {
Expand All @@ -49,8 +55,10 @@ impl ::core::fmt::Debug for Color {
}
}
}

impl<'a> ::flatbuffers::Follow<'a> for Color {
type Inner = Self;

#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = unsafe { ::flatbuffers::read_scalar_at::<i8>(buf, loc) };
Expand All @@ -60,6 +68,7 @@ impl<'a> ::flatbuffers::Follow<'a> for Color {

impl ::flatbuffers::Push for Color {
type Output = Color;

#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
unsafe { ::flatbuffers::emplace_scalar::<i8>(dst, self.0) };
Expand All @@ -68,10 +77,12 @@ impl ::flatbuffers::Push for Color {

impl ::flatbuffers::EndianScalar for Color {
type Scalar = i8;

#[inline]
fn to_little_endian(self) -> i8 {
self.0.to_le()
}

#[inline]
#[allow(clippy::wrong_self_convention)]
fn from_little_endian(v: i8) -> Self {
Expand Down
19 changes: 19 additions & 0 deletions samples/rust_generated/my_game/sample/equipment_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// @generated
extern crate alloc;
use super::*;

#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MIN_EQUIPMENT: u8 = 0;

#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")]
pub const ENUM_MAX_EQUIPMENT: u8 = 1;

#[deprecated(since = "2.0.0", note = "Use associated constants instead. This will no longer be generated in 2021.")]
#[allow(non_camel_case_types)]
pub const ENUM_VALUES_EQUIPMENT: [Equipment; 2] = [
Expand All @@ -16,6 +19,7 @@ pub const ENUM_VALUES_EQUIPMENT: [Equipment; 2] = [
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[repr(transparent)]
pub struct Equipment(pub u8);

#[allow(non_upper_case_globals)]
impl Equipment {
pub const NONE: Self = Self(0);
Expand All @@ -27,6 +31,7 @@ impl Equipment {
Self::NONE,
Self::Weapon,
];

/// Returns the variant's name or "" if unknown.
pub fn variant_name(self) -> Option<&'static str> {
match self {
Expand All @@ -36,6 +41,7 @@ impl Equipment {
}
}
}

impl ::core::fmt::Debug for Equipment {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
if let Some(name) = self.variant_name() {
Expand All @@ -45,8 +51,10 @@ impl ::core::fmt::Debug for Equipment {
}
}
}

impl<'a> ::flatbuffers::Follow<'a> for Equipment {
type Inner = Self;

#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = unsafe { ::flatbuffers::read_scalar_at::<u8>(buf, loc) };
Expand All @@ -56,6 +64,7 @@ impl<'a> ::flatbuffers::Follow<'a> for Equipment {

impl ::flatbuffers::Push for Equipment {
type Output = Equipment;

#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
unsafe { ::flatbuffers::emplace_scalar::<u8>(dst, self.0) };
Expand All @@ -64,10 +73,12 @@ impl ::flatbuffers::Push for Equipment {

impl ::flatbuffers::EndianScalar for Equipment {
type Scalar = u8;

#[inline]
fn to_little_endian(self) -> u8 {
self.0.to_le()
}

#[inline]
#[allow(clippy::wrong_self_convention)]
fn from_little_endian(v: u8) -> Self {
Expand All @@ -86,6 +97,7 @@ impl<'a> ::flatbuffers::Verifiable for Equipment {
}

impl ::flatbuffers::SimpleToVerifyInSlice for Equipment {}

pub struct EquipmentUnionTableOffset {}

#[allow(clippy::upper_case_acronyms)]
Expand All @@ -95,24 +107,28 @@ pub enum EquipmentT {
NONE,
Weapon(alloc::boxed::Box<WeaponT>),
}

impl Default for EquipmentT {
fn default() -> Self {
Self::NONE
}
}

impl EquipmentT {
pub fn equipment_type(&self) -> Equipment {
match self {
Self::NONE => Equipment::NONE,
Self::Weapon(_) => Equipment::Weapon,
}
}

pub fn pack<'b, A: ::flatbuffers::Allocator + 'b>(&self, fbb: &mut ::flatbuffers::FlatBufferBuilder<'b, A>) -> Option<::flatbuffers::WIPOffset<::flatbuffers::UnionWIPOffset>> {
match self {
Self::NONE => None,
Self::Weapon(v) => Some(v.pack(fbb).as_union_value()),
}
}

/// If the union variant matches, return the owned WeaponT, setting the union to NONE.
pub fn take_weapon(&mut self) -> Option<alloc::boxed::Box<WeaponT>> {
if let Self::Weapon(_) = self {
Expand All @@ -126,12 +142,15 @@ impl EquipmentT {
None
}
}

/// If the union variant matches, return a reference to the WeaponT.
pub fn as_weapon(&self) -> Option<&WeaponT> {
if let Self::Weapon(v) = self { Some(v.as_ref()) } else { None }
}

/// If the union variant matches, return a mutable reference to the WeaponT.
pub fn as_weapon_mut(&mut self) -> Option<&mut WeaponT> {
if let Self::Weapon(v) = self { Some(v.as_mut()) } else { None }
}
}

Loading