mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-06 13:37:25 +00:00
Code cleanup + updates test and readme (#6004)
Updates Readme + adds asserts Fixes documentation
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'FlatBuffers'
|
||||
s.version = '0.5.3'
|
||||
s.version = '0.6.0'
|
||||
s.summary = 'FlatBuffers: Memory Efficient Serialization Library'
|
||||
|
||||
s.description = "FlatBuffers is a cross platform serialization library architected for
|
||||
|
||||
@@ -7,5 +7,11 @@ and Cocoapods
|
||||
`pod 'FlatBuffers'`
|
||||
|
||||
### Notes
|
||||
|
||||
1- To report any error please use the main repository.
|
||||
2- The package 0.4.0 will break the generated code. You can download the [binary here](https://github.com/google/flatbuffers/actions) and select the latest push to master
|
||||
|
||||
2- `0.6.0` deprecates `add(condition:bool)` for `add(element:bool)`. You can download the [binary here](https://github.com/google/flatbuffers/actions) and select the latest push to master
|
||||
|
||||
### Contribute
|
||||
|
||||
1- Always run `swift test --generate-linuxmain` whenever new test functions are added or removed
|
||||
@@ -31,12 +31,12 @@ public struct ByteBuffer {
|
||||
}
|
||||
|
||||
func copy(from ptr: UnsafeRawPointer, count: Int) {
|
||||
precondition(!unowned)
|
||||
assert(!unowned, "copy should NOT be called on a buffer that is built by assumingMemoryBound")
|
||||
memory.copyMemory(from: ptr, byteCount: count)
|
||||
}
|
||||
|
||||
func initialize(for size: Int) {
|
||||
precondition(!unowned)
|
||||
assert(!unowned, "initalize should NOT be called on a buffer that is built by assumingMemoryBound")
|
||||
memset(memory, 0, size)
|
||||
}
|
||||
|
||||
@@ -107,15 +107,7 @@ public struct ByteBuffer {
|
||||
_storage.initialize(for: size)
|
||||
}
|
||||
|
||||
/// Constructor that creates a Flatbuffer from unsafe memory region without copying
|
||||
/// - Parameter assumingMemoryBound: The unsafe memory region
|
||||
/// - Parameter capacity: The size of the given memory region
|
||||
public init(assumingMemoryBound memory: UnsafeMutableRawPointer, capacity: Int) {
|
||||
_storage = Storage(memory: memory, capacity: capacity, unowned: true)
|
||||
_writerSize = capacity
|
||||
}
|
||||
|
||||
#if swift(>=5.0)
|
||||
#if swift(>=5.0)
|
||||
/// Constructor that creates a Flatbuffer object from a ContiguousBytes
|
||||
/// - Parameters:
|
||||
/// - contiguousBytes: Binary stripe to use as the buffer
|
||||
@@ -130,7 +122,15 @@ public struct ByteBuffer {
|
||||
_storage.copy(from: buf.baseAddress!, count: buf.count)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/// Constructor that creates a Flatbuffer from unsafe memory region without copying
|
||||
/// - Parameter assumingMemoryBound: The unsafe memory region
|
||||
/// - Parameter capacity: The size of the given memory region
|
||||
public init(assumingMemoryBound memory: UnsafeMutableRawPointer, capacity: Int) {
|
||||
_storage = Storage(memory: memory, capacity: capacity, unowned: true)
|
||||
_writerSize = capacity
|
||||
}
|
||||
|
||||
/// Creates a copy of the buffer that's being built by calling sizedBuffer
|
||||
/// - Parameters:
|
||||
@@ -156,6 +156,7 @@ public struct ByteBuffer {
|
||||
/// Fills the buffer with padding by adding to the writersize
|
||||
/// - Parameter padding: Amount of padding between two to be serialized objects
|
||||
@usableFromInline mutating func fill(padding: Int) {
|
||||
assert(padding >= 0, "Fill should be larger than or equal to zero")
|
||||
ensureSpace(size: padding)
|
||||
_writerSize = _writerSize &+ (MemoryLayout<UInt8>.size &* padding)
|
||||
}
|
||||
@@ -169,16 +170,6 @@ public struct ByteBuffer {
|
||||
push(value: s, len: MemoryLayout.size(ofValue: s))
|
||||
}
|
||||
}
|
||||
|
||||
///Adds an array of type Bool to the buffer memory
|
||||
/// - Parameter elements: An array of Bool
|
||||
@usableFromInline mutating func push(elements: [Bool]) {
|
||||
let size = elements.count &* MemoryLayout<Bool>.size
|
||||
ensureSpace(size: size)
|
||||
elements.lazy.reversed().forEach { (s) in
|
||||
push(value: s ? 1 : 0, len: MemoryLayout.size(ofValue: s))
|
||||
}
|
||||
}
|
||||
|
||||
/// A custom type of structs that are padded according to the flatbuffer padding,
|
||||
/// - Parameters:
|
||||
@@ -239,6 +230,8 @@ public struct ByteBuffer {
|
||||
if !direct {
|
||||
index = _storage.capacity &- index
|
||||
}
|
||||
assert(index < _storage.capacity, "Write index is out of writing bound")
|
||||
assert(index >= 0, "Writer index should be above zero")
|
||||
_storage.memory.storeBytes(of: value, toByteOffset: index, as: T.self)
|
||||
}
|
||||
|
||||
@@ -253,6 +246,17 @@ public struct ByteBuffer {
|
||||
return size
|
||||
}
|
||||
|
||||
/// Resizes the buffer size
|
||||
/// - Parameter size: new size for the buffer
|
||||
@usableFromInline mutating internal func resize(_ size: Int) {
|
||||
assert((_writerSize &- size) > 0, "New size should NOT be a negative number")
|
||||
var zero: UInt8 = 0
|
||||
for i in 0..<(_writerSize &- size) {
|
||||
memcpy(_storage.memory.advanced(by: writerIndex &+ i), &zero, MemoryLayout<UInt8>.size)
|
||||
}
|
||||
_writerSize = size
|
||||
}
|
||||
|
||||
/// Clears the current size of the buffer
|
||||
mutating public func clearSize() {
|
||||
_writerSize = 0
|
||||
@@ -267,22 +271,12 @@ public struct ByteBuffer {
|
||||
_storage.initialize(for: _storage.capacity)
|
||||
}
|
||||
|
||||
/// Resizes the buffer size
|
||||
/// - Parameter size: new size for the buffer
|
||||
@usableFromInline mutating internal func resize(_ size: Int) {
|
||||
assert((_writerSize &- size) > 0)
|
||||
var zero: UInt8 = 0
|
||||
for i in 0..<(_writerSize &- size) {
|
||||
memcpy(_storage.memory.advanced(by: writerIndex &+ i), &zero, MemoryLayout<UInt8>.size)
|
||||
}
|
||||
_writerSize = size
|
||||
}
|
||||
|
||||
/// Reads an object from the buffer
|
||||
/// - Parameters:
|
||||
/// - def: Type of the object
|
||||
/// - position: the index of the object in the buffer
|
||||
public func read<T>(def: T.Type, position: Int) -> T {
|
||||
assert(position < _storage.capacity, "Reading out of bounds is illegal")
|
||||
return _storage.memory.advanced(by: position).load(as: T.self)
|
||||
}
|
||||
|
||||
@@ -292,7 +286,9 @@ public struct ByteBuffer {
|
||||
/// - count: count of bytes in memory
|
||||
public func readSlice<T>(index: Int32,
|
||||
count: Int32) -> [T] {
|
||||
let start = _storage.memory.advanced(by: Int(index)).assumingMemoryBound(to: T.self)
|
||||
let _index = Int(index)
|
||||
assert(_index < _storage.capacity, "Reading out of bounds is illegal")
|
||||
let start = _storage.memory.advanced(by: _index).assumingMemoryBound(to: T.self)
|
||||
let array = UnsafeBufferPointer(start: start, count: Int(count))
|
||||
return Array(array)
|
||||
}
|
||||
@@ -305,7 +301,9 @@ public struct ByteBuffer {
|
||||
public func readString(at index: Int32,
|
||||
count: Int32,
|
||||
type: String.Encoding = .utf8) -> String? {
|
||||
let start = _storage.memory.advanced(by: Int(index)).assumingMemoryBound(to: UInt8.self)
|
||||
let _index = Int(index)
|
||||
assert(_index < _storage.capacity, "Reading out of bounds is illegal")
|
||||
let start = _storage.memory.advanced(by: _index).assumingMemoryBound(to: UInt8.self)
|
||||
let bufprt = UnsafeBufferPointer(start: start, count: Int(count))
|
||||
return String(bytes: Array(bufprt), encoding: type)
|
||||
}
|
||||
@@ -313,6 +311,8 @@ public struct ByteBuffer {
|
||||
/// Creates a new Flatbuffer object that's duplicated from the current one
|
||||
/// - Parameter removeBytes: the amount of bytes to remove from the current Size
|
||||
public func duplicate(removing removeBytes: Int = 0) -> ByteBuffer {
|
||||
assert(removeBytes > 0, "Can NOT remove negative bytes")
|
||||
assert(removeBytes < _storage.capacity, "Can NOT remove more bytes than the ones allocated")
|
||||
return ByteBuffer(memory: _storage.memory, count: _storage.capacity, removing: _writerSize &- removeBytes)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ public struct FlatBufferBuilder {
|
||||
}
|
||||
/// Returns the written size of the buffer
|
||||
public var sizedByteArray: [UInt8] {
|
||||
assert(finished, "Data shouldn't be called before finish()")
|
||||
let cp = _bb.capacity &- _bb.writerIndex
|
||||
let start = _bb.memory.advanced(by: _bb.writerIndex)
|
||||
.bindMemory(to: UInt8.self, capacity: cp)
|
||||
@@ -439,22 +440,7 @@ public struct FlatBufferBuilder {
|
||||
if (element == def && !serializeDefaults) { return }
|
||||
track(offset: push(element: element), at: position)
|
||||
}
|
||||
|
||||
/// Adds Boolean values into the buffer
|
||||
/// - Parameters:
|
||||
/// - condition: Condition to insert
|
||||
/// - def: Default condition
|
||||
/// - position: The predefined position of the element
|
||||
@available(*, deprecated, message: "Deprecated, function will be removed in Flatbuffers v0.6.0. Regenerate code")
|
||||
mutating public func add(condition: Bool, def: Bool, at position: VOffset) {
|
||||
if (condition == def && !serializeDefaults) {
|
||||
track(offset: 0, at: position)
|
||||
return
|
||||
}
|
||||
let off = push(element: Byte(condition ? 1 : 0))
|
||||
track(offset: off, at: position)
|
||||
}
|
||||
|
||||
|
||||
/// Pushes the values into the buffer
|
||||
/// - Parameter element: Element to insert
|
||||
/// - returns: Postion of the Element
|
||||
@@ -513,6 +499,7 @@ extension FlatBufferBuilder: CustomDebugStringConvertible {
|
||||
/// Builds a buffer with byte count of fieldloc.size * count of field numbers
|
||||
/// - Parameter count: number of fields to be written
|
||||
func start(count: Int) {
|
||||
assert(count >= 0, "number of fields should NOT be negative")
|
||||
let capacity = count &* size
|
||||
ensure(space: capacity)
|
||||
}
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
# FlatBuffers.GRPC.Swift
|
||||
|
||||
The following is Swift example on how GRPC would be with Swift Flatbuffers
|
||||
The following is Swift example on how GRPC would be with Swift Flatbuffers, you can simply run the following commands:
|
||||
|
||||
`swift run Server`
|
||||
|
||||
`swift run Client {port} {name}`
|
||||
|
||||
@@ -34,13 +34,6 @@ final class FlatBuffersTests: XCTestCase {
|
||||
XCTAssertEqual(b.startTable(with: 0), 12)
|
||||
}
|
||||
|
||||
func testCreate() {
|
||||
var b = FlatBufferBuilder(initialSize: 16)
|
||||
_ = Country.createCountry(builder: &b, name: country, log: 200, lan: 100)
|
||||
let v: [UInt8] = [10, 0, 16, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 12, 0, 0, 0, 100, 0, 0, 0, 200, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0]
|
||||
XCTAssertEqual(b.sizedByteArray, v)
|
||||
}
|
||||
|
||||
func testCreateFinish() {
|
||||
var b = FlatBufferBuilder(initialSize: 16)
|
||||
let countryOff = Country.createCountry(builder: &b, name: country, log: 200, lan: 100)
|
||||
|
||||
@@ -5,13 +5,6 @@ final class FlatBuffersDoubleTests: XCTestCase {
|
||||
|
||||
let country = "Norway"
|
||||
|
||||
func testCreateCountry() {
|
||||
var b = FlatBufferBuilder(initialSize: 16)
|
||||
_ = CountryDouble.createCountry(builder: &b, name: country, log: 200, lan: 100)
|
||||
let v: [UInt8] = [10, 0, 28, 0, 4, 0, 8, 0, 16, 0, 10, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89, 64, 0, 0, 0, 0, 0, 0, 105, 64, 0, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0]
|
||||
XCTAssertEqual(b.sizedByteArray, v)
|
||||
}
|
||||
|
||||
func testCreateFinish() {
|
||||
var b = FlatBufferBuilder(initialSize: 16)
|
||||
let countryOff = CountryDouble.createCountry(builder: &b, name: country, log: 200, lan: 100)
|
||||
|
||||
@@ -6,7 +6,6 @@ extension FlatBuffersDoubleTests {
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__FlatBuffersDoubleTests = [
|
||||
("testCreateCountry", testCreateCountry),
|
||||
("testCreateFinish", testCreateFinish),
|
||||
("testCreateFinishWithPrefix", testCreateFinishWithPrefix),
|
||||
]
|
||||
@@ -48,7 +47,6 @@ extension FlatBuffersTests {
|
||||
// `swift test --generate-linuxmain`
|
||||
// to regenerate.
|
||||
static let __allTests__FlatBuffersTests = [
|
||||
("testCreate", testCreate),
|
||||
("testCreateFinish", testCreateFinish),
|
||||
("testCreateFinishWithPrefix", testCreateFinishWithPrefix),
|
||||
("testCreateString", testCreateString),
|
||||
|
||||
Reference in New Issue
Block a user