From a77dd5f8549cff80863b79de281d68c1905edab4 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Mon, 24 Feb 2020 16:42:00 -0700 Subject: [PATCH] Fix Sequence to work for managed types T Fixes #167 --- src/Nerdbank.Streams.Tests/SequenceTests.cs | 10 ++++++++++ src/Nerdbank.Streams/Nerdbank.Streams.csproj | 1 + src/Nerdbank.Streams/Sequence`1.cs | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Nerdbank.Streams.Tests/SequenceTests.cs b/src/Nerdbank.Streams.Tests/SequenceTests.cs index aa87d365..acc4a19c 100644 --- a/src/Nerdbank.Streams.Tests/SequenceTests.cs +++ b/src/Nerdbank.Streams.Tests/SequenceTests.cs @@ -469,6 +469,16 @@ public void Dispose_ClearsAndAllowsReuse() Assert.Equal(3, seq.AsReadOnlySequence.Length); } + [Fact] + public void SequenceOfManagedType() + { + var seq = new Sequence(); + seq.Write(new object[] { new object(), new object() }); + Assert.Equal(2, seq.Length); + Assert.IsType(seq.AsReadOnlySequence.First.Span[0]); + Assert.IsType(seq.AsReadOnlySequence.First.Span[1]); + } + /// /// Adds a reference to an object in the sequence and returns a weak reference to it. /// diff --git a/src/Nerdbank.Streams/Nerdbank.Streams.csproj b/src/Nerdbank.Streams/Nerdbank.Streams.csproj index aa1854bf..a20e5065 100644 --- a/src/Nerdbank.Streams/Nerdbank.Streams.csproj +++ b/src/Nerdbank.Streams/Nerdbank.Streams.csproj @@ -21,6 +21,7 @@ + diff --git a/src/Nerdbank.Streams/Sequence`1.cs b/src/Nerdbank.Streams/Sequence`1.cs index 5f0fe8d4..4a5911c1 100644 --- a/src/Nerdbank.Streams/Sequence`1.cs +++ b/src/Nerdbank.Streams/Sequence`1.cs @@ -23,7 +23,7 @@ namespace Nerdbank.Streams [DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")] public class Sequence : IBufferWriter, IDisposable { - private static readonly int DefaultLengthFromArrayPool = 1 + (4095 / Marshal.SizeOf()); + private static readonly int DefaultLengthFromArrayPool = 1 + (4095 / Unsafe.SizeOf()); private readonly Stack segmentPool = new Stack();