5.7.0
FlatSharp 5.7.0 adds the most commonly requested feature: Value-type structs. Value type structs can be used from both C# and from .fbs files, but work best from FBS files:
struct ValueStruct (fs_valueStruct)
{
A : int;
B : ulong;
Vector : [ ubyte : 32 ];
}
FlatSharp value structs...
- Can only contain other value types (value structs and scalars). You cannot embed reference structs inside value structs. However, value structs can be embedded in reference structs.
- Support struct vectors. Due to value type semantics, the implementation is a little different than struct vectors for reference structs. The struct vectors sample has more detail.
- Are generally serialized and deserialized with a memcopy operation. Here's a typical value struct serializer:
Span<byte> sizedSpan = span.Slice(offset, 41);
if (BitConverter.IsLittleEndian)
{
var tempSpan = MemoryMarshal.Cast<byte, Vec3Value>(sizedSpan);
// Assign the struct into the underlying buffer. No need to write field-by-field since the struct's
// memory layout is the same as the flatbuffer layout!
tempSpan[0] = value;
}
else
{
WriteInlineValueOf(spanWriter, sizedSpan, value.temp, 0);
...
}
- Are never incrementally deserialized (it's not possible to only deserialize
valueStruct.Property1
-- all ofvalueStruct
is read and then you can accessproperty1
).
Full samples are available here.
5.7.0 has a few other assorted changes as well:
- Better parallelism if using runtime serializer generation
- Better error messaging when using the FlatSharp compiler (the C# errors are written to the output file)
- The testing infrastructure has been switched to xUnit.
Some types are now marked as obsolete. These are being considered for removal or are scheduled for removal in FlatSharp version 6.0.0, which is an upcoming release without a firm date or feature set.
- The
FlatSharp.Unsafe
package is scheduled for removal in version 6.0.0. - The
IFlatBufferAddressableStruct
interface is being considered for removal in version 6.0.0, because its primary purpose was enabling marshaling from reference -> value types, and this is now supported natively with value structs. If you want to keep usingIFlatBufferAddressableStruct
, even with the value structs feature, please raise an issue in this repository. - Shared String Readers (but not writers) are being considered for removal in version 6.0.0. Readers generally do not offer a performance benefit and are slower than simply re-reading the string.