Skip to content

Commit

Permalink
Merge pull request #168 from AArnott/fix167
Browse files Browse the repository at this point in the history
Fix Sequence<T> to work for managed types T
  • Loading branch information
AArnott authored Feb 24, 2020
2 parents 3f387f6 + a77dd5f commit d4dfdaa
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Nerdbank.Streams.Tests/SequenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,16 @@ public void Dispose_ClearsAndAllowsReuse()
Assert.Equal(3, seq.AsReadOnlySequence.Length);
}

[Fact]
public void SequenceOfManagedType()
{
var seq = new Sequence<object>();
seq.Write(new object[] { new object(), new object() });
Assert.Equal(2, seq.Length);
Assert.IsType<object>(seq.AsReadOnlySequence.First.Span[0]);
Assert.IsType<object>(seq.AsReadOnlySequence.First.Span[1]);
}

/// <summary>
/// Adds a reference to an object in the sequence and returns a weak reference to it.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Nerdbank.Streams/Nerdbank.Streams.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<PackageReference Include="System.Buffers" Version="4.5.0" />
<PackageReference Include="System.IO.Pipelines" Version="4.5.3" />
<PackageReference Include="System.Net.WebSockets" Version="4.3.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.6.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Nerdbank.Streams/Sequence`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Nerdbank.Streams
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
public class Sequence<T> : IBufferWriter<T>, IDisposable
{
private static readonly int DefaultLengthFromArrayPool = 1 + (4095 / Marshal.SizeOf<T>());
private static readonly int DefaultLengthFromArrayPool = 1 + (4095 / Unsafe.SizeOf<T>());

private static readonly ReadOnlySequence<T> Empty = new ReadOnlySequence<T>(SequenceSegment.Empty, 0, SequenceSegment.Empty, 0);

Expand Down

0 comments on commit d4dfdaa

Please sign in to comment.