feature: Weaver HashSet<T> support (we already had List<T> support) for cases where we a struct may need to serialize a HashSet member for serialization etc.

This commit is contained in:
Reuben Carolan 2022-12-09 19:44:25 +00:00 committed by vis2k
parent c36e1f564d
commit 7b4e174d23
3 changed files with 48 additions and 0 deletions

View File

@ -249,6 +249,19 @@ public static List<T> ReadList<T>(this NetworkReader reader)
return result;
}
public static HashSet<T> ReadHashSet<T>(this NetworkReader reader)
{
int length = reader.ReadInt();
if (length < 0)
return null;
HashSet<T> result = new HashSet<T>(length);
for (int i = 0; i < length; i++)
{
result.Add(reader.Read<T>());
}
return result;
}
public static T[] ReadArray<T>(this NetworkReader reader)
{
int length = reader.ReadInt();

View File

@ -293,6 +293,19 @@ public static void WriteArray<T>(this NetworkWriter writer, T[] array)
writer.Write(array[i]);
}
public static void WriteHashSet<T>(this NetworkWriter writer, HashSet<T> hashSet)
{
if (hashSet is null)
{
writer.WriteInt(-1);
return;
}
writer.WriteInt(hashSet.Count);
foreach (T item in hashSet)
writer.Write(item);
}
public static void WriteUri(this NetworkWriter writer, Uri uri)
{
writer.WriteString(uri?.ToString());

View File

@ -1317,6 +1317,28 @@ public void TestNullList()
Assert.That(readList, Is.Null);
}
public void TestHashSet()
{
HashSet<int> original = new HashSet<int>() { 1, 2, 3, 4, 5 };
NetworkWriter writer = new NetworkWriter();
writer.Write(original);
NetworkReader reader = new NetworkReader(writer.ToArray());
HashSet<int> readHashSet = reader.Read<HashSet<int>>();
Assert.That(readHashSet, Is.EqualTo(original));
}
[Test]
public void TestNullHashSet()
{
NetworkWriter writer = new NetworkWriter();
writer.Write<HashSet<int>>(null);
NetworkReader reader = new NetworkReader(writer.ToArray());
HashSet<int> readHashSet = reader.Read<HashSet<int>>();
Assert.That(readHashSet, Is.Null);
}
const int testArraySize = 4;
[Test]