Slide 32
Slide 32 text
public override void Deserialize(ref MemoryPackReader reader, scoped ref List? value)
{
if (!reader.TryReadCollectionHeader(out var length))
{
value = null;
return;
}
value = new List(length);
var span = CollectionsMarshalEx.CreateSpan(value, length);
var formatter = GetFormatter();
for (int i = 0; i < length; i++)
{
formatter.Deserialize(ref this, ref span[i]);
}
}
internal static class CollectionsMarshalEx
{
public static Span CreateSpan(
List list, int length)
{
list.EnsureCapacity(length);
ref var view = ref Unsafe.As,
ListView>(ref list);
view._size = length;
return view._items.AsSpan(0, length);
}
}
internal sealed class ListView
{
public T[] _items;
public int _size;
public int _version;
}