Permalink
Browse files

Use ValueTuple instead of Tuple

  • Loading branch information...
roman-kozachenko committed Mar 17, 2017
1 parent 63af35e commit c80aaa53eb518398dc0499b3044104dd11bae9aa
Showing with 19 additions and 8 deletions.
  1. +18 −8 src/msgpack.light/MsgPackMemoryStreamReader.cs
  2. +1 −0 src/msgpack.light/msgpack.light.csproj
@@ -10,7 +10,9 @@ internal class MsgPackMemoryStreamReader : BaseMsgPackReader, IDisposable
private readonly bool _disposeStream;
- private List<Tuple<byte, byte[]>> _bytesGatheringBuffer;
+ private readonly List<(byte, byte[])> _bytesGatheringBuffer = new List<(byte, byte[])>();
+
+ private bool _bytesGatheringInProgress;
public MsgPackMemoryStreamReader(MemoryStream stream, bool disposeStream = true)
{
@@ -26,25 +28,32 @@ public override byte ReadByte()
throw ExceptionUtils.NotEnoughBytes(0, 1);
}
- var result = (byte)temp;
- _bytesGatheringBuffer?.Add(Tuple.Create(result, (byte[])null));
+ var result = (byte) temp;
+ if (_bytesGatheringInProgress)
+ {
+ _bytesGatheringBuffer.Add((result, (byte[]) null));
+ }
return result;
}
public override ArraySegment<byte> ReadBytes(uint length)
{
var buffer = ReadBytesInternal(length);
- _bytesGatheringBuffer?.Add(Tuple.Create((byte)0, buffer));
+ if (_bytesGatheringInProgress)
+ {
+ _bytesGatheringBuffer.Add((0, buffer));
+ }
+
return new ArraySegment<byte>(buffer, 0, buffer.Length);
}
public override void Seek(long offset, SeekOrigin origin)
{
- if (_bytesGatheringBuffer != null)
+ if (_bytesGatheringInProgress)
{
var buffer = ReadBytesInternal((uint)offset);
- _bytesGatheringBuffer.Add(Tuple.Create((byte)0, buffer));
+ _bytesGatheringBuffer.Add((0, buffer));
}
else
{
@@ -73,13 +82,14 @@ protected override IList<byte> StopTokenGathering()
}
}
- _bytesGatheringBuffer = null;
+ _bytesGatheringInProgress = false;
return result;
}
protected override void StartTokenGathering()
{
- _bytesGatheringBuffer = new List<Tuple<byte, byte[]>>();
+ _bytesGatheringInProgress = true;
+ _bytesGatheringBuffer.Clear();
}
private byte[] ReadBytesInternal(uint length)
@@ -18,5 +18,6 @@
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="10.3.0" />
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.3.0" />
+ <PackageReference Include="System.ValueTuple" Version="4.3.0" />
</ItemGroup>
</Project>

0 comments on commit c80aaa5

Please sign in to comment.