mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-25 07:38:40 +00:00
C# support for directly reading and writting to memory other than byte[]. For example, ByteBuffer can be initialized with a custom allocator which uses shared memory / memory mapped files. (#4886)
Public access to the backing buffer uses Span<T> instead of ArraySegment<T>. Writing to the buffer now supports Span<T> in addition to T[]. To maintain backwards compatibility ENABLE_SPAN_T must be defined.
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
e1f48ad35a
commit
d0321df8cf
@@ -110,14 +110,19 @@ namespace FlatBuffers.Test
|
||||
Monster.FinishMonsterBuffer(fbb, mon);
|
||||
}
|
||||
|
||||
|
||||
// Dump to output directory so we can inspect later, if needed
|
||||
#if ENABLE_SPAN_T
|
||||
var data = fbb.DataBuffer.ToSizedArray();
|
||||
string filename = @"Resources/monsterdata_cstest" + (sizePrefix ? "_sp" : "") + ".mon";
|
||||
File.WriteAllBytes(filename, data);
|
||||
#else
|
||||
using (var ms = fbb.DataBuffer.ToMemoryStream(fbb.DataBuffer.Position, fbb.Offset))
|
||||
{
|
||||
var data = ms.ToArray();
|
||||
string filename = @"Resources/monsterdata_cstest" + (sizePrefix ? "_sp" : "") + ".mon";
|
||||
File.WriteAllBytes(filename, data);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Remove the size prefix if necessary for further testing
|
||||
ByteBuffer dataBuffer = fbb.DataBuffer;
|
||||
@@ -243,6 +248,19 @@ namespace FlatBuffers.Test
|
||||
|
||||
Assert.AreEqual(false, monster.Testbool);
|
||||
|
||||
#if ENABLE_SPAN_T
|
||||
var nameBytes = monster.GetNameBytes();
|
||||
Assert.AreEqual("MyMonster", Encoding.UTF8.GetString(nameBytes.ToArray(), 0, nameBytes.Length));
|
||||
|
||||
if (0 == monster.TestarrayofboolsLength)
|
||||
{
|
||||
Assert.IsFalse(monster.GetTestarrayofboolsBytes().Length != 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.IsTrue(monster.GetTestarrayofboolsBytes().Length == 0);
|
||||
}
|
||||
#else
|
||||
var nameBytes = monster.GetNameBytes().Value;
|
||||
Assert.AreEqual("MyMonster", Encoding.UTF8.GetString(nameBytes.Array, nameBytes.Offset, nameBytes.Count));
|
||||
|
||||
@@ -254,6 +272,7 @@ namespace FlatBuffers.Test
|
||||
{
|
||||
Assert.IsTrue(monster.GetTestarrayofboolsBytes().HasValue);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
[FlatBuffersTestMethod]
|
||||
|
||||
Reference in New Issue
Block a user