using System.Collections.Generic; using System.Runtime.CompilerServices; using JetBrains.Core; using JetBrains.Serialization; using JetBrains.Util; namespace JetBrains.Rd.Impl { public static class SerializersEx { [MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)] public static List? ReadList(this UnsafeReader reader, CtxReadDelegate itemReader, SerializationCtx ctx) { int count = reader.ReadInt32(); if (count < 0) return null; var res = new List(count); for (var i=0; i < count; i++) res.Add(itemReader(ctx, reader)); return res; } [MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)] public static void WriteList(this UnsafeWriter writer, CtxWriteDelegate itemWriter, SerializationCtx ctx, List? value) { if (value == null) { writer.WriteInt32(-1); return; } writer.WriteInt32(value.Count); // ReSharper disable once ForCanBeConvertedToForeach for (var i = 0; i < value.Count; i++) { itemWriter(ctx, writer, value[i]); } } [MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)] public static void WriteEnumerable(this UnsafeWriter writer, CtxWriteDelegate itemWriter, SerializationCtx ctx, IEnumerable value) { if (value == null) { writer.WriteInt32(-1); return; } var cookie = new UnsafeWriter.Bookmark(writer); writer.WriteInt32(-1); // length int i = 0; foreach (var item in value) { ++i; itemWriter(ctx, writer, item); } cookie.WriteIntLength(i); } [MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)] public static T[]? ReadArray(this UnsafeReader reader, CtxReadDelegate itemReader, SerializationCtx ctx) { int count = reader.ReadInt32(); if (count < 0) return null; var res = new T[count]; for (var i=0; i < count; i++) res[i] = itemReader(ctx, reader); return res; } [MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)] public static void WriteArray(this UnsafeWriter writer, CtxWriteDelegate itemWriter, SerializationCtx ctx, T[]? value) { if (value == null) { writer.WriteInt32(-1); return; } writer.WriteInt32(value.Length); // ReSharper disable once ForCanBeConvertedToForeach for (var i = 0; i < value.Length; i++) { itemWriter(ctx, writer, value[i]); } } [MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)] public static T? ReadNullableClass(this UnsafeReader reader, CtxReadDelegate itemReader, SerializationCtx ctx) where T:class { return reader.ReadNullness() ? itemReader(ctx, reader) : null; } [MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)] public static T? ReadNullableStruct(this UnsafeReader reader, CtxReadDelegate itemReader, SerializationCtx ctx) where T:struct { return reader.ReadNullness() ? itemReader(ctx, reader).ToNullable() : null; } [MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)] public static void WriteNullableClass(this UnsafeWriter writer, CtxWriteDelegate itemWriter, SerializationCtx ctx, T? value) where T:class { if (writer.WriteNullness(value)) itemWriter(ctx, writer, value); } [MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)] public static void WriteNullableStruct(this UnsafeWriter writer, CtxWriteDelegate itemWriter, SerializationCtx ctx, T? value) where T:struct { if (writer.WriteNullness(value)) itemWriter(ctx, writer, value.Value); } [MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)] public static RdSecureString ReadSecureString(this UnsafeReader reader) { var str = reader.ReadString(); if (str == null) return default; return new RdSecureString(str); } [MethodImpl(MethodImplAdvancedOptions.AggressiveInlining)] public static void Write(this UnsafeWriter writer, RdSecureString @string) { writer.WriteString(@string.Contents); } public static RdId ReadRdId(this UnsafeReader reader) { return RdId.Read(reader); } public static void Write(this UnsafeWriter writer, RdId id) { id.Write(writer); } public static Unit ReadVoid(this UnsafeReader reader) { return Unit.Instance; } public static void Write(this UnsafeWriter writer, Unit value) {} // Composition functors //Readers public static CtxReadDelegate Array(this CtxReadDelegate inner) { return (ctx, reader) => reader.ReadArray(inner, ctx); } public static CtxReadDelegate?> List(this CtxReadDelegate inner) { return (ctx, reader) => reader.ReadList(inner, ctx); } public static CtxReadDelegate NullableClass(this CtxReadDelegate inner) where T : class { return (ctx, reader) => reader.ReadNullableClass(inner, ctx); } public static CtxReadDelegate NullableStruct(this CtxReadDelegate inner) where T : struct { return (ctx, reader) => reader.ReadNullableStruct(inner, ctx); } public static CtxReadDelegate Interned(this CtxReadDelegate inner, string internKey) { return (ctx, reader) => ctx.ReadInterned(reader, internKey, inner); } //Writers public static CtxWriteDelegate Array(this CtxWriteDelegate inner) { return (ctx, reader, value) => reader.WriteArray(inner, ctx, value); } public static CtxWriteDelegate> List(this CtxWriteDelegate inner) { return (ctx, reader, value) => reader.WriteList(inner, ctx, value); } public static CtxWriteDelegate NullableClass(this CtxWriteDelegate inner) where T:class { return (ctx, reader, value) => reader.WriteNullableClass(inner, ctx, value); } public static CtxWriteDelegate NullableStruct(this CtxWriteDelegate inner) where T : struct { return (ctx, reader, value) => reader.WriteNullableStruct(inner, ctx, value); } public static CtxWriteDelegate Interned(this CtxWriteDelegate inner, string internKey) { return (ctx, reader, value) => ctx.WriteInterned(reader, value, internKey, inner); } } }