Skip to content

Improves Rust code generation #8564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion rust/flatbuffers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ homepage = "https://google.github.io/flatbuffers/"
repository = "https://github.com/google/flatbuffers"
keywords = ["flatbuffers", "serialization", "zero-copy"]
categories = ["encoding", "data-structures", "memory-management"]
rust = "1.51"
rust-version = "1.51"

[features]
default = ["std"]
Expand Down
3 changes: 2 additions & 1 deletion rust/flatbuffers/build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use rustc_version::{version_meta, Channel};
use rustc_version::{Channel, version_meta};

fn main() {
let version_meta = version_meta().unwrap();

// To use nightly features we declare this and then we can use
// #[cfg(nightly)]
// for nightly only features
println!("cargo:rustc-check-cfg=cfg(nightly)");
if version_meta.channel == Channel::Nightly {
println!("cargo:rustc-cfg=nightly")
}
Expand Down
2 changes: 1 addition & 1 deletion rust/flexbuffers/src/builder/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl<'a> ser::Serializer for &'a mut FlexbufferSerializer {
type Ok = ();
type Error = Error;
fn is_human_readable(&self) -> bool {
cfg!(serialize_human_readable)
cfg!(feature = "serialize_human_readable")
}
fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
self.builder.push(v);
Expand Down
2 changes: 1 addition & 1 deletion rust/flexbuffers/src/reader/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl<'de> VariantAccess<'de> for Reader<&'de [u8]> {
impl<'de> Deserializer<'de> for Reader<&'de [u8]> {
type Error = DeserializationError;
fn is_human_readable(&self) -> bool {
cfg!(deserialize_human_readable)
cfg!(feature = "deserialize_human_readable")
}

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
Expand Down
2 changes: 1 addition & 1 deletion rust/flexbuffers/src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod serialize;
mod vector;
pub use de::DeserializationError;
pub use iter::ReaderIterator;
pub use map::{MapReader, MapReaderIndexer};
pub use map::MapReader;
pub use vector::VectorReader;

/// All the possible errors when reading a flexbuffer.
Expand Down
30 changes: 11 additions & 19 deletions samples/rust_generated/my_game/sample/color_generated.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
// @generated
extern crate alloc;
extern crate flatbuffers;
use alloc::boxed::Box;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::mem;
use core::cmp::Ordering;
use self::flatbuffers::{EndianScalar, Follow};
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;
Expand Down Expand Up @@ -47,33 +40,33 @@ impl Color {
}
}
}
impl core::fmt::Debug for Color {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
impl ::core::fmt::Debug for Color {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::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 Color {
impl<'a> ::flatbuffers::Follow<'a> for Color {
type Inner = Self;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = flatbuffers::read_scalar_at::<i8>(buf, loc);
let b = ::flatbuffers::read_scalar_at::<i8>(buf, loc);
Self(b)
}
}

impl flatbuffers::Push for Color {
impl ::flatbuffers::Push for Color {
type Output = Color;
#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
flatbuffers::emplace_scalar::<i8>(dst, self.0);
::flatbuffers::emplace_scalar::<i8>(dst, self.0);
}
}

impl flatbuffers::EndianScalar for Color {
impl ::flatbuffers::EndianScalar for Color {
type Scalar = i8;
#[inline]
fn to_little_endian(self) -> i8 {
Expand All @@ -87,14 +80,13 @@ impl flatbuffers::EndianScalar for Color {
}
}

impl<'a> flatbuffers::Verifiable for Color {
impl<'a> ::flatbuffers::Verifiable for Color {
#[inline]
fn run_verifier(
v: &mut flatbuffers::Verifier, pos: usize
) -> Result<(), flatbuffers::InvalidFlatbuffer> {
use self::flatbuffers::Verifiable;
v: &mut ::flatbuffers::Verifier, pos: usize
) -> Result<(), ::flatbuffers::InvalidFlatbuffer> {
i8::run_verifier(v, pos)
}
}

impl flatbuffers::SimpleToVerifyInSlice for Color {}
impl ::flatbuffers::SimpleToVerifyInSlice for Color {}
38 changes: 15 additions & 23 deletions samples/rust_generated/my_game/sample/equipment_generated.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
// @generated
extern crate alloc;
extern crate flatbuffers;
use alloc::boxed::Box;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::mem;
use core::cmp::Ordering;
use self::flatbuffers::{EndianScalar, Follow};
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;
Expand Down Expand Up @@ -43,33 +36,33 @@ impl Equipment {
}
}
}
impl core::fmt::Debug for Equipment {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
impl ::core::fmt::Debug for Equipment {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::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 Equipment {
impl<'a> ::flatbuffers::Follow<'a> for Equipment {
type Inner = Self;
#[inline]
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = flatbuffers::read_scalar_at::<u8>(buf, loc);
let b = ::flatbuffers::read_scalar_at::<u8>(buf, loc);
Self(b)
}
}

impl flatbuffers::Push for Equipment {
impl ::flatbuffers::Push for Equipment {
type Output = Equipment;
#[inline]
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
flatbuffers::emplace_scalar::<u8>(dst, self.0);
::flatbuffers::emplace_scalar::<u8>(dst, self.0);
}
}

impl flatbuffers::EndianScalar for Equipment {
impl ::flatbuffers::EndianScalar for Equipment {
type Scalar = u8;
#[inline]
fn to_little_endian(self) -> u8 {
Expand All @@ -83,25 +76,24 @@ impl flatbuffers::EndianScalar for Equipment {
}
}

impl<'a> flatbuffers::Verifiable for Equipment {
impl<'a> ::flatbuffers::Verifiable for Equipment {
#[inline]
fn run_verifier(
v: &mut flatbuffers::Verifier, pos: usize
) -> Result<(), flatbuffers::InvalidFlatbuffer> {
use self::flatbuffers::Verifiable;
v: &mut ::flatbuffers::Verifier, pos: usize
) -> Result<(), ::flatbuffers::InvalidFlatbuffer> {
u8::run_verifier(v, pos)
}
}

impl flatbuffers::SimpleToVerifyInSlice for Equipment {}
impl ::flatbuffers::SimpleToVerifyInSlice for Equipment {}
pub struct EquipmentUnionTableOffset {}

#[allow(clippy::upper_case_acronyms)]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum EquipmentT {
NONE,
Weapon(Box<WeaponT>),
Weapon(alloc::boxed::Box<WeaponT>),
}
impl Default for EquipmentT {
fn default() -> Self {
Expand All @@ -115,16 +107,16 @@ impl EquipmentT {
Self::Weapon(_) => Equipment::Weapon,
}
}
pub fn pack<'b, A: flatbuffers::Allocator + 'b>(&self, fbb: &mut flatbuffers::FlatBufferBuilder<'b, A>) -> Option<flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>> {
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<Box<WeaponT>> {
pub fn take_weapon(&mut self) -> Option<alloc::boxed::Box<WeaponT>> {
if let Self::Weapon(_) = self {
let v = core::mem::replace(self, Self::NONE);
let v = ::core::mem::replace(self, Self::NONE);
if let Self::Weapon(w) = v {
Some(w)
} else {
Expand Down
Loading
Loading