-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a serialize helper function to native table (#6059)
* Adds a serialize helper function to native table * Updated version
- Loading branch information
mustiikhalil
authored
Aug 4, 2020
1 parent
c30a87d
commit 63cc0ee
Showing
7 changed files
with
86 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Foundation | ||
|
||
public protocol NativeTable {} | ||
|
||
extension NativeTable { | ||
|
||
/// Serialize is a helper function that serailizes the data from the Object API to a bytebuffer directly th | ||
/// - Parameter type: Type of the Flatbuffer object | ||
/// - Returns: returns the encoded sized ByteBuffer | ||
public func serialize<T: ObjectAPI>(type: T.Type) -> ByteBuffer where T.T == Self { | ||
var builder = FlatBufferBuilder(initialSize: 1024) | ||
return serialize(builder: &builder, type: type.self) | ||
} | ||
|
||
/// Serialize is a helper function that serailizes the data from the Object API to a bytebuffer directly. | ||
/// | ||
/// - Parameters: | ||
/// - builder: A FlatBufferBuilder | ||
/// - type: Type of the Flatbuffer object | ||
/// - Returns: returns the encoded sized ByteBuffer | ||
/// - Note: The `serialize(builder:type)` can be considered as a function that allows you to create smaller builder instead of the default `1024`. | ||
/// It can be considered less expensive in terms of memory allocation | ||
public func serialize<T: ObjectAPI>(builder: inout FlatBufferBuilder, type: T.Type) -> ByteBuffer where T.T == Self { | ||
var s = self | ||
let root = type.pack(&builder, obj: &s) | ||
builder.finish(offset: root) | ||
return builder.sizedBuffer | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters