diff options
author | Derek Bailey <derekbailey@google.com> | 2023-05-09 22:42:31 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-09 22:42:31 -0700 |
commit | 3e6cd51b6307d680ac8eb6a3f1671fb756228e8b (patch) | |
tree | 7bdfafdbd49ff936d90debb037eae874b1e7f8c0 /src/idl_gen_rust.cpp | |
parent | 10b79d87c19bd2d0f4fd061d623f3b08cb55a1a9 (diff) | |
download | flatbuffers-3e6cd51b6307d680ac8eb6a3f1671fb756228e8b.tar.gz flatbuffers-3e6cd51b6307d680ac8eb6a3f1671fb756228e8b.tar.bz2 flatbuffers-3e6cd51b6307d680ac8eb6a3f1671fb756228e8b.zip |
fixed bfbs gen to pass extra options (#7949)
Diffstat (limited to 'src/idl_gen_rust.cpp')
-rw-r--r-- | src/idl_gen_rust.cpp | 103 |
1 files changed, 66 insertions, 37 deletions
diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index f04e2361..95fa39bd 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -277,10 +277,10 @@ static bool IsBitFlagsEnum(const EnumDef &enum_def) { static bool IsOptionalToBuilder(const FieldDef &field) { return field.IsOptional() || !IsScalar(field.value.type.base_type); } -} // namespace +} // namespace static bool GenerateRustModuleRootFile(const Parser &parser, - const std::string &output_dir) { + const std::string &output_dir) { if (!parser.opts.rust_module_root_file) { // Don't generate a root file when generating one file. This isn't an error // so return true. @@ -708,7 +708,7 @@ class RustGenerator : public BaseGenerator { // and an enum array of values void GenEnum(const EnumDef &enum_def) { const bool is_private = parser_.opts.no_leak_private_annotations && - (enum_def.attributes.Lookup("private") != nullptr); + (enum_def.attributes.Lookup("private") != nullptr); code_.SetValue("ACCESS_TYPE", is_private ? "pub(crate)" : "pub"); code_.SetValue("ENUM_TY", namer_.Type(enum_def)); code_.SetValue("BASE_TYPE", GetEnumTypeForDecl(enum_def.underlying_type)); @@ -842,15 +842,21 @@ class RustGenerator : public BaseGenerator { code_ += " type Inner = Self;"; code_ += " #[inline]"; code_ += " unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {"; - code_ += " let b = flatbuffers::read_scalar_at::<{{BASE_TYPE}}>(buf, loc);"; + code_ += + " let b = flatbuffers::read_scalar_at::<{{BASE_TYPE}}>(buf, loc);"; if (IsBitFlagsEnum(enum_def)) { // Safety: - // This is safe because we know bitflags is implemented with a repr transparent uint of the correct size. - // from_bits_unchecked will be replaced by an equivalent but safe from_bits_retain in bitflags 2.0 + // This is safe because we know bitflags is implemented with a repr + // transparent uint of the correct size. from_bits_unchecked will be + // replaced by an equivalent but safe from_bits_retain in bitflags 2.0 // https://github.com/bitflags/bitflags/issues/262 code_ += " // Safety:"; - code_ += " // This is safe because we know bitflags is implemented with a repr transparent uint of the correct size."; - code_ += " // from_bits_unchecked will be replaced by an equivalent but safe from_bits_retain in bitflags 2.0"; + code_ += + " // This is safe because we know bitflags is implemented with a " + "repr transparent uint of the correct size."; + code_ += + " // from_bits_unchecked will be replaced by an equivalent but " + "safe from_bits_retain in bitflags 2.0"; code_ += " // https://github.com/bitflags/bitflags/issues/262"; code_ += " Self::from_bits_unchecked(b)"; } else { @@ -863,7 +869,9 @@ class RustGenerator : public BaseGenerator { code_ += " type Output = {{ENUM_TY}};"; code_ += " #[inline]"; code_ += " unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {"; - code_ += " flatbuffers::emplace_scalar::<{{BASE_TYPE}}>(dst, {{INTO_BASE}});"; + code_ += + " flatbuffers::emplace_scalar::<{{BASE_TYPE}}>(dst, " + "{{INTO_BASE}});"; code_ += " }"; code_ += "}"; code_ += ""; @@ -879,12 +887,17 @@ class RustGenerator : public BaseGenerator { code_ += " let b = {{BASE_TYPE}}::from_le(v);"; if (IsBitFlagsEnum(enum_def)) { // Safety: - // This is safe because we know bitflags is implemented with a repr transparent uint of the correct size. - // from_bits_unchecked will be replaced by an equivalent but safe from_bits_retain in bitflags 2.0 + // This is safe because we know bitflags is implemented with a repr + // transparent uint of the correct size. from_bits_unchecked will be + // replaced by an equivalent but safe from_bits_retain in bitflags 2.0 // https://github.com/bitflags/bitflags/issues/262 code_ += " // Safety:"; - code_ += " // This is safe because we know bitflags is implemented with a repr transparent uint of the correct size."; - code_ += " // from_bits_unchecked will be replaced by an equivalent but safe from_bits_retain in bitflags 2.0"; + code_ += + " // This is safe because we know bitflags is implemented with a " + "repr transparent uint of the correct size."; + code_ += + " // from_bits_unchecked will be replaced by an equivalent but " + "safe from_bits_retain in bitflags 2.0"; code_ += " // https://github.com/bitflags/bitflags/issues/262"; code_ += " unsafe { Self::from_bits_unchecked(b) }"; } else { @@ -1458,7 +1471,8 @@ class RustGenerator : public BaseGenerator { case ftVectorOfBool: case ftVectorOfFloat: { const auto typname = GetTypeBasic(type.VectorType()); - return WrapOption("flatbuffers::Vector<" + lifetime + ", " + typname + ">"); + return WrapOption("flatbuffers::Vector<" + lifetime + ", " + typname + + ">"); } case ftVectorOfEnumKey: { const auto typname = WrapInNameSpace(*type.enum_def); @@ -1467,7 +1481,8 @@ class RustGenerator : public BaseGenerator { } case ftVectorOfStruct: { const auto typname = WrapInNameSpace(*type.struct_def); - return WrapOption("flatbuffers::Vector<" + lifetime + ", " + typname + ">"); + return WrapOption("flatbuffers::Vector<" + lifetime + ", " + typname + + ">"); } case ftVectorOfTable: { const auto typname = WrapInNameSpace(*type.struct_def); @@ -1585,8 +1600,9 @@ class RustGenerator : public BaseGenerator { : "None"; const std::string unwrap = field.IsOptional() ? "" : ".unwrap()"; - return "unsafe { self._tab.get::<" + typname + ">({{STRUCT_TY}}::" + vt_offset + - ", " + default_value + ")" + unwrap + "}"; + return "unsafe { self._tab.get::<" + typname + + ">({{STRUCT_TY}}::" + vt_offset + ", " + default_value + ")" + + unwrap + "}"; } // Generates a fully-qualified name getter for use with --gen-name-strings @@ -1646,8 +1662,8 @@ class RustGenerator : public BaseGenerator { // Generate an accessor struct, builder struct, and create function for a // table. void GenTable(const StructDef &struct_def) { - - const bool is_private = parser_.opts.no_leak_private_annotations && + const bool is_private = + parser_.opts.no_leak_private_annotations && (struct_def.attributes.Lookup("private") != nullptr); code_.SetValue("ACCESS_TYPE", is_private ? "pub(crate)" : "pub"); code_.SetValue("STRUCT_TY", namer_.Type(struct_def)); @@ -1933,13 +1949,17 @@ class RustGenerator : public BaseGenerator { code_ += " // Safety:"; code_ += " // Created from a valid Table for this object"; code_ += " // Which contains a valid union in this slot"; - code_ += " Some(unsafe { {{U_ELEMENT_TABLE_TYPE}}::init_from_table(u) })"; + code_ += + " Some(unsafe { " + "{{U_ELEMENT_TABLE_TYPE}}::init_from_table(u) })"; } else { - code_ +=" self.{{FIELD}}().map(|t| {"; + code_ += " self.{{FIELD}}().map(|t| {"; code_ += " // Safety:"; code_ += " // Created from a valid Table for this object"; code_ += " // Which contains a valid union in this slot"; - code_ += " unsafe { {{U_ELEMENT_TABLE_TYPE}}::init_from_table(t) }"; + code_ += + " unsafe { {{U_ELEMENT_TABLE_TYPE}}::init_from_table(t) " + "}"; code_ += " })"; } code_ += " } else {"; @@ -2264,7 +2284,8 @@ class RustGenerator : public BaseGenerator { case ftUnionValue: { code_.SetValue("ENUM_METHOD", namer_.Method(*field.value.type.enum_def)); - code_.SetValue("DISCRIMINANT", namer_.LegacyRustUnionTypeMethod(field)); + code_.SetValue("DISCRIMINANT", + namer_.LegacyRustUnionTypeMethod(field)); code_ += " let {{DISCRIMINANT}} = " "self.{{FIELD}}.{{ENUM_METHOD}}_type();"; @@ -2312,10 +2333,10 @@ class RustGenerator : public BaseGenerator { // TODO(cneo): create_vector* should be more generic to avoid // allocations. - MapNativeTableField( - field, - "let w: Vec<_> = x.iter().map(|s| _fbb.create_string(s)).collect();" - "_fbb.create_vector(&w)"); + MapNativeTableField(field, + "let w: Vec<_> = x.iter().map(|s| " + "_fbb.create_string(s)).collect();" + "_fbb.create_vector(&w)"); return; } case ftVectorOfTable: { @@ -2601,7 +2622,8 @@ class RustGenerator : public BaseGenerator { } // Generate an accessor struct with constructor for a flatbuffers struct. void GenStruct(const StructDef &struct_def) { - const bool is_private = parser_.opts.no_leak_private_annotations && + const bool is_private = + parser_.opts.no_leak_private_annotations && (struct_def.attributes.Lookup("private") != nullptr); code_.SetValue("ACCESS_TYPE", is_private ? "pub(crate)" : "pub"); // Generates manual padding and alignment. @@ -2665,7 +2687,9 @@ class RustGenerator : public BaseGenerator { code_ += " type Output = {{STRUCT_TY}};"; code_ += " #[inline]"; code_ += " unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {"; - code_ += " let src = ::core::slice::from_raw_parts(self as *const {{STRUCT_TY}} as *const u8, Self::size());"; + code_ += + " let src = ::core::slice::from_raw_parts(self as *const " + "{{STRUCT_TY}} as *const u8, Self::size());"; code_ += " dst.copy_from_slice(src);"; code_ += " }"; code_ += "}"; @@ -2759,7 +2783,9 @@ class RustGenerator : public BaseGenerator { code_ += " // Safety:"; code_ += " // Created from a valid Table for this object"; code_ += " // Which contains a valid array in this slot"; - code_ += " unsafe { flatbuffers::Array::follow(&self.0, {{FIELD_OFFSET}}) }"; + code_ += + " unsafe { flatbuffers::Array::follow(&self.0, {{FIELD_OFFSET}}) " + "}"; } else { code_ += "pub fn {{FIELD}}(&self) -> {{FIELD_TYPE}} {"; code_ += @@ -2772,7 +2798,9 @@ class RustGenerator : public BaseGenerator { code_ += " core::ptr::copy_nonoverlapping("; code_ += " self.0[{{FIELD_OFFSET}}..].as_ptr(),"; code_ += " mem.as_mut_ptr() as *mut u8,"; - code_ += " core::mem::size_of::<<{{FIELD_TYPE}} as EndianScalar>::Scalar>(),"; + code_ += + " core::mem::size_of::<<{{FIELD_TYPE}} as " + "EndianScalar>::Scalar>(),"; code_ += " );"; code_ += " mem.assume_init()"; code_ += " })"; @@ -2827,7 +2855,9 @@ class RustGenerator : public BaseGenerator { code_ += " core::ptr::copy_nonoverlapping("; code_ += " &x_le as *const _ as *const u8,"; code_ += " self.0[{{FIELD_OFFSET}}..].as_mut_ptr(),"; - code_ += " core::mem::size_of::<<{{FIELD_TYPE}} as EndianScalar>::Scalar>(),"; + code_ += + " core::mem::size_of::<<{{FIELD_TYPE}} as " + "EndianScalar>::Scalar>(),"; code_ += " );"; code_ += " }"; } @@ -2990,13 +3020,13 @@ class RustGenerator : public BaseGenerator { } // namespace rust static bool GenerateRust(const Parser &parser, const std::string &path, - const std::string &file_name) { + const std::string &file_name) { rust::RustGenerator generator(parser, path, file_name); return generator.generate(); } static std::string RustMakeRule(const Parser &parser, const std::string &path, - const std::string &file_name) { + const std::string &file_name) { std::string filebase = flatbuffers::StripPath(flatbuffers::StripExtension(file_name)); rust::RustGenerator generator(parser, path, file_name); @@ -3020,9 +3050,8 @@ class RustCodeGenerator : public CodeGenerator { return Status::OK; } - Status GenerateCode(const uint8_t *buffer, int64_t length) override { - (void)buffer; - (void)length; + Status GenerateCode(const uint8_t *, int64_t, + const CodeGenOptions &) override { return Status::NOT_IMPLEMENTED; } |