Skip to content

Commit 345190c

Browse files
committed
UnsafeWriter: migrate whole RD to explicit type APIs
1 parent 0f4742d commit 345190c

37 files changed

+405
-380
lines changed

rd-net/Lifetimes/Serialization/UnsafeWriter.cs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ namespace JetBrains.Serialization
1818
/// It is <see cref="IDisposable"/> so must be used only with (possibly nested) <c>using</c> in stack-like way.
1919
/// <see cref="Cookie"/> contains start position and length of currently serialized data (start + len = position), so when disposed it reverts writer
2020
/// position to the cookie's start position.
21-
///
22-
///
2321
/// <seealso cref="UnsafeReader"/>
2422
/// </summary>
2523
[PublicAPI]
@@ -116,7 +114,7 @@ public byte* Data
116114

117115
/// <summary>
118116
/// Writes `<see cref="Count"/><c> - sizeof(int)</c>` into the <see cref="Data"/> pointer.
119-
/// Cookie must be prepared by invoking `<see cref="UnsafeWriter.Write(int)"/><c>(0)</c>` as first cookie call.
117+
/// Cookie must be prepared by invoking `<see cref="UnsafeWriter.WriteInt32(int)"/><c>(0)</c>` as first cookie call.
120118
/// </summary>
121119
public void WriteIntLength()
122120
{
@@ -640,36 +638,38 @@ public void WriteDateTime(DateTime value)
640638
{
641639
if (Mode.IsAssertion) Assertion.Assert(value.Kind != DateTimeKind.Local, "Use UTC time");
642640

643-
Write(value.Ticks);
641+
WriteInt64(value.Ticks);
644642
}
645643

646644
[MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)]
647645
public void WriteTimeSpan(TimeSpan value)
648646
{
649-
Write(value.Ticks);
647+
WriteInt64(value.Ticks);
650648
}
651649

652650
[MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)]
653651
public void WriteUri(Uri value)
654652
{
655-
Write(Uri.EscapeUriString(value.OriginalString));
653+
WriteString(Uri.EscapeUriString(value.OriginalString));
656654
}
657655

658656
[MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)]
659657
public void WriteString(string? value)
660658
{
661-
if (value == null) Write(-1);
659+
if (value == null)
660+
{
661+
WriteInt32(-1);
662+
}
662663
else
663664
{
664-
Write(value.Length);
665+
WriteInt32(value.Length);
665666
WriteStringContentInternal(this, value, 0, value.Length);
666667
}
667668
}
668669

669670
/// <summary>
670-
/// Doesn't write length prefix, only string contents. If value == null, does nothing.
671+
/// Doesn't write length prefix, only string contents. If <paramref name="value"/> is <c>value</c>, does nothing.
671672
/// </summary>
672-
/// <param name="value"></param>
673673
[MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)]
674674
public void WriteStringContent(string? value)
675675
{
@@ -678,11 +678,8 @@ public void WriteStringContent(string? value)
678678
}
679679

