Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 1 | import Foundation |
| 2 | |
| 3 | public protocol NativeTable {} |
| 4 | |
| 5 | extension NativeTable { |
| 6 | |
| 7 | /// Serialize is a helper function that serailizes the data from the Object API to a bytebuffer directly th |
| 8 | /// - Parameter type: Type of the Flatbuffer object |
| 9 | /// - Returns: returns the encoded sized ByteBuffer |
| 10 | public func serialize<T: ObjectAPI>(type: T.Type) -> ByteBuffer where T.T == Self { |
| 11 | var builder = FlatBufferBuilder(initialSize: 1024) |
| 12 | return serialize(builder: &builder, type: type.self) |
| 13 | } |
| 14 | |
| 15 | /// Serialize is a helper function that serailizes the data from the Object API to a bytebuffer directly. |
| 16 | /// |
| 17 | /// - Parameters: |
| 18 | /// - builder: A FlatBufferBuilder |
| 19 | /// - type: Type of the Flatbuffer object |
| 20 | /// - Returns: returns the encoded sized ByteBuffer |
| 21 | /// - Note: The `serialize(builder:type)` can be considered as a function that allows you to create smaller builder instead of the default `1024`. |
| 22 | /// It can be considered less expensive in terms of memory allocation |
| 23 | public func serialize<T: ObjectAPI>(builder: inout FlatBufferBuilder, type: T.Type) -> ByteBuffer where T.T == Self { |
| 24 | var s = self |
| 25 | let root = type.pack(&builder, obj: &s) |
| 26 | builder.finish(offset: root) |
| 27 | return builder.sizedBuffer |
| 28 | } |
| 29 | } |