diff --git a/samples/monster_generated.swift b/samples/monster_generated.swift index ebbc008ef..395f3d994 100644 --- a/samples/monster_generated.swift +++ b/samples/monster_generated.swift @@ -82,10 +82,7 @@ public struct MyGame_Sample_Vec3: NativeStruct, FlatbuffersVectorInitializable, private var _z: Float32 public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _x = _accessor.readBuffer(of: Float32.self, at: 0) - _y = _accessor.readBuffer(of: Float32.self, at: 4) - _z = _accessor.readBuffer(of: Float32.self, at: 8) + self = bb.read(def: Self.self, position: Int(o)) } public init(x: Float32, y: Float32, z: Float32) { @@ -100,7 +97,7 @@ public struct MyGame_Sample_Vec3: NativeStruct, FlatbuffersVectorInitializable, _z = 0.0 } - public init(_ _t: inout MyGame_Sample_Vec3_Mutable) { + public init(_ _t: borrowing MyGame_Sample_Vec3_Mutable) { _x = _t.x _y = _t.y _z = _t.z @@ -150,10 +147,9 @@ public struct MyGame_Sample_Vec3_Mutable: FlatBufferStruct, FlatbuffersVectorIni @discardableResult public func mutate(y: Float32) -> Bool { return _accessor.mutate(y, index: 4) } public var z: Float32 { return _accessor.readBuffer(of: Float32.self, at: 8) } @discardableResult public func mutate(z: Float32) -> Bool { return _accessor.mutate(z, index: 8) } - - public mutating func unpack() -> MyGame_Sample_Vec3 { - return MyGame_Sample_Vec3(&self) + public func unpack() -> MyGame_Sample_Vec3 { + return MyGame_Sample_Vec3(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Sample_Vec3?) -> Offset { guard var obj = obj else { return Offset() } @@ -249,10 +245,9 @@ public struct MyGame_Sample_Monster: FlatBufferTable, FlatbuffersVectorInitializ MyGame_Sample_Monster.addVectorOf(path: path, &fbb) return MyGame_Sample_Monster.endMonster(&fbb, start: __start) } - - public mutating func unpack() -> MyGame_Sample_MonsterT { - return MyGame_Sample_MonsterT(&self) + public func unpack() -> MyGame_Sample_MonsterT { + return MyGame_Sample_MonsterT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Sample_MonsterT?) -> Offset { guard var obj = obj else { return Offset() } @@ -372,7 +367,7 @@ public class MyGame_Sample_MonsterT: NativeObject { public var equipped: MyGame_Sample_EquipmentUnion? public var path: [MyGame_Sample_Vec3] - public init(_ _t: inout MyGame_Sample_Monster) { + public init(_ _t: borrowing MyGame_Sample_Monster) { pos = _t.pos mana = _t.mana hp = _t.hp @@ -381,12 +376,12 @@ public class MyGame_Sample_MonsterT: NativeObject { inventory.append(contentsOf: _t.inventory) color = _t.color weapons = [] - for var val in _t.weapons{ + for val in _t.weapons{ weapons.append(val.unpack()) } switch _t.equippedType { case .weapon: - var _v = _t.equipped(type: MyGame_Sample_Weapon.self) + let _v = _t.equipped(type: MyGame_Sample_Weapon.self) equipped = MyGame_Sample_EquipmentUnion(_v?.unpack(), type: .weapon) default: break } @@ -441,10 +436,9 @@ public struct MyGame_Sample_Weapon: FlatBufferTable, FlatbuffersVectorInitializa MyGame_Sample_Weapon.add(damage: damage, &fbb) return MyGame_Sample_Weapon.endWeapon(&fbb, start: __start) } - - public mutating func unpack() -> MyGame_Sample_WeaponT { - return MyGame_Sample_WeaponT(&self) + public func unpack() -> MyGame_Sample_WeaponT { + return MyGame_Sample_WeaponT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Sample_WeaponT?) -> Offset { guard var obj = obj else { return Offset() } @@ -493,7 +487,7 @@ public class MyGame_Sample_WeaponT: NativeObject { public var name: String? public var damage: Int16 - public init(_ _t: inout MyGame_Sample_Weapon) { + public init(_ _t: borrowing MyGame_Sample_Weapon) { name = _t.name damage = _t.damage } diff --git a/scripts/generate_code.py b/scripts/generate_code.py index e2a71884b..c1a4448c0 100755 --- a/scripts/generate_code.py +++ b/scripts/generate_code.py @@ -451,6 +451,13 @@ flatc( include="include_test", prefix=swift_prefix, ) + +flatc( + SWIFT_OPTS + BASE_OPTS, + schema="arrays_test.fbs", + prefix=swift_prefix, +) + flatc( SWIFT_OPTS + BASE_OPTS, schema="union_vector/union_vector.fbs", diff --git a/src/idl_gen_swift.cpp b/src/idl_gen_swift.cpp index 334563bdc..e0e231978 100644 --- a/src/idl_gen_swift.cpp +++ b/src/idl_gen_swift.cpp @@ -167,6 +167,7 @@ class SwiftGenerator : public BaseGenerator { code_ += "// " + std::string(FlatBuffersGeneratedWarning()); code_ += "// swiftlint:disable all"; code_ += "// swiftformat:disable all\n"; + if (parser_.opts.include_dependence_headers || parser_.opts.generate_all) { code_.SetValue("IMPLEMENTONLY", parser_.opts.swift_implementation_only ? "@_implementationOnly " @@ -178,6 +179,22 @@ class SwiftGenerator : public BaseGenerator { code_ += "{{IMPLEMENTONLY}}import FlatBuffers\n"; } + if (parser_.advanced_features_ == reflection::AdvancedArrayFeatures) { + code_ += "#if compiler(>=6.2)"; + } + + GenerateCode(); + + if (parser_.advanced_features_ == reflection::AdvancedArrayFeatures) { + code_ += "#endif"; + } + + const auto filename = GeneratedFileName(path_, file_name_, parser_.opts); + const auto final_code = code_.ToString(); + return SaveFile(filename.c_str(), final_code, false); + } + + void GenerateCode() { // Generate code for all the enum declarations. for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); ++it) { @@ -206,10 +223,6 @@ class SwiftGenerator : public BaseGenerator { } } } - - const auto filename = GeneratedFileName(path_, file_name_, parser_.opts); - const auto final_code = code_.ToString(); - return SaveFile(filename.c_str(), final_code, false); } void mark(const std::string& str) { @@ -227,6 +240,7 @@ class SwiftGenerator : public BaseGenerator { code_.SetValue("ACCESS_TYPE", is_private_access ? "internal" : "public"); GenComment(struct_def.doc_comment); code_.SetValue("STRUCTNAME", namer_.NamespacedType(struct_def)); + GenOSVersionChecks(); code_ += "{{ACCESS_TYPE}} struct {{STRUCTNAME}}: NativeStruct, " "FlatbuffersVectorInitializable, Verifiable, " @@ -250,31 +264,69 @@ class SwiftGenerator : public BaseGenerator { if (!constructor.empty()) constructor += ", "; const auto field_var = namer_.Variable(field); - const auto type = GenType(field.value.type); code_.SetValue("FIELDVAR", field_var); + const auto type = GenType(field.value.type); if (IsEnum(field.value.type)) { code_.SetValue("BASEVALUE", GenTypeBasic(field.value.type, false)); } code_.SetValue("VALUETYPE", type); GenComment(field.doc_comment); - std::string valueType = - IsEnum(field.value.type) ? "{{BASEVALUE}}" : "{{VALUETYPE}}"; - code_ += "private var _{{FIELDVAR}}: " + valueType; - const auto accessing_value = IsEnum(field.value.type) ? ".value" : ""; - const auto base_value = - IsStruct(field.value.type) ? (type + "()") : SwiftConstant(field); - main_constructor.push_back("_" + field_var + " = " + field_var + - accessing_value); - base_constructor.push_back("_" + field_var + " = " + base_value); + if (IsArray(field.value.type)) { + std::string valueType = IsEnum(field.value.type.VectorType()) + ? "{{BASEVALUE}}" + : "{{VALUETYPE}}"; + const auto fixed_length = + NumToString(field.value.type.VectorType().fixed_length); + code_.SetValue("FIXEDLENGTH", fixed_length); - if (field.padding) { - GenPadding(field, &padding_id); + const auto vector_base_type = IsStruct(field.value.type.VectorType()) + ? (type + "()") + : SwiftConstant(field); + code_ += "private var _{{FIELDVAR}}: InlineArray<{{FIXEDLENGTH}}, " + + valueType + ">"; + + main_constructor.push_back("_" + field_var + " = " + field_var); + base_constructor.push_back( + "_" + field_var + " = InlineArray(repeating: " + vector_base_type + + ")"); + + if (field.padding) { + GenPadding(field, &padding_id); + } + constructor += field_var + ": " + "InlineArray<" + fixed_length + ", "; + if (IsEnum(field.value.type.VectorType())) { + constructor += + GenTypeBasic(field.value.type.VectorType(), false) + ">"; + } else { + constructor += type + ">"; + } + } else { + const auto accessing_value = IsEnum(field.value.type) ? ".value" : ""; + const auto base_value = + IsStruct(field.value.type) ? (type + "()") : SwiftConstant(field); + + std::string valueType = + IsEnum(field.value.type) ? "{{BASEVALUE}}" : "{{VALUETYPE}}"; + code_ += "private var _{{FIELDVAR}}: " + valueType; + + main_constructor.push_back("_" + field_var + " = " + field_var + + accessing_value); + base_constructor.push_back("_" + field_var + " = " + base_value); + + if (field.padding) { + GenPadding(field, &padding_id); + } + constructor += field_var + ": " + type; } - constructor += field_var + ": " + type; } code_ += ""; - BuildStructConstructor(struct_def); + code_ += "{{ACCESS_TYPE}} init(_ bb: ByteBuffer, o: Int32) {"; + Indent(); + code_ += "self = bb.read(def: Self.self, position: Int(o))"; + Outdent(); + code_ += "}\n"; + BuildObjectConstructor(main_constructor, constructor); BuildObjectConstructor(base_constructor, ""); @@ -288,7 +340,21 @@ class SwiftGenerator : public BaseGenerator { code_.SetValue("FIELDVAR", namer_.Variable(field)); code_.SetValue("VALUETYPE", GenType(field.value.type)); GenComment(field.doc_comment); - if (!IsEnum(field.value.type)) { + if (IsArray(field.value.type)) { + const auto fixed_length = + NumToString(field.value.type.VectorType().fixed_length); + code_.SetValue("FIXEDLENGTH", fixed_length); + if (IsEnum(field.value.type.VectorType())) { + code_ += + "{{ACCESS_TYPE}} var {{FIELDVAR}}: InlineArray<{{FIXEDLENGTH}}, " + "{{VALUETYPE}}> { InlineArray { {{VALUETYPE}}(rawValue: " + "_{{FIELDVAR}}[$0])! } }"; + } else { + code_ += + "{{ACCESS_TYPE}} var {{FIELDVAR}}: InlineArray<{{FIXEDLENGTH}}, " + "{{VALUETYPE}}> { _{{FIELDVAR}} }"; + } + } else if (!IsEnum(field.value.type)) { code_ += GenReaderMainBody() + "_{{FIELDVAR}} }"; } else if (IsEnum(field.value.type)) { code_ += @@ -310,34 +376,6 @@ class SwiftGenerator : public BaseGenerator { if (parser_.opts.gen_json_coders) GenerateJSONEncodingAPIs(struct_def); } - void BuildStructConstructor(const StructDef& struct_def) { - code_ += "{{ACCESS_TYPE}} init(_ bb: ByteBuffer, o: Int32) {"; - Indent(); - code_ += "let {{ACCESS}} = Struct(bb: bb, position: o)"; - for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); ++it) { - const auto& field = **it; - if (field.deprecated) continue; - const auto type = field.value.type; - code_.SetValue("FIELDVAR", namer_.Variable(field)); - code_.SetValue("VALUETYPE", GenType(type)); - code_.SetValue("OFFSET", NumToString(field.value.offset)); - if (IsScalar(type.base_type)) { - if (IsEnum(type)) - code_.SetValue("VALUETYPE", GenTypeBasic(field.value.type, false)); - code_ += - "_{{FIELDVAR}} = {{ACCESS}}.readBuffer(of: {{VALUETYPE}}.self, " - "at: {{OFFSET}})"; - } else { - code_ += - "_{{FIELDVAR}} = {{VALUETYPE}}({{ACCESS}}.bb, o: " - "{{ACCESS}}.position + {{OFFSET}})"; - } - } - Outdent(); - code_ += "}\n"; - } - void GenMutableStructReader(const StructDef& struct_def) { GenObjectHeader(struct_def); @@ -353,7 +391,26 @@ class SwiftGenerator : public BaseGenerator { } code_.SetValue("VALUETYPE", type); code_.SetValue("OFFSET", offset); - if (IsScalar(field.value.type.base_type) && !IsEnum(field.value.type)) { + + if (IsArray(field.value.type)) { + code_.SetValue("OFFSET_VALUE", NumToString(field.value.offset)); + code_.SetValue("SIZE", + NumToString(InlineSize(field.value.type.VectorType()))); + code_.SetValue("MUTABLE", IsStruct(field.value.type.VectorType()) + ? Mutable() + : ""); + const auto fixed_length = + NumToString(field.value.type.VectorType().fixed_length); + code_.SetValue("FIXEDLENGTH", fixed_length); + + code_ += + "{{ACCESS_TYPE}} var {{FIELDVAR}}: " + "FlatbufferVector<{{VALUETYPE}}{{MUTABLE}}> " + "{ return {{ACCESS}}.vector(at: {{OFFSET_VALUE}}, count: " + "{{FIXEDLENGTH}}, size: {{SIZE}}) " + "}"; + } else if (IsScalar(field.value.type.base_type) && + !IsEnum(field.value.type)) { code_ += GenReaderMainBody() + "return " + GenReader("VALUETYPE") + " }"; } else if (IsEnum(field.value.type)) { @@ -365,13 +422,32 @@ class SwiftGenerator : public BaseGenerator { code_.SetValue("VALUETYPE", GenType(field.value.type) + Mutable()); code_ += GenReaderMainBody() + "return " + GenConstructor("{{ACCESS}}.position + {{OFFSET}}"); + } else if (IsVector(field.value.type.base_type)) { + code_.SetValue("VALUETYPE", GenType(field.value.type) + Mutable()); + code_ += GenReaderMainBody() + "return " + + GenConstructor("{{ACCESS}}.position + {{OFFSET}}"); + } + + if (parser_.opts.mutable_buffer) { + if (!IsStruct(field.value.type) && !IsArray(field.value.type)) { + code_ += GenMutate("{{OFFSET}}", "", IsEnum(field.value.type)); + } else if (IsArray(field.value.type) && + !IsStruct(field.value.type.VectorType())) { + code_.SetValue("IS_RAW", IsEnum(field.value.type.VectorType()) + ? ".rawValue" + : ""); + code_ += + "@discardableResult {{ACCESS_TYPE}} func mutate({{FIELDVAR}}: " + "{{VALUETYPE}}, at index: Int32) -> Bool { " + "return {{ACCESS}}.mutate({{FIELDVAR}}{{IS_RAW}}, index: " + "{{OFFSET_VALUE}} + (index * {{SIZE}})) }"; + } } - if (parser_.opts.mutable_buffer && !IsStruct(field.value.type)) - code_ += GenMutate("{{OFFSET}}", "", IsEnum(field.value.type)); } if (parser_.opts.generate_object_based_api) { - GenerateObjectAPIExtensionHeader(namer_.NamespacedType(struct_def)); + GenerateObjectAPIExtensionHeader(namer_.NamespacedType(struct_def), + struct_def.fixed); code_ += "return builder.create(struct: obj)"; Outdent(); code_ += "}"; @@ -467,6 +543,8 @@ class SwiftGenerator : public BaseGenerator { code_.SetValue("STRUCTNAME", namer_.NamespacedType(struct_def)); code_.SetValue("OBJECTTYPE", struct_def.fixed ? "Struct" : "Table"); code_.SetValue("MUTABLE", struct_def.fixed ? Mutable() : ""); + + GenOSVersionChecks(); code_ += "{{ACCESS_TYPE}} struct {{STRUCTNAME}}{{MUTABLE}}: " "FlatBuffer{{OBJECTTYPE}}, FlatbuffersVectorInitializable\\"; @@ -986,6 +1064,16 @@ class SwiftGenerator : public BaseGenerator { if (IsUnion(type) && !IsEnum(type)) { GenerateEncoderUnionBody(field); + } else if (IsArray(type)) { + code_ += + "var {{FIELDVAR}}Container = " + "container.nestedUnkeyedContainer(forKey: .{{FIELDVAR}})"; + code_ += + "for index in {{FIELDVAR}}.startIndex..<{{FIELDVAR}}.endIndex {"; + Indent(); + code_ += "try {{FIELDVAR}}Container.encode({{FIELDVAR}}[index])"; + Outdent(); + code_ += "}"; } else { code_ += "try container.encodeIfPresent({{FIELDVAR}}, forKey: " @@ -1000,6 +1088,7 @@ class SwiftGenerator : public BaseGenerator { } void GenerateJSONEncodingAPIs(const StructDef& struct_def) { + GenOSVersionChecks(); code_ += "extension {{STRUCTNAME}}: Encodable {"; Indent(); code_ += ""; @@ -1228,11 +1317,13 @@ class SwiftGenerator : public BaseGenerator { // MARK: - Object API - void GenerateObjectAPIExtensionHeader(std::string type_name) { - code_ += "\n"; - code_ += "{{ACCESS_TYPE}} mutating func unpack() -> " + type_name + " {"; + void GenerateObjectAPIExtensionHeader(std::string type_name, + const bool is_fixed_struct) { + // code_.SetValue("MUTATING", is_fixed_struct ? "" : " mutating"); + code_ += ""; + code_ += "{{ACCESS_TYPE}} func unpack() -> " + type_name + " {"; Indent(); - code_ += "return " + type_name + "(&self)"; + code_ += "return " + type_name + (is_fixed_struct ? "(self)" : "(self)"); Outdent(); code_ += "}"; code_ += @@ -1255,8 +1346,7 @@ class SwiftGenerator : public BaseGenerator { } void GenerateObjectAPIStructConstructor(const StructDef& struct_def) { - code_ += - "{{ACCESS_TYPE}} init(_ _t: inout {{STRUCTNAME}}" + Mutable() + ") {"; + code_ += "{{ACCESS_TYPE}} init(_ _t: borrowing {{STRUCTNAME}}" + Mutable() + ") {"; Indent(); for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { @@ -1265,8 +1355,18 @@ class SwiftGenerator : public BaseGenerator { const auto type = GenType(field.value.type); code_.SetValue("FIELDVAR", namer_.Variable(field)); - if (IsStruct(field.value.type)) { - code_ += "var _v{{FIELDVAR}} = _t.{{FIELDVAR}}"; + if (IsArray(field.value.type)) { + code_.SetValue( + "RAW_VALUE", + IsStruct(field.value.type.VectorType()) + ? ".unpack()" + : (IsEnum(field.value.type.VectorType()) ? ".rawValue" : "")); + code_ += "let _v{{FIELDVAR}} = _t.{{FIELDVAR}}"; + code_ += + "_{{FIELDVAR}} = InlineArray { _v{{FIELDVAR}}[$0]{{RAW_VALUE}} }"; + continue; + } else if (IsStruct(field.value.type)) { + code_ += "let _v{{FIELDVAR}} = _t.{{FIELDVAR}}"; code_ += "_{{FIELDVAR}} = _v{{FIELDVAR}}.unpack()"; continue; } @@ -1278,6 +1378,7 @@ class SwiftGenerator : public BaseGenerator { } void GenObjectAPI(const StructDef& struct_def) { + GenOSVersionChecks(); code_ += "{{ACCESS_TYPE}} class " + namer_.NamespacedObjectType(struct_def) + ": NativeObject {\n"; std::vector buffer_constructor; @@ -1292,7 +1393,7 @@ class SwiftGenerator : public BaseGenerator { } code_ += ""; BuildObjectConstructor(buffer_constructor, - "_ _t: inout " + namer_.NamespacedType(struct_def)); + "_ _t: borrowing " + namer_.NamespacedType(struct_def)); BuildObjectConstructor(base_constructor); if (!struct_def.fixed) code_ += @@ -1304,7 +1405,8 @@ class SwiftGenerator : public BaseGenerator { } void GenerateObjectAPITableExtension(const StructDef& struct_def) { - GenerateObjectAPIExtensionHeader(namer_.NamespacedObjectType(struct_def)); + GenerateObjectAPIExtensionHeader(namer_.NamespacedObjectType(struct_def), + struct_def.fixed); std::vector unpack_body; std::string builder = ", &builder)"; for (auto it = struct_def.fields.vec.begin(); @@ -1502,10 +1604,8 @@ class SwiftGenerator : public BaseGenerator { if (field.value.type.struct_def->fixed) { buffer_constructor.push_back("" + field_var + " = _t." + field_field); } else { - buffer_constructor.push_back("var __" + field_var + " = _t." + - field_field); buffer_constructor.push_back( - "" + field_var + " = __" + field_var + + "" + field_var + " = _t." + field_var + (field.IsRequired() ? "!" : question_mark) + ".unpack()"); } break; @@ -1592,8 +1692,7 @@ class SwiftGenerator : public BaseGenerator { code_ += "{{ACCESS_TYPE}} var {{FIELDVAR}}: [{{VALUETYPE}}{{OPTIONAL}}]"; if (!vectortype.struct_def->fixed) { - buffer_constructor.push_back("for var val in _t." + field_field + - "{"); + buffer_constructor.push_back("for val in _t." + field_field + "{"); buffer_constructor.push_back(indentation + field_var + ".append(val.unpack())"); buffer_constructor.push_back("}"); @@ -1683,13 +1782,13 @@ class SwiftGenerator : public BaseGenerator { const auto constructor = ns_type + "Union(_v?.unpack(), type: ." + variant + ")"; if (is_vector) { - buffer_constructor.push_back(indentation + " var _v = _t." + field + + buffer_constructor.push_back(indentation + " let _v = _t." + field + "(at: Int32(index), type: " + type + ".self)"); buffer_constructor.push_back(indentation + " " + field + ".append(" + constructor + ")"); } else { - buffer_constructor.push_back(indentation + " var _v = _t." + field + + buffer_constructor.push_back(indentation + " let _v = _t." + field + "(" + "type: " + type + ".self)"); buffer_constructor.push_back(indentation + " " + field + " = " + constructor); @@ -1765,7 +1864,7 @@ class SwiftGenerator : public BaseGenerator { for (int i = 0; i < 4; i++) { if (static_cast(field.padding) & (1 << i)) { const auto bits = (1 << i) * 8; - code_ += "private let padding" + NumToString((*id)++) + "__: UInt" + + code_ += "private var padding" + NumToString((*id)++) + "__: UInt" + NumToString(bits) + " = 0"; } } @@ -1783,6 +1882,12 @@ class SwiftGenerator : public BaseGenerator { } } + void GenOSVersionChecks() { + if (parser_.advanced_features_ == reflection::AdvancedArrayFeatures) { + code_ += "@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *)"; + } + } + std::string GenOffset() { return "let o = {{ACCESS}}.offset({{TABLEOFFSET}}.{{OFFSET}}.v); "; } diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 42df4cde7..c0f96ef0b 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -2758,7 +2758,8 @@ bool Parser::SupportsAdvancedArrayFeatures() const { return (opts.lang_to_generate & ~(IDLOptions::kCpp | IDLOptions::kPython | IDLOptions::kJava | IDLOptions::kCSharp | IDLOptions::kJsonSchema | IDLOptions::kJson | - IDLOptions::kBinary | IDLOptions::kRust | IDLOptions::kTs)) == 0; + IDLOptions::kBinary | IDLOptions::kRust | IDLOptions::kTs | + IDLOptions::kSwift)) == 0; } bool Parser::Supports64BitOffsets() const { diff --git a/swift/Sources/FlatBuffers/FlatBufferObject.swift b/swift/Sources/FlatBuffers/FlatBufferObject.swift index 3ac8deaf1..0b9f01b07 100644 --- a/swift/Sources/FlatBuffers/FlatBufferObject.swift +++ b/swift/Sources/FlatBuffers/FlatBufferObject.swift @@ -69,5 +69,5 @@ public protocol ObjectAPIPacker { static func pack(_ builder: inout FlatBufferBuilder, obj: inout T) -> Offset /// ``unpack()`` unpacks a ``FlatBuffers`` object into a Native swift object. - mutating func unpack() -> T + func unpack() -> T } diff --git a/swift/Sources/FlatBuffers/String+extension.swift b/swift/Sources/FlatBuffers/String+extension.swift index a72786d54..6c3419665 100644 --- a/swift/Sources/FlatBuffers/String+extension.swift +++ b/swift/Sources/FlatBuffers/String+extension.swift @@ -88,7 +88,7 @@ extension String: ObjectAPIPacker { builder.create(string: obj) } - public mutating func unpack() -> String { + public func unpack() -> String { self } diff --git a/swift/Sources/FlatBuffers/Struct.swift b/swift/Sources/FlatBuffers/Struct.swift index 979e68e3f..470af97fe 100644 --- a/swift/Sources/FlatBuffers/Struct.swift +++ b/swift/Sources/FlatBuffers/Struct.swift @@ -48,4 +48,16 @@ public struct Struct { let r = bb.read(def: T.self, position: Int(o &+ position)) return r } + + public func vector( + at off: Int32, + count: Int, + size: Int) -> FlatbufferVector + { + FlatbufferVector( + bb: bb, + startPosition: position &+ off, + count: count, + byteSize: size) + } } diff --git a/swift/Sources/FlatBuffers/Table.swift b/swift/Sources/FlatBuffers/Table.swift index ebfa66ed6..4aa08d576 100644 --- a/swift/Sources/FlatBuffers/Table.swift +++ b/swift/Sources/FlatBuffers/Table.swift @@ -137,7 +137,6 @@ public struct Table { if offset == 0 { return 0 } - return vector(count: offset) } diff --git a/tests/swift/Tests/Flatbuffers/FlatBuffersArraysTests.swift b/tests/swift/Tests/Flatbuffers/FlatBuffersArraysTests.swift new file mode 100644 index 000000000..7ea7b5b2d --- /dev/null +++ b/tests/swift/Tests/Flatbuffers/FlatBuffersArraysTests.swift @@ -0,0 +1,236 @@ +/* + * Copyright 2024 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#if compiler(>=6.2) + +import XCTest + +@testable import FlatBuffers + +@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) +final class FlatBuffersArraysTests: XCTestCase { + + func testStructSizes() { + XCTAssertEqual(MemoryLayout.size, 32) + XCTAssertEqual(MemoryLayout.size, 160) + } + + func testGoldenData() { + // swiftformat:disable all + let data: [UInt8] = [ + 20, 0, 0, 0, 65, 82, 82, 84, 0, 0, 0, 0, 0, 0, 6, 0, 164, 0, 4, 0, 6, + 0, 0, 0, 164, 112, 69, 65, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, + 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 9, 0, 0, 0, 10, + 0, 0, 0, 11, 0, 0, 0, 12, 0, 0, 0, 13, 0, 0, 0, 14, 0, 0, 0, 15, 0, 0, + 0, 129, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 2, 0, 0, 0, 0, 2, 1, + 0, 0, 0, 0, 0, 136, 119, 102, 85, 68, 51, 34, 17, 120, 136, 153, 170, + 187, 204, 221, 238, 3, 0, 0, 0, 252, 255, 255, 255, 1, 1, 0, 0, 0, 0, + 0, 0, 120, 136, 153, 170, 187, 204, 221, 238, 136, 119, 102, 85, 68, + 51, 34, 17, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 255, + 255, 255, 255, 255, 255, 255, 127 + ] + // swiftformat:enable all + + XCTAssertEqual(data, createArrayTable()) + } + + func testData() throws { + var buf = ByteBuffer(bytes: createArrayTable()) + let table: MyGame_Example_ArrayTable = try getCheckedRoot( + byteBuffer: &buf, + fileId: "ARRT") + verifyNativeStruct(a: table.a) + verifyMutations(in: table) + verifyNativeStruct(a: table.a) + } + + func testObjectAPI() throws { + var buf = ByteBuffer(bytes: createArrayTable()) + let table: MyGame_Example_ArrayTable = try getCheckedRoot( + byteBuffer: &buf, + fileId: "ARRT") + verifyNativeStruct(a: table.unpack().a) + } + + func testDefaults() { + XCTAssertEqual( + MyGame_Example_NestedStruct(), + MyGame_Example_NestedStruct( + a: [0, 0], + b: .a, + c: [0, 0], + d: [0, 0])) + } + + func verifyNativeStruct(a: MyGame_Example_ArrayStruct?) { + let a = a! + XCTAssertEqual(a.a, 12.34) + XCTAssertEqual(a.b.count, 15) + var sum: Int32 = 0 + for i in a.b.startIndex.. [UInt8] { + var builder = FlatBufferBuilder(initialSize: 1024) + + let nestedStruct1 = MyGame_Example_NestedStruct( + a: [-1, 2], + b: .a, + c: [ + MyGame_Example_TestEnum.c.rawValue, + MyGame_Example_TestEnum.b.rawValue, + ], + d: [0x1122334455667788, -0x1122334455667788]) + + let nestedStruct2 = MyGame_Example_NestedStruct( + a: [3, -4], + b: .b, + c: [ + MyGame_Example_TestEnum.b.rawValue, + MyGame_Example_TestEnum.a.rawValue, + ], + d: [-0x1122334455667788, 0x1122334455667788]) + + let arrayStruct = MyGame_Example_ArrayStruct( + a: 12.34, + b: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF], + c: -127, + d: [nestedStruct1, nestedStruct2], + e: 1, + f: [-0x8000000000000000, 0x7FFFFFFFFFFFFFFF]) + + let arrayTable = MyGame_Example_ArrayTable.createArrayTable( + &builder, + a: arrayStruct) + builder.finish(offset: arrayTable, fileId: "ARRT") + + return builder.sizedByteArray + } +} + +@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) +extension MyGame_Example_NestedStruct: Equatable { + public static func == ( + lhs: MyGame_Example_NestedStruct, + rhs: MyGame_Example_NestedStruct) -> Bool + { + lhs.a == rhs.a && lhs.c == rhs.c && lhs.d == rhs.d && lhs.b == rhs.b + } +} + +@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) +extension InlineArray: @retroactive Equatable where Element: Equatable { + public static func == (lhs: Self, rhs: Self) -> Bool { + guard lhs.count == rhs.count else { return false } + + for i in 0.. [Element] { + var result: [Element] = [] + for i in startIndex.. Bool { return _accessor.mutate(property, index: 0) } - - public mutating func unpack() -> Property { - return Property(&self) + public func unpack() -> Property { + return Property(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout Property?) -> Offset { guard var obj = obj else { return Offset() } @@ -104,10 +102,9 @@ public struct TestMutatingBool: FlatBufferTable, FlatbuffersVectorInitializable, TestMutatingBool.add(b: b, &fbb) return TestMutatingBool.endTestMutatingBool(&fbb, start: __start) } - - public mutating func unpack() -> TestMutatingBoolT { - return TestMutatingBoolT(&self) + public func unpack() -> TestMutatingBoolT { + return TestMutatingBoolT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout TestMutatingBoolT?) -> Offset { guard var obj = obj else { return Offset() } @@ -142,7 +139,7 @@ public class TestMutatingBoolT: NativeObject { public var b: Property? - public init(_ _t: inout TestMutatingBool) { + public init(_ _t: borrowing TestMutatingBool) { b = _t.b } diff --git a/tests/swift/Tests/Flatbuffers/arrays_test_generated.swift b/tests/swift/Tests/Flatbuffers/arrays_test_generated.swift new file mode 100644 index 000000000..e0c8f183d --- /dev/null +++ b/tests/swift/Tests/Flatbuffers/arrays_test_generated.swift @@ -0,0 +1,359 @@ +// automatically generated by the FlatBuffers compiler, do not modify +// swiftlint:disable all +// swiftformat:disable all + +#if canImport(Common) +import Common +#endif + +import FlatBuffers + +#if compiler(>=6.2) +public enum MyGame_Example_TestEnum: Int8, FlatbuffersVectorInitializable, Enum, Verifiable { + public typealias T = Int8 + public static var byteSize: Int { return MemoryLayout.size } + public var value: Int8 { return self.rawValue } + case a = 0 + case b = 1 + case c = 2 + + public static var max: MyGame_Example_TestEnum { return .c } + public static var min: MyGame_Example_TestEnum { return .a } +} + +extension MyGame_Example_TestEnum: Encodable { + public func encode(to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + switch self { + case .a: try container.encode("A") + case .b: try container.encode("B") + case .c: try container.encode("C") + } + } +} + +@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) +public struct MyGame_Example_NestedStruct: NativeStruct, FlatbuffersVectorInitializable, Verifiable, FlatbuffersInitializable, NativeObject { + + static func validateVersion() { FlatBuffersVersion_25_9_23() } + + private var _a: InlineArray<2, Int32> + private var _b: Int8 + private var _c: InlineArray<2, Int8> + private var padding0__: UInt8 = 0 + private var padding1__: UInt32 = 0 + private var _d: InlineArray<2, Int64> + + public init(_ bb: ByteBuffer, o: Int32) { + self = bb.read(def: Self.self, position: Int(o)) + } + + public init(a: InlineArray<2, Int32>, b: MyGame_Example_TestEnum, c: InlineArray<2, Int8>, d: InlineArray<2, Int64>) { + _a = a + _b = b.value + _c = c + _d = d + } + + public init() { + _a = InlineArray(repeating: 0) + _b = 0 + _c = InlineArray(repeating: 0) + _d = InlineArray(repeating: 0) + } + + public init(_ _t: borrowing MyGame_Example_NestedStruct_Mutable) { + let _va = _t.a + _a = InlineArray { _va[$0] } + _b = _t.b.value + let _vc = _t.c + _c = InlineArray { _vc[$0].rawValue } + let _vd = _t.d + _d = InlineArray { _vd[$0] } + } + + public var a: InlineArray<2, Int32> { _a } + public var b: MyGame_Example_TestEnum { MyGame_Example_TestEnum(rawValue: _b)! } + public var c: InlineArray<2, MyGame_Example_TestEnum> { InlineArray { MyGame_Example_TestEnum(rawValue: _c[$0])! } } + public var d: InlineArray<2, Int64> { _d } + + public static func verify(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable { + try verifier.inBuffer(position: position, of: MyGame_Example_NestedStruct.self) + } +} + +@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) +extension MyGame_Example_NestedStruct: Encodable { + + enum CodingKeys: String, CodingKey { + case a = "a" + case b = "b" + case c = "c" + case d = "d" + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + var aContainer = container.nestedUnkeyedContainer(forKey: .a) + for index in a.startIndex.. { return _accessor.vector(at: 0, count: 2, size: 4) } + @discardableResult public func mutate(a: Int32, at index: Int32) -> Bool { return _accessor.mutate(a, index: 0 + (index * 4)) } + public var b: MyGame_Example_TestEnum { return MyGame_Example_TestEnum(rawValue: _accessor.readBuffer(of: Int8.self, at: 8)) ?? .a } + @discardableResult public func mutate(b: MyGame_Example_TestEnum) -> Bool { return _accessor.mutate(b.rawValue, index: 8) } + public var c: FlatbufferVector { return _accessor.vector(at: 9, count: 2, size: 1) } + @discardableResult public func mutate(c: MyGame_Example_TestEnum, at index: Int32) -> Bool { return _accessor.mutate(c.rawValue, index: 9 + (index * 1)) } + public var d: FlatbufferVector { return _accessor.vector(at: 16, count: 2, size: 8) } + @discardableResult public func mutate(d: Int64, at index: Int32) -> Bool { return _accessor.mutate(d, index: 16 + (index * 8)) } + + public func unpack() -> MyGame_Example_NestedStruct { + return MyGame_Example_NestedStruct(self) + } + public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_NestedStruct?) -> Offset { + guard var obj = obj else { return Offset() } + return pack(&builder, obj: &obj) + } + + public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_NestedStruct) -> Offset { + return builder.create(struct: obj) + } +} + +@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) +public struct MyGame_Example_ArrayStruct: NativeStruct, FlatbuffersVectorInitializable, Verifiable, FlatbuffersInitializable, NativeObject { + + static func validateVersion() { FlatBuffersVersion_25_9_23() } + + private var _a: Float32 + private var _b: InlineArray<15, Int32> + private var _c: Int8 + private var padding0__: UInt8 = 0 + private var padding1__: UInt16 = 0 + private var padding2__: UInt32 = 0 + private var _d: InlineArray<2, MyGame_Example_NestedStruct> + private var _e: Int32 + private var padding3__: UInt32 = 0 + private var _f: InlineArray<2, Int64> + + public init(_ bb: ByteBuffer, o: Int32) { + self = bb.read(def: Self.self, position: Int(o)) + } + + public init(a: Float32, b: InlineArray<15, Int32>, c: Int8, d: InlineArray<2, MyGame_Example_NestedStruct>, e: Int32, f: InlineArray<2, Int64>) { + _a = a + _b = b + _c = c + _d = d + _e = e + _f = f + } + + public init() { + _a = 0.0 + _b = InlineArray(repeating: 0) + _c = 0 + _d = InlineArray(repeating: MyGame_Example_NestedStruct()) + _e = 0 + _f = InlineArray(repeating: 0) + } + + public init(_ _t: borrowing MyGame_Example_ArrayStruct_Mutable) { + _a = _t.a + let _vb = _t.b + _b = InlineArray { _vb[$0] } + _c = _t.c + let _vd = _t.d + _d = InlineArray { _vd[$0].unpack() } + _e = _t.e + let _vf = _t.f + _f = InlineArray { _vf[$0] } + } + + public var a: Float32 { _a } + public var b: InlineArray<15, Int32> { _b } + public var c: Int8 { _c } + public var d: InlineArray<2, MyGame_Example_NestedStruct> { _d } + public var e: Int32 { _e } + public var f: InlineArray<2, Int64> { _f } + + public static func verify(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable { + try verifier.inBuffer(position: position, of: MyGame_Example_ArrayStruct.self) + } +} + +@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) +extension MyGame_Example_ArrayStruct: Encodable { + + enum CodingKeys: String, CodingKey { + case a = "a" + case b = "b" + case c = "c" + case d = "d" + case e = "e" + case f = "f" + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + if a != 0.0 { + try container.encodeIfPresent(a, forKey: .a) + } + var bContainer = container.nestedUnkeyedContainer(forKey: .b) + for index in b.startIndex.. Bool { return _accessor.mutate(a, index: 0) } + public var b: FlatbufferVector { return _accessor.vector(at: 4, count: 15, size: 4) } + @discardableResult public func mutate(b: Int32, at index: Int32) -> Bool { return _accessor.mutate(b, index: 4 + (index * 4)) } + public var c: Int8 { return _accessor.readBuffer(of: Int8.self, at: 64) } + @discardableResult public func mutate(c: Int8) -> Bool { return _accessor.mutate(c, index: 64) } + public var d: FlatbufferVector { return _accessor.vector(at: 72, count: 2, size: 32) } + public var e: Int32 { return _accessor.readBuffer(of: Int32.self, at: 136) } + @discardableResult public func mutate(e: Int32) -> Bool { return _accessor.mutate(e, index: 136) } + public var f: FlatbufferVector { return _accessor.vector(at: 144, count: 2, size: 8) } + @discardableResult public func mutate(f: Int64, at index: Int32) -> Bool { return _accessor.mutate(f, index: 144 + (index * 8)) } + + public func unpack() -> MyGame_Example_ArrayStruct { + return MyGame_Example_ArrayStruct(self) + } + public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_ArrayStruct?) -> Offset { + guard var obj = obj else { return Offset() } + return pack(&builder, obj: &obj) + } + + public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_ArrayStruct) -> Offset { + return builder.create(struct: obj) + } +} + +@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) +public struct MyGame_Example_ArrayTable: FlatBufferTable, FlatbuffersVectorInitializable, Verifiable, ObjectAPIPacker { + + static func validateVersion() { FlatBuffersVersion_25_9_23() } + public var __buffer: ByteBuffer! { return _accessor.bb } + private var _accessor: Table + + public static var id: String { "ARRT" } + public static func finish(_ fbb: inout FlatBufferBuilder, end: Offset, prefix: Bool = false) { fbb.finish(offset: end, fileId: MyGame_Example_ArrayTable.id, addPrefix: prefix) } + private init(_ t: Table) { _accessor = t } + public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) } + + private enum VTOFFSET: VOffset { + case a = 4 + var v: Int32 { Int32(self.rawValue) } + var p: VOffset { self.rawValue } + } + + public var a: MyGame_Example_ArrayStruct? { let o = _accessor.offset(VTOFFSET.a.v); return o == 0 ? nil : _accessor.readBuffer(of: MyGame_Example_ArrayStruct.self, at: o) } + public var mutableA: MyGame_Example_ArrayStruct_Mutable? { let o = _accessor.offset(VTOFFSET.a.v); return o == 0 ? nil : MyGame_Example_ArrayStruct_Mutable(_accessor.bb, o: o + _accessor.position) } + public static func startArrayTable(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 1) } + public static func add(a: MyGame_Example_ArrayStruct?, _ fbb: inout FlatBufferBuilder) { guard let a = a else { return }; fbb.create(struct: a, position: VTOFFSET.a.p) } + public static func endArrayTable(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end } + public static func createArrayTable( + _ fbb: inout FlatBufferBuilder, + a: MyGame_Example_ArrayStruct? = nil + ) -> Offset { + let __start = MyGame_Example_ArrayTable.startArrayTable(&fbb) + MyGame_Example_ArrayTable.add(a: a, &fbb) + return MyGame_Example_ArrayTable.endArrayTable(&fbb, start: __start) + } + + public func unpack() -> MyGame_Example_ArrayTableT { + return MyGame_Example_ArrayTableT(self) + } + public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_ArrayTableT?) -> Offset { + guard var obj = obj else { return Offset() } + return pack(&builder, obj: &obj) + } + + public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_ArrayTableT) -> Offset { + let __root = MyGame_Example_ArrayTable.startArrayTable(&builder) + MyGame_Example_ArrayTable.add(a: obj.a, &builder) + return MyGame_Example_ArrayTable.endArrayTable(&builder, start: __root) + } + + public static func verify(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable { + var _v = try verifier.visitTable(at: position) + try _v.visit(field: VTOFFSET.a.p, fieldName: "a", required: false, type: MyGame_Example_ArrayStruct.self) + _v.finish() + } +} + +@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) +extension MyGame_Example_ArrayTable: Encodable { + + enum CodingKeys: String, CodingKey { + case a = "a" + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encodeIfPresent(a, forKey: .a) + } +} + +@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, *) +public class MyGame_Example_ArrayTableT: NativeObject { + + public var a: MyGame_Example_ArrayStruct? + + public init(_ _t: borrowing MyGame_Example_ArrayTable) { + a = _t.a + } + + public init() { + a = MyGame_Example_ArrayStruct() + } + + public func serialize() -> ByteBuffer { return serialize(type: MyGame_Example_ArrayTable.self) } + +} +#endif diff --git a/tests/swift/Tests/Flatbuffers/monster_test_generated.swift b/tests/swift/Tests/Flatbuffers/monster_test_generated.swift index ff1d94142..75dca157b 100644 --- a/tests/swift/Tests/Flatbuffers/monster_test_generated.swift +++ b/tests/swift/Tests/Flatbuffers/monster_test_generated.swift @@ -245,12 +245,10 @@ public struct MyGame_Example_Test: NativeStruct, FlatbuffersVectorInitializable, private var _a: Int16 private var _b: Int8 - private let padding0__: UInt8 = 0 + private var padding0__: UInt8 = 0 public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _a = _accessor.readBuffer(of: Int16.self, at: 0) - _b = _accessor.readBuffer(of: Int8.self, at: 2) + self = bb.read(def: Self.self, position: Int(o)) } public init(a: Int16, b: Int8) { @@ -263,7 +261,7 @@ public struct MyGame_Example_Test: NativeStruct, FlatbuffersVectorInitializable, _b = 0 } - public init(_ _t: inout MyGame_Example_Test_Mutable) { + public init(_ _t: borrowing MyGame_Example_Test_Mutable) { _a = _t.a _b = _t.b } @@ -305,10 +303,9 @@ public struct MyGame_Example_Test_Mutable: FlatBufferStruct, FlatbuffersVectorIn @discardableResult public func mutate(a: Int16) -> Bool { return _accessor.mutate(a, index: 0) } public var b: Int8 { return _accessor.readBuffer(of: Int8.self, at: 2) } @discardableResult public func mutate(b: Int8) -> Bool { return _accessor.mutate(b, index: 2) } - - public mutating func unpack() -> MyGame_Example_Test { - return MyGame_Example_Test(&self) + public func unpack() -> MyGame_Example_Test { + return MyGame_Example_Test(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_Test?) -> Offset { guard var obj = obj else { return Offset() } @@ -327,21 +324,15 @@ public struct MyGame_Example_Vec3: NativeStruct, FlatbuffersVectorInitializable, private var _x: Float32 private var _y: Float32 private var _z: Float32 - private let padding0__: UInt32 = 0 + private var padding0__: UInt32 = 0 private var _test1: Double private var _test2: UInt8 - private let padding1__: UInt8 = 0 + private var padding1__: UInt8 = 0 private var _test3: MyGame_Example_Test - private let padding2__: UInt16 = 0 + private var padding2__: UInt16 = 0 public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _x = _accessor.readBuffer(of: Float32.self, at: 0) - _y = _accessor.readBuffer(of: Float32.self, at: 4) - _z = _accessor.readBuffer(of: Float32.self, at: 8) - _test1 = _accessor.readBuffer(of: Double.self, at: 16) - _test2 = _accessor.readBuffer(of: UInt8.self, at: 24) - _test3 = MyGame_Example_Test(_accessor.bb, o: _accessor.position + 26) + self = bb.read(def: Self.self, position: Int(o)) } public init(x: Float32, y: Float32, z: Float32, test1: Double, test2: MyGame_Example_Color, test3: MyGame_Example_Test) { @@ -362,13 +353,13 @@ public struct MyGame_Example_Vec3: NativeStruct, FlatbuffersVectorInitializable, _test3 = MyGame_Example_Test() } - public init(_ _t: inout MyGame_Example_Vec3_Mutable) { + public init(_ _t: borrowing MyGame_Example_Vec3_Mutable) { _x = _t.x _y = _t.y _z = _t.z _test1 = _t.test1 _test2 = _t.test2.value - var _vtest3 = _t.test3 + let _vtest3 = _t.test3 _test3 = _vtest3.unpack() } @@ -434,10 +425,9 @@ public struct MyGame_Example_Vec3_Mutable: FlatBufferStruct, FlatbuffersVectorIn public var test2: MyGame_Example_Color { return MyGame_Example_Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: 24)) ?? .red } @discardableResult public func mutate(test2: MyGame_Example_Color) -> Bool { return _accessor.mutate(test2.rawValue, index: 24) } public var test3: MyGame_Example_Test_Mutable { return MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.position + 26) } - - public mutating func unpack() -> MyGame_Example_Vec3 { - return MyGame_Example_Vec3(&self) + public func unpack() -> MyGame_Example_Vec3 { + return MyGame_Example_Vec3(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_Vec3?) -> Offset { guard var obj = obj else { return Offset() } @@ -457,9 +447,7 @@ public struct MyGame_Example_Ability: NativeStruct, FlatbuffersVectorInitializab private var _distance: UInt32 public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _id = _accessor.readBuffer(of: UInt32.self, at: 0) - _distance = _accessor.readBuffer(of: UInt32.self, at: 4) + self = bb.read(def: Self.self, position: Int(o)) } public init(id: UInt32, distance: UInt32) { @@ -472,7 +460,7 @@ public struct MyGame_Example_Ability: NativeStruct, FlatbuffersVectorInitializab _distance = 0 } - public init(_ _t: inout MyGame_Example_Ability_Mutable) { + public init(_ _t: borrowing MyGame_Example_Ability_Mutable) { _id = _t.id _distance = _t.distance } @@ -514,10 +502,9 @@ public struct MyGame_Example_Ability_Mutable: FlatBufferStruct, FlatbuffersVecto @discardableResult public func mutate(id: UInt32) -> Bool { return _accessor.mutate(id, index: 0) } public var distance: UInt32 { return _accessor.readBuffer(of: UInt32.self, at: 4) } @discardableResult public func mutate(distance: UInt32) -> Bool { return _accessor.mutate(distance, index: 4) } - - public mutating func unpack() -> MyGame_Example_Ability { - return MyGame_Example_Ability(&self) + public func unpack() -> MyGame_Example_Ability { + return MyGame_Example_Ability(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_Ability?) -> Offset { guard var obj = obj else { return Offset() } @@ -538,10 +525,7 @@ public struct MyGame_Example_StructOfStructs: NativeStruct, FlatbuffersVectorIni private var _c: MyGame_Example_Ability public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _a = MyGame_Example_Ability(_accessor.bb, o: _accessor.position + 0) - _b = MyGame_Example_Test(_accessor.bb, o: _accessor.position + 8) - _c = MyGame_Example_Ability(_accessor.bb, o: _accessor.position + 12) + self = bb.read(def: Self.self, position: Int(o)) } public init(a: MyGame_Example_Ability, b: MyGame_Example_Test, c: MyGame_Example_Ability) { @@ -556,12 +540,12 @@ public struct MyGame_Example_StructOfStructs: NativeStruct, FlatbuffersVectorIni _c = MyGame_Example_Ability() } - public init(_ _t: inout MyGame_Example_StructOfStructs_Mutable) { - var _va = _t.a + public init(_ _t: borrowing MyGame_Example_StructOfStructs_Mutable) { + let _va = _t.a _a = _va.unpack() - var _vb = _t.b + let _vb = _t.b _b = _vb.unpack() - var _vc = _t.c + let _vc = _t.c _c = _vc.unpack() } @@ -600,10 +584,9 @@ public struct MyGame_Example_StructOfStructs_Mutable: FlatBufferStruct, Flatbuff public var a: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.position + 0) } public var b: MyGame_Example_Test_Mutable { return MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.position + 8) } public var c: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.position + 12) } - - public mutating func unpack() -> MyGame_Example_StructOfStructs { - return MyGame_Example_StructOfStructs(&self) + public func unpack() -> MyGame_Example_StructOfStructs { + return MyGame_Example_StructOfStructs(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_StructOfStructs?) -> Offset { guard var obj = obj else { return Offset() } @@ -622,8 +605,7 @@ public struct MyGame_Example_StructOfStructsOfStructs: NativeStruct, Flatbuffers private var _a: MyGame_Example_StructOfStructs public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _a = MyGame_Example_StructOfStructs(_accessor.bb, o: _accessor.position + 0) + self = bb.read(def: Self.self, position: Int(o)) } public init(a: MyGame_Example_StructOfStructs) { @@ -634,8 +616,8 @@ public struct MyGame_Example_StructOfStructsOfStructs: NativeStruct, Flatbuffers _a = MyGame_Example_StructOfStructs() } - public init(_ _t: inout MyGame_Example_StructOfStructsOfStructs_Mutable) { - var _va = _t.a + public init(_ _t: borrowing MyGame_Example_StructOfStructsOfStructs_Mutable) { + let _va = _t.a _a = _va.unpack() } @@ -666,10 +648,9 @@ public struct MyGame_Example_StructOfStructsOfStructs_Mutable: FlatBufferStruct, public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) } public var a: MyGame_Example_StructOfStructs_Mutable { return MyGame_Example_StructOfStructs_Mutable(_accessor.bb, o: _accessor.position + 0) } - - public mutating func unpack() -> MyGame_Example_StructOfStructsOfStructs { - return MyGame_Example_StructOfStructsOfStructs(&self) + public func unpack() -> MyGame_Example_StructOfStructsOfStructs { + return MyGame_Example_StructOfStructsOfStructs(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_StructOfStructsOfStructs?) -> Offset { guard var obj = obj else { return Offset() } @@ -694,10 +675,9 @@ public struct MyGame_InParentNamespace: FlatBufferTable, FlatbuffersVectorInitia public static func startInParentNamespace(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 0) } public static func endInParentNamespace(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end } - - public mutating func unpack() -> MyGame_InParentNamespaceT { - return MyGame_InParentNamespaceT(&self) + public func unpack() -> MyGame_InParentNamespaceT { + return MyGame_InParentNamespaceT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_InParentNamespaceT?) -> Offset { guard var obj = obj else { return Offset() } @@ -724,7 +704,7 @@ extension MyGame_InParentNamespace: Encodable { public class MyGame_InParentNamespaceT: NativeObject { - public init(_ _t: inout MyGame_InParentNamespace) { + public init(_ _t: borrowing MyGame_InParentNamespace) { } public init() { @@ -746,10 +726,9 @@ public struct MyGame_Example2_Monster: FlatBufferTable, FlatbuffersVectorInitial public static func startMonster(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 0) } public static func endMonster(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end } - - public mutating func unpack() -> MyGame_Example2_MonsterT { - return MyGame_Example2_MonsterT(&self) + public func unpack() -> MyGame_Example2_MonsterT { + return MyGame_Example2_MonsterT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example2_MonsterT?) -> Offset { guard var obj = obj else { return Offset() } @@ -776,7 +755,7 @@ extension MyGame_Example2_Monster: Encodable { public class MyGame_Example2_MonsterT: NativeObject { - public init(_ _t: inout MyGame_Example2_Monster) { + public init(_ _t: borrowing MyGame_Example2_Monster) { } public init() { @@ -815,10 +794,9 @@ internal struct MyGame_Example_TestSimpleTableWithEnum: FlatBufferTable, Flatbuf MyGame_Example_TestSimpleTableWithEnum.add(color: color, &fbb) return MyGame_Example_TestSimpleTableWithEnum.endTestSimpleTableWithEnum(&fbb, start: __start) } - - internal mutating func unpack() -> MyGame_Example_TestSimpleTableWithEnumT { - return MyGame_Example_TestSimpleTableWithEnumT(&self) + internal func unpack() -> MyGame_Example_TestSimpleTableWithEnumT { + return MyGame_Example_TestSimpleTableWithEnumT(self) } internal static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_TestSimpleTableWithEnumT?) -> Offset { guard var obj = obj else { return Offset() } @@ -855,7 +833,7 @@ internal class MyGame_Example_TestSimpleTableWithEnumT: NativeObject { internal var color: MyGame_Example_Color - internal init(_ _t: inout MyGame_Example_TestSimpleTableWithEnum) { + internal init(_ _t: borrowing MyGame_Example_TestSimpleTableWithEnum) { color = _t.color } @@ -932,10 +910,9 @@ public struct MyGame_Example_Stat: FlatBufferTable, FlatbuffersVectorInitializab } return nil } - - public mutating func unpack() -> MyGame_Example_StatT { - return MyGame_Example_StatT(&self) + public func unpack() -> MyGame_Example_StatT { + return MyGame_Example_StatT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_StatT?) -> Offset { guard var obj = obj else { return Offset() } @@ -991,7 +968,7 @@ public class MyGame_Example_StatT: NativeObject { public var val: Int64 public var count: UInt16 - public init(_ _t: inout MyGame_Example_Stat) { + public init(_ _t: borrowing MyGame_Example_Stat) { id = _t.id val = _t.val count = _t.count @@ -1059,10 +1036,9 @@ public struct MyGame_Example_Referrable: FlatBufferTable, FlatbuffersVectorIniti } return nil } - - public mutating func unpack() -> MyGame_Example_ReferrableT { - return MyGame_Example_ReferrableT(&self) + public func unpack() -> MyGame_Example_ReferrableT { + return MyGame_Example_ReferrableT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_ReferrableT?) -> Offset { guard var obj = obj else { return Offset() } @@ -1099,7 +1075,7 @@ public class MyGame_Example_ReferrableT: NativeObject { public var id: UInt64 - public init(_ _t: inout MyGame_Example_Referrable) { + public init(_ _t: borrowing MyGame_Example_Referrable) { id = _t.id } @@ -1539,10 +1515,9 @@ public struct MyGame_Example_Monster: FlatBufferTable, FlatbuffersVectorInitiali } return nil } - - public mutating func unpack() -> MyGame_Example_MonsterT { - return MyGame_Example_MonsterT(&self) + public func unpack() -> MyGame_Example_MonsterT { + return MyGame_Example_MonsterT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_MonsterT?) -> Offset { guard var obj = obj else { return Offset() } @@ -2065,7 +2040,7 @@ public class MyGame_Example_MonsterT: NativeObject { public var negativeInfinityDefault: Float32 public var doubleInfDefault: Double - public init(_ _t: inout MyGame_Example_Monster) { + public init(_ _t: borrowing MyGame_Example_Monster) { pos = _t.pos mana = _t.mana hp = _t.hp @@ -2075,13 +2050,13 @@ public class MyGame_Example_MonsterT: NativeObject { color = _t.color switch _t.testType { case .monster: - var _v = _t.test(type: MyGame_Example_Monster.self) + let _v = _t.test(type: MyGame_Example_Monster.self) test = MyGame_Example_Any_Union(_v?.unpack(), type: .monster) case .testsimpletablewithenum: - var _v = _t.test(type: MyGame_Example_TestSimpleTableWithEnum.self) + let _v = _t.test(type: MyGame_Example_TestSimpleTableWithEnum.self) test = MyGame_Example_Any_Union(_v?.unpack(), type: .testsimpletablewithenum) case .mygameExample2Monster: - var _v = _t.test(type: MyGame_Example2_Monster.self) + let _v = _t.test(type: MyGame_Example2_Monster.self) test = MyGame_Example_Any_Union(_v?.unpack(), type: .mygameExample2Monster) default: break } @@ -2090,15 +2065,13 @@ public class MyGame_Example_MonsterT: NativeObject { testarrayofstring = [] testarrayofstring.append(contentsOf: _t.testarrayofstring) testarrayoftables = [] - for var val in _t.testarrayoftables{ + for val in _t.testarrayoftables{ testarrayoftables.append(val.unpack()) } - var __enemy = _t.enemy - enemy = __enemy?.unpack() + enemy = _t.enemy?.unpack() testnestedflatbuffer = [] testnestedflatbuffer.append(contentsOf: _t.testnestedflatbuffer) - var __testempty = _t.testempty - testempty = __testempty?.unpack() + testempty = _t.testempty?.unpack() testbool = _t.testbool testhashs32Fnv1 = _t.testhashs32Fnv1 testhashu32Fnv1 = _t.testhashu32Fnv1 @@ -2125,17 +2098,16 @@ public class MyGame_Example_MonsterT: NativeObject { vectorOfLongs.append(contentsOf: _t.vectorOfLongs) vectorOfDoubles = [] vectorOfDoubles.append(contentsOf: _t.vectorOfDoubles) - var __parentNamespaceTest = _t.parentNamespaceTest - parentNamespaceTest = __parentNamespaceTest?.unpack() + parentNamespaceTest = _t.parentNamespaceTest?.unpack() vectorOfReferrables = [] - for var val in _t.vectorOfReferrables{ + for val in _t.vectorOfReferrables{ vectorOfReferrables.append(val.unpack()) } singleWeakReference = _t.singleWeakReference vectorOfWeakReferences = [] vectorOfWeakReferences.append(contentsOf: _t.vectorOfWeakReferences) vectorOfStrongReferrables = [] - for var val in _t.vectorOfStrongReferrables{ + for val in _t.vectorOfStrongReferrables{ vectorOfStrongReferrables.append(val.unpack()) } coOwningReference = _t.coOwningReference @@ -2146,25 +2118,25 @@ public class MyGame_Example_MonsterT: NativeObject { vectorOfNonOwningReferences.append(contentsOf: _t.vectorOfNonOwningReferences) switch _t.anyUniqueType { case .m: - var _v = _t.anyUnique(type: MyGame_Example_Monster.self) + let _v = _t.anyUnique(type: MyGame_Example_Monster.self) anyUnique = MyGame_Example_AnyUniqueAliasesUnion(_v?.unpack(), type: .m) case .ts: - var _v = _t.anyUnique(type: MyGame_Example_TestSimpleTableWithEnum.self) + let _v = _t.anyUnique(type: MyGame_Example_TestSimpleTableWithEnum.self) anyUnique = MyGame_Example_AnyUniqueAliasesUnion(_v?.unpack(), type: .ts) case .m2: - var _v = _t.anyUnique(type: MyGame_Example2_Monster.self) + let _v = _t.anyUnique(type: MyGame_Example2_Monster.self) anyUnique = MyGame_Example_AnyUniqueAliasesUnion(_v?.unpack(), type: .m2) default: break } switch _t.anyAmbiguousType { case .m1: - var _v = _t.anyAmbiguous(type: MyGame_Example_Monster.self) + let _v = _t.anyAmbiguous(type: MyGame_Example_Monster.self) anyAmbiguous = MyGame_Example_AnyAmbiguousAliasesUnion(_v?.unpack(), type: .m1) case .m2: - var _v = _t.anyAmbiguous(type: MyGame_Example_Monster.self) + let _v = _t.anyAmbiguous(type: MyGame_Example_Monster.self) anyAmbiguous = MyGame_Example_AnyAmbiguousAliasesUnion(_v?.unpack(), type: .m2) case .m3: - var _v = _t.anyAmbiguous(type: MyGame_Example_Monster.self) + let _v = _t.anyAmbiguous(type: MyGame_Example_Monster.self) anyAmbiguous = MyGame_Example_AnyAmbiguousAliasesUnion(_v?.unpack(), type: .m3) default: break } @@ -2174,7 +2146,7 @@ public class MyGame_Example_MonsterT: NativeObject { testrequirednestedflatbuffer = [] testrequirednestedflatbuffer.append(contentsOf: _t.testrequirednestedflatbuffer) scalarKeySortedTables = [] - for var val in _t.scalarKeySortedTables{ + for val in _t.scalarKeySortedTables{ scalarKeySortedTables.append(val.unpack()) } nativeInline = _t.nativeInline @@ -2349,10 +2321,9 @@ public struct MyGame_Example_TypeAliases: FlatBufferTable, FlatbuffersVectorInit MyGame_Example_TypeAliases.addVectorOf(vf64: vf64, &fbb) return MyGame_Example_TypeAliases.endTypeAliases(&fbb, start: __start) } - - public mutating func unpack() -> MyGame_Example_TypeAliasesT { - return MyGame_Example_TypeAliasesT(&self) + public func unpack() -> MyGame_Example_TypeAliasesT { + return MyGame_Example_TypeAliasesT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_TypeAliasesT?) -> Offset { guard var obj = obj else { return Offset() } @@ -2464,7 +2435,7 @@ public class MyGame_Example_TypeAliasesT: NativeObject { public var v8: [Int8] public var vf64: [Double] - public init(_ _t: inout MyGame_Example_TypeAliases) { + public init(_ _t: borrowing MyGame_Example_TypeAliases) { i8 = _t.i8 u8 = _t.u8 i16 = _t.i16 diff --git a/tests/swift/Tests/Flatbuffers/more_defaults_generated.swift b/tests/swift/Tests/Flatbuffers/more_defaults_generated.swift index 757c7fe8d..42bdcfc9c 100644 --- a/tests/swift/Tests/Flatbuffers/more_defaults_generated.swift +++ b/tests/swift/Tests/Flatbuffers/more_defaults_generated.swift @@ -88,10 +88,9 @@ public struct MoreDefaults: FlatBufferTable, FlatbuffersVectorInitializable, Ver MoreDefaults.addVectorOf(bools: bools, &fbb) return MoreDefaults.endMoreDefaults(&fbb, start: __start) } - - public mutating func unpack() -> MoreDefaultsT { - return MoreDefaultsT(&self) + public func unpack() -> MoreDefaultsT { + return MoreDefaultsT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MoreDefaultsT?) -> Offset { guard var obj = obj else { return Offset() } @@ -169,7 +168,7 @@ public class MoreDefaultsT: NativeObject { public var abcs: [ABC] public var bools: [Bool] - public init(_ _t: inout MoreDefaults) { + public init(_ _t: borrowing MoreDefaults) { ints = [] ints.append(contentsOf: _t.ints) floats = [] diff --git a/tests/swift/Tests/Flatbuffers/union_vector_generated.swift b/tests/swift/Tests/Flatbuffers/union_vector_generated.swift index 22dab4e63..59d78793b 100644 --- a/tests/swift/Tests/Flatbuffers/union_vector_generated.swift +++ b/tests/swift/Tests/Flatbuffers/union_vector_generated.swift @@ -129,8 +129,7 @@ public struct Rapunzel: NativeStruct, FlatbuffersVectorInitializable, Verifiable private var _hairLength: Int32 public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _hairLength = _accessor.readBuffer(of: Int32.self, at: 0) + self = bb.read(def: Self.self, position: Int(o)) } public init(hairLength: Int32) { @@ -141,7 +140,7 @@ public struct Rapunzel: NativeStruct, FlatbuffersVectorInitializable, Verifiable _hairLength = 0 } - public init(_ _t: inout Rapunzel_Mutable) { + public init(_ _t: borrowing Rapunzel_Mutable) { _hairLength = _t.hairLength } @@ -175,10 +174,9 @@ public struct Rapunzel_Mutable: FlatBufferStruct, FlatbuffersVectorInitializable public var hairLength: Int32 { return _accessor.readBuffer(of: Int32.self, at: 0) } @discardableResult public func mutate(hairLength: Int32) -> Bool { return _accessor.mutate(hairLength, index: 0) } - - public mutating func unpack() -> Rapunzel { - return Rapunzel(&self) + public func unpack() -> Rapunzel { + return Rapunzel(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout Rapunzel?) -> Offset { guard var obj = obj else { return Offset() } @@ -197,8 +195,7 @@ public struct BookReader: NativeStruct, FlatbuffersVectorInitializable, Verifiab private var _booksRead: Int32 public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _booksRead = _accessor.readBuffer(of: Int32.self, at: 0) + self = bb.read(def: Self.self, position: Int(o)) } public init(booksRead: Int32) { @@ -209,7 +206,7 @@ public struct BookReader: NativeStruct, FlatbuffersVectorInitializable, Verifiab _booksRead = 0 } - public init(_ _t: inout BookReader_Mutable) { + public init(_ _t: borrowing BookReader_Mutable) { _booksRead = _t.booksRead } @@ -243,10 +240,9 @@ public struct BookReader_Mutable: FlatBufferStruct, FlatbuffersVectorInitializab public var booksRead: Int32 { return _accessor.readBuffer(of: Int32.self, at: 0) } @discardableResult public func mutate(booksRead: Int32) -> Bool { return _accessor.mutate(booksRead, index: 0) } - - public mutating func unpack() -> BookReader { - return BookReader(&self) + public func unpack() -> BookReader { + return BookReader(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout BookReader?) -> Offset { guard var obj = obj else { return Offset() } @@ -265,8 +261,7 @@ public struct FallingTub: NativeStruct, FlatbuffersVectorInitializable, Verifiab private var _weight: Int32 public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _weight = _accessor.readBuffer(of: Int32.self, at: 0) + self = bb.read(def: Self.self, position: Int(o)) } public init(weight: Int32) { @@ -277,7 +272,7 @@ public struct FallingTub: NativeStruct, FlatbuffersVectorInitializable, Verifiab _weight = 0 } - public init(_ _t: inout FallingTub_Mutable) { + public init(_ _t: borrowing FallingTub_Mutable) { _weight = _t.weight } @@ -311,10 +306,9 @@ public struct FallingTub_Mutable: FlatBufferStruct, FlatbuffersVectorInitializab public var weight: Int32 { return _accessor.readBuffer(of: Int32.self, at: 0) } @discardableResult public func mutate(weight: Int32) -> Bool { return _accessor.mutate(weight, index: 0) } - - public mutating func unpack() -> FallingTub { - return FallingTub(&self) + public func unpack() -> FallingTub { + return FallingTub(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout FallingTub?) -> Offset { guard var obj = obj else { return Offset() } @@ -356,10 +350,9 @@ public struct Attacker: FlatBufferTable, FlatbuffersVectorInitializable, Verifia Attacker.add(swordAttackDamage: swordAttackDamage, &fbb) return Attacker.endAttacker(&fbb, start: __start) } - - public mutating func unpack() -> AttackerT { - return AttackerT(&self) + public func unpack() -> AttackerT { + return AttackerT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout AttackerT?) -> Offset { guard var obj = obj else { return Offset() } @@ -396,7 +389,7 @@ public class AttackerT: NativeObject { public var swordAttackDamage: Int32 - public init(_ _t: inout Attacker) { + public init(_ _t: borrowing Attacker) { swordAttackDamage = _t.swordAttackDamage } @@ -437,10 +430,9 @@ public struct HandFan: FlatBufferTable, FlatbuffersVectorInitializable, Verifiab HandFan.add(length: length, &fbb) return HandFan.endHandFan(&fbb, start: __start) } - - public mutating func unpack() -> HandFanT { - return HandFanT(&self) + public func unpack() -> HandFanT { + return HandFanT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout HandFanT?) -> Offset { guard var obj = obj else { return Offset() } @@ -477,7 +469,7 @@ public class HandFanT: NativeObject { public var length: Int32 - public init(_ _t: inout HandFan) { + public init(_ _t: borrowing HandFan) { length = _t.length } @@ -533,10 +525,9 @@ public struct Movie: FlatBufferTable, FlatbuffersVectorInitializable, Verifiable Movie.addVectorOf(characters: characters, &fbb) return Movie.endMovie(&fbb, start: __start) } - - public mutating func unpack() -> MovieT { - return MovieT(&self) + public func unpack() -> MovieT { + return MovieT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MovieT?) -> Offset { guard var obj = obj else { return Offset() } @@ -673,25 +664,25 @@ public class MovieT: NativeObject { public var mainCharacter: CharacterUnion? public var characters: [CharacterUnion?] - public init(_ _t: inout Movie) { + public init(_ _t: borrowing Movie) { switch _t.mainCharacterType { case .mulan: - var _v = _t.mainCharacter(type: Attacker.self) + let _v = _t.mainCharacter(type: Attacker.self) mainCharacter = CharacterUnion(_v?.unpack(), type: .mulan) case .rapunzel: - var _v = _t.mainCharacter(type: Rapunzel_Mutable.self) + let _v = _t.mainCharacter(type: Rapunzel_Mutable.self) mainCharacter = CharacterUnion(_v?.unpack(), type: .rapunzel) case .belle: - var _v = _t.mainCharacter(type: BookReader_Mutable.self) + let _v = _t.mainCharacter(type: BookReader_Mutable.self) mainCharacter = CharacterUnion(_v?.unpack(), type: .belle) case .bookfan: - var _v = _t.mainCharacter(type: BookReader_Mutable.self) + let _v = _t.mainCharacter(type: BookReader_Mutable.self) mainCharacter = CharacterUnion(_v?.unpack(), type: .bookfan) case .other: - var _v = _t.mainCharacter(type: String.self) + let _v = _t.mainCharacter(type: String.self) mainCharacter = CharacterUnion(_v?.unpack(), type: .other) case .unused: - var _v = _t.mainCharacter(type: String.self) + let _v = _t.mainCharacter(type: String.self) mainCharacter = CharacterUnion(_v?.unpack(), type: .unused) default: break } @@ -700,22 +691,22 @@ public class MovieT: NativeObject { for index in _charactersType.startIndex..<_charactersType.endIndex { switch _t.charactersType[index] { case .mulan: - var _v = _t.characters(at: Int32(index), type: Attacker.self) + let _v = _t.characters(at: Int32(index), type: Attacker.self) characters.append(CharacterUnion(_v?.unpack(), type: .mulan)) case .rapunzel: - var _v = _t.characters(at: Int32(index), type: Rapunzel_Mutable.self) + let _v = _t.characters(at: Int32(index), type: Rapunzel_Mutable.self) characters.append(CharacterUnion(_v?.unpack(), type: .rapunzel)) case .belle: - var _v = _t.characters(at: Int32(index), type: BookReader_Mutable.self) + let _v = _t.characters(at: Int32(index), type: BookReader_Mutable.self) characters.append(CharacterUnion(_v?.unpack(), type: .belle)) case .bookfan: - var _v = _t.characters(at: Int32(index), type: BookReader_Mutable.self) + let _v = _t.characters(at: Int32(index), type: BookReader_Mutable.self) characters.append(CharacterUnion(_v?.unpack(), type: .bookfan)) case .other: - var _v = _t.characters(at: Int32(index), type: String.self) + let _v = _t.characters(at: Int32(index), type: String.self) characters.append(CharacterUnion(_v?.unpack(), type: .other)) case .unused: - var _v = _t.characters(at: Int32(index), type: String.self) + let _v = _t.characters(at: Int32(index), type: String.self) characters.append(CharacterUnion(_v?.unpack(), type: .unused)) default: break } diff --git a/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift b/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift index ff1d94142..75dca157b 100644 --- a/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift +++ b/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift @@ -245,12 +245,10 @@ public struct MyGame_Example_Test: NativeStruct, FlatbuffersVectorInitializable, private var _a: Int16 private var _b: Int8 - private let padding0__: UInt8 = 0 + private var padding0__: UInt8 = 0 public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _a = _accessor.readBuffer(of: Int16.self, at: 0) - _b = _accessor.readBuffer(of: Int8.self, at: 2) + self = bb.read(def: Self.self, position: Int(o)) } public init(a: Int16, b: Int8) { @@ -263,7 +261,7 @@ public struct MyGame_Example_Test: NativeStruct, FlatbuffersVectorInitializable, _b = 0 } - public init(_ _t: inout MyGame_Example_Test_Mutable) { + public init(_ _t: borrowing MyGame_Example_Test_Mutable) { _a = _t.a _b = _t.b } @@ -305,10 +303,9 @@ public struct MyGame_Example_Test_Mutable: FlatBufferStruct, FlatbuffersVectorIn @discardableResult public func mutate(a: Int16) -> Bool { return _accessor.mutate(a, index: 0) } public var b: Int8 { return _accessor.readBuffer(of: Int8.self, at: 2) } @discardableResult public func mutate(b: Int8) -> Bool { return _accessor.mutate(b, index: 2) } - - public mutating func unpack() -> MyGame_Example_Test { - return MyGame_Example_Test(&self) + public func unpack() -> MyGame_Example_Test { + return MyGame_Example_Test(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_Test?) -> Offset { guard var obj = obj else { return Offset() } @@ -327,21 +324,15 @@ public struct MyGame_Example_Vec3: NativeStruct, FlatbuffersVectorInitializable, private var _x: Float32 private var _y: Float32 private var _z: Float32 - private let padding0__: UInt32 = 0 + private var padding0__: UInt32 = 0 private var _test1: Double private var _test2: UInt8 - private let padding1__: UInt8 = 0 + private var padding1__: UInt8 = 0 private var _test3: MyGame_Example_Test - private let padding2__: UInt16 = 0 + private var padding2__: UInt16 = 0 public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _x = _accessor.readBuffer(of: Float32.self, at: 0) - _y = _accessor.readBuffer(of: Float32.self, at: 4) - _z = _accessor.readBuffer(of: Float32.self, at: 8) - _test1 = _accessor.readBuffer(of: Double.self, at: 16) - _test2 = _accessor.readBuffer(of: UInt8.self, at: 24) - _test3 = MyGame_Example_Test(_accessor.bb, o: _accessor.position + 26) + self = bb.read(def: Self.self, position: Int(o)) } public init(x: Float32, y: Float32, z: Float32, test1: Double, test2: MyGame_Example_Color, test3: MyGame_Example_Test) { @@ -362,13 +353,13 @@ public struct MyGame_Example_Vec3: NativeStruct, FlatbuffersVectorInitializable, _test3 = MyGame_Example_Test() } - public init(_ _t: inout MyGame_Example_Vec3_Mutable) { + public init(_ _t: borrowing MyGame_Example_Vec3_Mutable) { _x = _t.x _y = _t.y _z = _t.z _test1 = _t.test1 _test2 = _t.test2.value - var _vtest3 = _t.test3 + let _vtest3 = _t.test3 _test3 = _vtest3.unpack() } @@ -434,10 +425,9 @@ public struct MyGame_Example_Vec3_Mutable: FlatBufferStruct, FlatbuffersVectorIn public var test2: MyGame_Example_Color { return MyGame_Example_Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: 24)) ?? .red } @discardableResult public func mutate(test2: MyGame_Example_Color) -> Bool { return _accessor.mutate(test2.rawValue, index: 24) } public var test3: MyGame_Example_Test_Mutable { return MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.position + 26) } - - public mutating func unpack() -> MyGame_Example_Vec3 { - return MyGame_Example_Vec3(&self) + public func unpack() -> MyGame_Example_Vec3 { + return MyGame_Example_Vec3(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_Vec3?) -> Offset { guard var obj = obj else { return Offset() } @@ -457,9 +447,7 @@ public struct MyGame_Example_Ability: NativeStruct, FlatbuffersVectorInitializab private var _distance: UInt32 public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _id = _accessor.readBuffer(of: UInt32.self, at: 0) - _distance = _accessor.readBuffer(of: UInt32.self, at: 4) + self = bb.read(def: Self.self, position: Int(o)) } public init(id: UInt32, distance: UInt32) { @@ -472,7 +460,7 @@ public struct MyGame_Example_Ability: NativeStruct, FlatbuffersVectorInitializab _distance = 0 } - public init(_ _t: inout MyGame_Example_Ability_Mutable) { + public init(_ _t: borrowing MyGame_Example_Ability_Mutable) { _id = _t.id _distance = _t.distance } @@ -514,10 +502,9 @@ public struct MyGame_Example_Ability_Mutable: FlatBufferStruct, FlatbuffersVecto @discardableResult public func mutate(id: UInt32) -> Bool { return _accessor.mutate(id, index: 0) } public var distance: UInt32 { return _accessor.readBuffer(of: UInt32.self, at: 4) } @discardableResult public func mutate(distance: UInt32) -> Bool { return _accessor.mutate(distance, index: 4) } - - public mutating func unpack() -> MyGame_Example_Ability { - return MyGame_Example_Ability(&self) + public func unpack() -> MyGame_Example_Ability { + return MyGame_Example_Ability(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_Ability?) -> Offset { guard var obj = obj else { return Offset() } @@ -538,10 +525,7 @@ public struct MyGame_Example_StructOfStructs: NativeStruct, FlatbuffersVectorIni private var _c: MyGame_Example_Ability public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _a = MyGame_Example_Ability(_accessor.bb, o: _accessor.position + 0) - _b = MyGame_Example_Test(_accessor.bb, o: _accessor.position + 8) - _c = MyGame_Example_Ability(_accessor.bb, o: _accessor.position + 12) + self = bb.read(def: Self.self, position: Int(o)) } public init(a: MyGame_Example_Ability, b: MyGame_Example_Test, c: MyGame_Example_Ability) { @@ -556,12 +540,12 @@ public struct MyGame_Example_StructOfStructs: NativeStruct, FlatbuffersVectorIni _c = MyGame_Example_Ability() } - public init(_ _t: inout MyGame_Example_StructOfStructs_Mutable) { - var _va = _t.a + public init(_ _t: borrowing MyGame_Example_StructOfStructs_Mutable) { + let _va = _t.a _a = _va.unpack() - var _vb = _t.b + let _vb = _t.b _b = _vb.unpack() - var _vc = _t.c + let _vc = _t.c _c = _vc.unpack() } @@ -600,10 +584,9 @@ public struct MyGame_Example_StructOfStructs_Mutable: FlatBufferStruct, Flatbuff public var a: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.position + 0) } public var b: MyGame_Example_Test_Mutable { return MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.position + 8) } public var c: MyGame_Example_Ability_Mutable { return MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.position + 12) } - - public mutating func unpack() -> MyGame_Example_StructOfStructs { - return MyGame_Example_StructOfStructs(&self) + public func unpack() -> MyGame_Example_StructOfStructs { + return MyGame_Example_StructOfStructs(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_StructOfStructs?) -> Offset { guard var obj = obj else { return Offset() } @@ -622,8 +605,7 @@ public struct MyGame_Example_StructOfStructsOfStructs: NativeStruct, Flatbuffers private var _a: MyGame_Example_StructOfStructs public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _a = MyGame_Example_StructOfStructs(_accessor.bb, o: _accessor.position + 0) + self = bb.read(def: Self.self, position: Int(o)) } public init(a: MyGame_Example_StructOfStructs) { @@ -634,8 +616,8 @@ public struct MyGame_Example_StructOfStructsOfStructs: NativeStruct, Flatbuffers _a = MyGame_Example_StructOfStructs() } - public init(_ _t: inout MyGame_Example_StructOfStructsOfStructs_Mutable) { - var _va = _t.a + public init(_ _t: borrowing MyGame_Example_StructOfStructsOfStructs_Mutable) { + let _va = _t.a _a = _va.unpack() } @@ -666,10 +648,9 @@ public struct MyGame_Example_StructOfStructsOfStructs_Mutable: FlatBufferStruct, public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) } public var a: MyGame_Example_StructOfStructs_Mutable { return MyGame_Example_StructOfStructs_Mutable(_accessor.bb, o: _accessor.position + 0) } - - public mutating func unpack() -> MyGame_Example_StructOfStructsOfStructs { - return MyGame_Example_StructOfStructsOfStructs(&self) + public func unpack() -> MyGame_Example_StructOfStructsOfStructs { + return MyGame_Example_StructOfStructsOfStructs(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_StructOfStructsOfStructs?) -> Offset { guard var obj = obj else { return Offset() } @@ -694,10 +675,9 @@ public struct MyGame_InParentNamespace: FlatBufferTable, FlatbuffersVectorInitia public static func startInParentNamespace(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 0) } public static func endInParentNamespace(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end } - - public mutating func unpack() -> MyGame_InParentNamespaceT { - return MyGame_InParentNamespaceT(&self) + public func unpack() -> MyGame_InParentNamespaceT { + return MyGame_InParentNamespaceT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_InParentNamespaceT?) -> Offset { guard var obj = obj else { return Offset() } @@ -724,7 +704,7 @@ extension MyGame_InParentNamespace: Encodable { public class MyGame_InParentNamespaceT: NativeObject { - public init(_ _t: inout MyGame_InParentNamespace) { + public init(_ _t: borrowing MyGame_InParentNamespace) { } public init() { @@ -746,10 +726,9 @@ public struct MyGame_Example2_Monster: FlatBufferTable, FlatbuffersVectorInitial public static func startMonster(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 0) } public static func endMonster(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end } - - public mutating func unpack() -> MyGame_Example2_MonsterT { - return MyGame_Example2_MonsterT(&self) + public func unpack() -> MyGame_Example2_MonsterT { + return MyGame_Example2_MonsterT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example2_MonsterT?) -> Offset { guard var obj = obj else { return Offset() } @@ -776,7 +755,7 @@ extension MyGame_Example2_Monster: Encodable { public class MyGame_Example2_MonsterT: NativeObject { - public init(_ _t: inout MyGame_Example2_Monster) { + public init(_ _t: borrowing MyGame_Example2_Monster) { } public init() { @@ -815,10 +794,9 @@ internal struct MyGame_Example_TestSimpleTableWithEnum: FlatBufferTable, Flatbuf MyGame_Example_TestSimpleTableWithEnum.add(color: color, &fbb) return MyGame_Example_TestSimpleTableWithEnum.endTestSimpleTableWithEnum(&fbb, start: __start) } - - internal mutating func unpack() -> MyGame_Example_TestSimpleTableWithEnumT { - return MyGame_Example_TestSimpleTableWithEnumT(&self) + internal func unpack() -> MyGame_Example_TestSimpleTableWithEnumT { + return MyGame_Example_TestSimpleTableWithEnumT(self) } internal static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_TestSimpleTableWithEnumT?) -> Offset { guard var obj = obj else { return Offset() } @@ -855,7 +833,7 @@ internal class MyGame_Example_TestSimpleTableWithEnumT: NativeObject { internal var color: MyGame_Example_Color - internal init(_ _t: inout MyGame_Example_TestSimpleTableWithEnum) { + internal init(_ _t: borrowing MyGame_Example_TestSimpleTableWithEnum) { color = _t.color } @@ -932,10 +910,9 @@ public struct MyGame_Example_Stat: FlatBufferTable, FlatbuffersVectorInitializab } return nil } - - public mutating func unpack() -> MyGame_Example_StatT { - return MyGame_Example_StatT(&self) + public func unpack() -> MyGame_Example_StatT { + return MyGame_Example_StatT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_StatT?) -> Offset { guard var obj = obj else { return Offset() } @@ -991,7 +968,7 @@ public class MyGame_Example_StatT: NativeObject { public var val: Int64 public var count: UInt16 - public init(_ _t: inout MyGame_Example_Stat) { + public init(_ _t: borrowing MyGame_Example_Stat) { id = _t.id val = _t.val count = _t.count @@ -1059,10 +1036,9 @@ public struct MyGame_Example_Referrable: FlatBufferTable, FlatbuffersVectorIniti } return nil } - - public mutating func unpack() -> MyGame_Example_ReferrableT { - return MyGame_Example_ReferrableT(&self) + public func unpack() -> MyGame_Example_ReferrableT { + return MyGame_Example_ReferrableT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_ReferrableT?) -> Offset { guard var obj = obj else { return Offset() } @@ -1099,7 +1075,7 @@ public class MyGame_Example_ReferrableT: NativeObject { public var id: UInt64 - public init(_ _t: inout MyGame_Example_Referrable) { + public init(_ _t: borrowing MyGame_Example_Referrable) { id = _t.id } @@ -1539,10 +1515,9 @@ public struct MyGame_Example_Monster: FlatBufferTable, FlatbuffersVectorInitiali } return nil } - - public mutating func unpack() -> MyGame_Example_MonsterT { - return MyGame_Example_MonsterT(&self) + public func unpack() -> MyGame_Example_MonsterT { + return MyGame_Example_MonsterT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_MonsterT?) -> Offset { guard var obj = obj else { return Offset() } @@ -2065,7 +2040,7 @@ public class MyGame_Example_MonsterT: NativeObject { public var negativeInfinityDefault: Float32 public var doubleInfDefault: Double - public init(_ _t: inout MyGame_Example_Monster) { + public init(_ _t: borrowing MyGame_Example_Monster) { pos = _t.pos mana = _t.mana hp = _t.hp @@ -2075,13 +2050,13 @@ public class MyGame_Example_MonsterT: NativeObject { color = _t.color switch _t.testType { case .monster: - var _v = _t.test(type: MyGame_Example_Monster.self) + let _v = _t.test(type: MyGame_Example_Monster.self) test = MyGame_Example_Any_Union(_v?.unpack(), type: .monster) case .testsimpletablewithenum: - var _v = _t.test(type: MyGame_Example_TestSimpleTableWithEnum.self) + let _v = _t.test(type: MyGame_Example_TestSimpleTableWithEnum.self) test = MyGame_Example_Any_Union(_v?.unpack(), type: .testsimpletablewithenum) case .mygameExample2Monster: - var _v = _t.test(type: MyGame_Example2_Monster.self) + let _v = _t.test(type: MyGame_Example2_Monster.self) test = MyGame_Example_Any_Union(_v?.unpack(), type: .mygameExample2Monster) default: break } @@ -2090,15 +2065,13 @@ public class MyGame_Example_MonsterT: NativeObject { testarrayofstring = [] testarrayofstring.append(contentsOf: _t.testarrayofstring) testarrayoftables = [] - for var val in _t.testarrayoftables{ + for val in _t.testarrayoftables{ testarrayoftables.append(val.unpack()) } - var __enemy = _t.enemy - enemy = __enemy?.unpack() + enemy = _t.enemy?.unpack() testnestedflatbuffer = [] testnestedflatbuffer.append(contentsOf: _t.testnestedflatbuffer) - var __testempty = _t.testempty - testempty = __testempty?.unpack() + testempty = _t.testempty?.unpack() testbool = _t.testbool testhashs32Fnv1 = _t.testhashs32Fnv1 testhashu32Fnv1 = _t.testhashu32Fnv1 @@ -2125,17 +2098,16 @@ public class MyGame_Example_MonsterT: NativeObject { vectorOfLongs.append(contentsOf: _t.vectorOfLongs) vectorOfDoubles = [] vectorOfDoubles.append(contentsOf: _t.vectorOfDoubles) - var __parentNamespaceTest = _t.parentNamespaceTest - parentNamespaceTest = __parentNamespaceTest?.unpack() + parentNamespaceTest = _t.parentNamespaceTest?.unpack() vectorOfReferrables = [] - for var val in _t.vectorOfReferrables{ + for val in _t.vectorOfReferrables{ vectorOfReferrables.append(val.unpack()) } singleWeakReference = _t.singleWeakReference vectorOfWeakReferences = [] vectorOfWeakReferences.append(contentsOf: _t.vectorOfWeakReferences) vectorOfStrongReferrables = [] - for var val in _t.vectorOfStrongReferrables{ + for val in _t.vectorOfStrongReferrables{ vectorOfStrongReferrables.append(val.unpack()) } coOwningReference = _t.coOwningReference @@ -2146,25 +2118,25 @@ public class MyGame_Example_MonsterT: NativeObject { vectorOfNonOwningReferences.append(contentsOf: _t.vectorOfNonOwningReferences) switch _t.anyUniqueType { case .m: - var _v = _t.anyUnique(type: MyGame_Example_Monster.self) + let _v = _t.anyUnique(type: MyGame_Example_Monster.self) anyUnique = MyGame_Example_AnyUniqueAliasesUnion(_v?.unpack(), type: .m) case .ts: - var _v = _t.anyUnique(type: MyGame_Example_TestSimpleTableWithEnum.self) + let _v = _t.anyUnique(type: MyGame_Example_TestSimpleTableWithEnum.self) anyUnique = MyGame_Example_AnyUniqueAliasesUnion(_v?.unpack(), type: .ts) case .m2: - var _v = _t.anyUnique(type: MyGame_Example2_Monster.self) + let _v = _t.anyUnique(type: MyGame_Example2_Monster.self) anyUnique = MyGame_Example_AnyUniqueAliasesUnion(_v?.unpack(), type: .m2) default: break } switch _t.anyAmbiguousType { case .m1: - var _v = _t.anyAmbiguous(type: MyGame_Example_Monster.self) + let _v = _t.anyAmbiguous(type: MyGame_Example_Monster.self) anyAmbiguous = MyGame_Example_AnyAmbiguousAliasesUnion(_v?.unpack(), type: .m1) case .m2: - var _v = _t.anyAmbiguous(type: MyGame_Example_Monster.self) + let _v = _t.anyAmbiguous(type: MyGame_Example_Monster.self) anyAmbiguous = MyGame_Example_AnyAmbiguousAliasesUnion(_v?.unpack(), type: .m2) case .m3: - var _v = _t.anyAmbiguous(type: MyGame_Example_Monster.self) + let _v = _t.anyAmbiguous(type: MyGame_Example_Monster.self) anyAmbiguous = MyGame_Example_AnyAmbiguousAliasesUnion(_v?.unpack(), type: .m3) default: break } @@ -2174,7 +2146,7 @@ public class MyGame_Example_MonsterT: NativeObject { testrequirednestedflatbuffer = [] testrequirednestedflatbuffer.append(contentsOf: _t.testrequirednestedflatbuffer) scalarKeySortedTables = [] - for var val in _t.scalarKeySortedTables{ + for val in _t.scalarKeySortedTables{ scalarKeySortedTables.append(val.unpack()) } nativeInline = _t.nativeInline @@ -2349,10 +2321,9 @@ public struct MyGame_Example_TypeAliases: FlatBufferTable, FlatbuffersVectorInit MyGame_Example_TypeAliases.addVectorOf(vf64: vf64, &fbb) return MyGame_Example_TypeAliases.endTypeAliases(&fbb, start: __start) } - - public mutating func unpack() -> MyGame_Example_TypeAliasesT { - return MyGame_Example_TypeAliasesT(&self) + public func unpack() -> MyGame_Example_TypeAliasesT { + return MyGame_Example_TypeAliasesT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Example_TypeAliasesT?) -> Offset { guard var obj = obj else { return Offset() } @@ -2464,7 +2435,7 @@ public class MyGame_Example_TypeAliasesT: NativeObject { public var v8: [Int8] public var vf64: [Double] - public init(_ _t: inout MyGame_Example_TypeAliases) { + public init(_ _t: borrowing MyGame_Example_TypeAliases) { i8 = _t.i8 u8 = _t.u8 i16 = _t.i16 diff --git a/tests/swift/fuzzer/CodeGenerationTests/test_import_generated.swift b/tests/swift/fuzzer/CodeGenerationTests/test_import_generated.swift index 6251d1a84..9b33aa626 100644 --- a/tests/swift/fuzzer/CodeGenerationTests/test_import_generated.swift +++ b/tests/swift/fuzzer/CodeGenerationTests/test_import_generated.swift @@ -36,10 +36,9 @@ internal struct Message: FlatBufferTable, FlatbuffersVectorInitializable, Verifi Message.add(internalMessage: internalMessage, &fbb) return Message.endMessage(&fbb, start: __start) } - - internal mutating func unpack() -> MessageT { - return MessageT(&self) + internal func unpack() -> MessageT { + return MessageT(self) } internal static func pack(_ builder: inout FlatBufferBuilder, obj: inout MessageT?) -> Offset { guard var obj = obj else { return Offset() } @@ -81,7 +80,7 @@ internal class MessageT: NativeObject { internal var internalMessage: String? - internal init(_ _t: inout Message) { + internal init(_ _t: borrowing Message) { internalMessage = _t.internalMessage } diff --git a/tests/swift/fuzzer/CodeGenerationTests/test_no_include_generated.swift b/tests/swift/fuzzer/CodeGenerationTests/test_no_include_generated.swift index a3e7d1938..0635d9f47 100644 --- a/tests/swift/fuzzer/CodeGenerationTests/test_no_include_generated.swift +++ b/tests/swift/fuzzer/CodeGenerationTests/test_no_include_generated.swift @@ -9,8 +9,7 @@ public struct BytesCount: NativeStruct, FlatbuffersVectorInitializable, Verifiab private var _x: Int64 public init(_ bb: ByteBuffer, o: Int32) { - let _accessor = Struct(bb: bb, position: o) - _x = _accessor.readBuffer(of: Int64.self, at: 0) + self = bb.read(def: Self.self, position: Int(o)) } public init(x: Int64) { @@ -21,7 +20,7 @@ public struct BytesCount: NativeStruct, FlatbuffersVectorInitializable, Verifiab _x = 0 } - public init(_ _t: inout BytesCount_Mutable) { + public init(_ _t: borrowing BytesCount_Mutable) { _x = _t.x } @@ -55,10 +54,9 @@ public struct BytesCount_Mutable: FlatBufferStruct, FlatbuffersVectorInitializab public var x: Int64 { return _accessor.readBuffer(of: Int64.self, at: 0) } @discardableResult public func mutate(x: Int64) -> Bool { return _accessor.mutate(x, index: 0) } - - public mutating func unpack() -> BytesCount { - return BytesCount(&self) + public func unpack() -> BytesCount { + return BytesCount(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout BytesCount?) -> Offset { guard var obj = obj else { return Offset() } @@ -98,10 +96,9 @@ public struct InternalMessage: FlatBufferTable, FlatbuffersVectorInitializable, InternalMessage.add(str: str, &fbb) return InternalMessage.endInternalMessage(&fbb, start: __start) } - - public mutating func unpack() -> InternalMessageT { - return InternalMessageT(&self) + public func unpack() -> InternalMessageT { + return InternalMessageT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout InternalMessageT?) -> Offset { guard var obj = obj else { return Offset() } @@ -143,7 +140,7 @@ public class InternalMessageT: NativeObject { public var str: String? - public init(_ _t: inout InternalMessage) { + public init(_ _t: borrowing InternalMessage) { str = _t.str } @@ -192,10 +189,9 @@ public struct Message: FlatBufferTable, FlatbuffersVectorInitializable, Verifiab Message.add(pointer: pointer, &fbb) return Message.endMessage(&fbb, start: __start) } - - public mutating func unpack() -> MessageT { - return MessageT(&self) + public func unpack() -> MessageT { + return MessageT(self) } public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MessageT?) -> Offset { guard var obj = obj else { return Offset() } @@ -243,11 +239,10 @@ public class MessageT: NativeObject { public var position: BytesCount public var pointer: InternalMessageT - public init(_ _t: inout Message) { + public init(_ _t: borrowing Message) { id = _t.id position = _t.position - var __pointer = _t.pointer - pointer = __pointer!.unpack() + pointer = _t.pointer!.unpack() } public init() {