680680
/// <summary>
681-
/// Doesn't write length prefix, only string contents. If value == null, does nothing.
681+
/// Doesn't write length prefix, only string contents. If <paramref name="value"/> is <c>value</c>, does nothing.
682682
/// </summary>
683-
/// <param name="value"></param>
684-
/// <param name="offset"></param>
685-
/// <param name="count"></param>
686683
[MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)]
687684
public void WriteStringContent(string? value, int offset, int count)
688685
{
@@ -787,11 +784,11 @@ public void WriteArray(int[]? value)
787784
{
788785
if (value == null)
789786
{
790-
Write(-1);
787+
WriteInt32(-1);
791788
}
792789
else
793790
{
794-
Write(value.Length);
791+
WriteInt32(value.Length);
795792
fixed (int* c = value)
796793
{
797794
Write((byte*)c, value.Length * sizeof(int));
@@ -803,12 +800,12 @@ public void WriteByteArray(byte[]? value)
803800
{
804801
if (value == null)
805802
{
806-
Write(-1);
803+
WriteInt32(-1);
807804
}
808805
else
809806
{
810807
var size = value.Length;
811-
Write(size);
808+
WriteInt32(size);
812809
Prepare(size);
813810
Marshal.Copy(value, 0, (IntPtr)myPtr, size); // Unlike MemoryUtil::CopyMemory, this is a CLR intrinsic call
814811
myPtr += size;
@@ -879,28 +876,30 @@ public void WriteCollection<T, TCollection>(WriteDelegate<T> writeDelegate, TCol
879876
{
880877
if (value == null)
881878
{
882-
Write(-1);
879+
WriteInt32(-1);
883880
}
884881
else
885882
{
886-
Write(value.Count);
883+
WriteInt32(value.Count);
887884
foreach (var x in value)
888885
{
889886
writeDelegate(this, x);
890887
}
891888
}
892889
}
893890

894-
public void Write<TK, TV, TDictionary>(WriteDelegate<TK> writeKeyDelegate, WriteDelegate<TV> writeValueDelegate, TDictionary? value)
891+
public void Write<TK, TV, TDictionary>(
892+
WriteDelegate<TK> writeKeyDelegate, WriteDelegate<TV> writeValueDelegate, TDictionary? value)
895893
where TDictionary : IDictionary<TK, TV>
896894
{
897895
if (value == null)
898896
{
899-
Write(-1);
897+
WriteInt32(-1);
900898
}
901899
else
902900
{
903-
Write(value.Count);
901+
WriteInt32(value.Count);
902+
904903
foreach (var kv in value)
905904
{
906905
writeKeyDelegate(this, kv.Key);
@@ -915,15 +914,15 @@ public void Write<TK, TV, TDictionary>(WriteDelegate<TK> writeKeyDelegate, Write
915914
public bool WriteNullness<T>([NotNullWhen(true)] T? value) where T : struct
916915
{
917916
var res = value != null;
918-
Write(res);
917+
WriteBoolean(res);
919918
return res;
920919
}
921920

922921
[ContractAnnotation("null=>false")]
923922
public bool WriteNullness<T>([NotNullWhen(true)] T? value) where T : class
924923
{
925924
var res = value != null;
926-
Write(res);
925+
WriteBoolean(res);
927926
return res;
928927
}
929928
}

rd-net/RdFramework.Reflection/CollectionSerializers.cs

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,83 +12,104 @@ internal class CollectionSerializers
1212
{
1313
public static SerializerPair CreateListSerializerPair<T>(SerializerPair itemSerializer)
1414
{
15-
CtxReadDelegate<List<T>?> readListSerializer = (ctx, reader) => reader.ReadList(itemSerializer.GetReader<T>(), ctx);
16-
CtxWriteDelegate<IEnumerable<T>> writeListSerializer =(ctx, writer, value) => writer.WriteEnumerable(itemSerializer.GetWriter<T>(), ctx, value);
15+
CtxReadDelegate<List<T>?> readListSerializer =
16+
(ctx, reader) => reader.ReadList(itemSerializer.GetReader<T>(), ctx);
17+
18+
CtxWriteDelegate<IEnumerable<T>> writeListSerializer =
19+
(ctx, writer, value) => writer.WriteEnumerable(itemSerializer.GetWriter<T>(), ctx, value);
20+
1721
return new SerializerPair(readListSerializer, writeListSerializer);
1822
}
1923

20-
public static SerializerPair CreateDictionarySerializerPair<TKey, TValue>(SerializerPair keySerializer, SerializerPair valueSerializer)
24+
public static SerializerPair CreateDictionarySerializerPair<TKey, TValue>(
25+
SerializerPair keySerializer, SerializerPair valueSerializer)
2126
{
2227
var read = CreateReadDictionary<TKey, TValue>(keySerializer, valueSerializer);
28+
2329
CtxWriteDelegate<IDictionary<TKey, TValue>?> write = (ctx, writer, value) =>
2430
{
2531
if (value is Dictionary<TKey, TValue> val && !Equals(val.Comparer, EqualityComparer<TKey>.Default))
2632
throw new Exception($"Unable to serialize {value.GetType().ToString(true)}. Custom equality comparers are not supported");
33+
2734
if (value == null)
2835
{
29-
writer.Write(-1);
36+
writer.WriteInt32(-1);
3037
return;
3138
}
32-
writer.Write(value.Count);
33-
var keyw = keySerializer.GetWriter<TKey>();
34-
var valuew = valueSerializer.GetWriter<TValue>();
39+
40+
writer.WriteInt32(value.Count);
41+
42+
var keyWriter = keySerializer.GetWriter<TKey>();
43+
var valueWriter = valueSerializer.GetWriter<TValue>();
44+
3545
foreach (var kvp in value)
3646
{
37-
keyw(ctx, writer, kvp.Key);
38-
valuew(ctx, writer, kvp.Value);
47+
keyWriter(ctx, writer, kvp.Key);
48+
valueWriter(ctx, writer, kvp.Value);
3949
}
4050
};
51+
4152
return new SerializerPair(read, write);
4253
}
4354

44-
public static SerializerPair CreateReadOnlyDictionarySerializerPair<TKey, TValue>(SerializerPair keySerializer, SerializerPair valueSerializer)
55+
public static SerializerPair CreateReadOnlyDictionarySerializerPair<TKey, TValue>(
56+
SerializerPair keySerializer, SerializerPair valueSerializer)
4557
{
4658
#if NET35
4759
throw new NotSupportedException();
4860
#else
4961
var read = CreateReadDictionary<TKey, TValue>(keySerializer, valueSerializer);
50-
CtxWriteDelegate<IReadOnlyDictionary<TKey, TValue>?> write = (ctx, writer, value) =>
62+
63+
CtxWriteDelegate<IReadOnlyDictionary<TKey, TValue>?> write = (context, writer, value) =>
5164
{
5265
if (value is Dictionary<TKey, TValue> val && !Equals(val.Comparer, EqualityComparer<TKey>.Default))
5366
throw new Exception($"Unable to serialize {value.GetType().ToString(true)}. Custom equality comparers are not supported");
67+
5468
if (value == null)
5569
{
56-
writer.Write(-1);
70+
writer.WriteInt32(-1);
5771
return;
5872
}
59-
writer.Write(value.Count);
60-
var keyw = keySerializer.GetWriter<TKey>();
61-
var valuew = valueSerializer.GetWriter<TValue>();
73+
74+
writer.WriteInt32(value.Count);
75+
76+
var keyWriter = keySerializer.GetWriter<TKey>();
77+
var valueWriter = valueSerializer.GetWriter<TValue>();
78+
6279
foreach (var kvp in value)
6380
{
64-
keyw(ctx, writer, kvp.Key);
65-
valuew(ctx, writer, kvp.Value);
81+
keyWriter(context, writer, kvp.Key);
82+
valueWriter(context, writer, kvp.Value);
6683
}
6784
};
85+
6886
return new SerializerPair(read, write);
6987
#endif
7088
}
7189

72-
73-
private static CtxReadDelegate<Dictionary<TKey, TValue>?> CreateReadDictionary<TKey, TValue>(SerializerPair keySerializer, SerializerPair valueSerializer)
90+
private static CtxReadDelegate<Dictionary<TKey, TValue>?> CreateReadDictionary<TKey, TValue>(
91+
SerializerPair keySerializer, SerializerPair valueSerializer)
7492
{
75-
CtxReadDelegate<Dictionary<TKey, TValue>?> read = (ctx, reader) =>
93+
CtxReadDelegate<Dictionary<TKey, TValue>?> read = (context, reader) =>
7694
{
7795
int count = reader.ReadInt();
7896
if (count == -1)
7997
return null;
98+
8099
var result = new Dictionary<TKey, TValue>(count);
81-
var keyr = keySerializer.GetReader<TKey>();
82-
var valuer = valueSerializer.GetReader<TValue>();
83-
for (int i = 0; i < count; i++)
100+
var keyReader = keySerializer.GetReader<TKey>();
101+
var valueReader = valueSerializer.GetReader<TValue>();
102+
103+
for (var index = 0; index < count; index++)
84104
{
85-
var key = keyr(ctx, reader);
86-
var value = valuer(ctx, reader);
105+
var key = keyReader(context, reader);
106+
var value = valueReader(context, reader);
87107
result.Add(key, value);
88108
}
89109

90110
return result;
91111
};
112+
92113
return read;
93114
}
94115
}

rd-net/RdFramework.Reflection/ReflectionSerializers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ private void RegisterModelSerializer<T>()
253253
{
254254
if (allowNullable)
255255
{
256-
unsafeWriter.Write(value != null);
256+
unsafeWriter.WriteBoolean(value != null);
257257
if (value == null)
258258
return;
259259
}

rd-net/RdFramework.Reflection/ScalarSerializer.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Concurrent;
3-
using System.Collections.Generic;
42
using System.Linq;
53
using System.Linq.Expressions;
64
using System.Reflection;
@@ -154,7 +152,7 @@ private SerializerPair CreateCustomScalar<T>(ISerializersSource serializers)
154152

155153
if (allowNullable)
156154
{
157-
unsafeWriter.Write(value != null);
155+
unsafeWriter.WriteBoolean(value != null);
158156
if (value == null)
159157
return;
160158
}
@@ -207,9 +205,10 @@ private SerializerPair CreateEnumSerializer<T>()
207205
var writerCaster = Expression.Lambda<Func<T, int>>(writerConvert, writerParameter).Compile();
208206

209207
if (Mode.IsAssertion) Assertion.Require(typeof(T).IsSubclassOf(typeof(Enum)), "{0}", typeof(T));
208+
210209
var result = new SerializerPair(
211-
(CtxReadDelegate<T>) ((ctx, reader) => readerCaster(reader.ReadInt())),
212-
(CtxWriteDelegate<T>) ((ctx, w, o) => w.Write(writerCaster(o))));
210+
(CtxReadDelegate<T>) ((_, reader) => readerCaster(reader.ReadInt())),
211+
(CtxWriteDelegate<T>) ((_, writer, o) => writer.WriteInt32(writerCaster(o))));
213212

214213
return result;
215214
}

0 commit comments

Comments
 (0)