Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Nullable annotation for System.IO.Pipelines #41407

Merged
merged 6 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/System.IO.Pipelines/ref/System.IO.Pipelines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Reset() { }
}
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
public partial class PipeOptions
{
public PipeOptions(System.Buffers.MemoryPool<byte> pool = null, System.IO.Pipelines.PipeScheduler readerScheduler = null, System.IO.Pipelines.PipeScheduler writerScheduler = null, long pauseWriterThreshold = (long)-1, long resumeWriterThreshold = (long)-1, int minimumSegmentSize = -1, bool useSynchronizationContext = true) { }
public PipeOptions(System.Buffers.MemoryPool<byte>? pool = null, System.IO.Pipelines.PipeScheduler? readerScheduler = null, System.IO.Pipelines.PipeScheduler? writerScheduler = null, long pauseWriterThreshold = (long)-1, long resumeWriterThreshold = (long)-1, int minimumSegmentSize = -1, bool useSynchronizationContext = true) { }
public static System.IO.Pipelines.PipeOptions Default { get { throw null; } }
public int MinimumSegmentSize { get { throw null; } }
public long PauseWriterThreshold { get { throw null; } }
Expand All @@ -46,13 +46,13 @@ protected PipeReader() { }
public abstract void AdvanceTo(System.SequencePosition consumed, System.SequencePosition examined);
public virtual System.IO.Stream AsStream(bool leaveOpen = false) { throw null; }
public abstract void CancelPendingRead();
public abstract void Complete(System.Exception exception = null);
public virtual System.Threading.Tasks.ValueTask CompleteAsync(System.Exception exception = null) { throw null; }
public abstract void Complete(System.Exception? exception = null);
public virtual System.Threading.Tasks.ValueTask CompleteAsync(System.Exception? exception = null) { throw null; }
public virtual System.Threading.Tasks.Task CopyToAsync(System.IO.Pipelines.PipeWriter destination, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task CopyToAsync(System.IO.Stream destination, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.IO.Pipelines.PipeReader Create(System.IO.Stream stream, System.IO.Pipelines.StreamPipeReaderOptions readerOptions = null) { throw null; }
public static System.IO.Pipelines.PipeReader Create(System.IO.Stream stream, System.IO.Pipelines.StreamPipeReaderOptions? readerOptions = null) { throw null; }
[System.ObsoleteAttribute("OnWriterCompleted may not be invoked on all implementations of PipeReader. This will be removed in a future release.")]
public virtual void OnWriterCompleted(System.Action<System.Exception, object> callback, object state) { }
public virtual void OnWriterCompleted(System.Action<System.Exception?, object> callback, object state) { }
public abstract System.Threading.Tasks.ValueTask<System.IO.Pipelines.ReadResult> ReadAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
public abstract bool TryRead(out System.IO.Pipelines.ReadResult result);
}
Expand All @@ -61,23 +61,23 @@ public abstract partial class PipeScheduler
protected PipeScheduler() { }
public static System.IO.Pipelines.PipeScheduler Inline { get { throw null; } }
public static System.IO.Pipelines.PipeScheduler ThreadPool { get { throw null; } }
public abstract void Schedule(System.Action<object> action, object state);
public abstract void Schedule(System.Action<object?> action, object? state);
}
public abstract partial class PipeWriter : System.Buffers.IBufferWriter<byte>
{
protected PipeWriter() { }
public abstract void Advance(int bytes);
public virtual System.IO.Stream AsStream(bool leaveOpen = false) { throw null; }
public abstract void CancelPendingFlush();
public abstract void Complete(System.Exception exception = null);
public virtual System.Threading.Tasks.ValueTask CompleteAsync(System.Exception exception = null) { throw null; }
public abstract void Complete(System.Exception? exception = null);
public virtual System.Threading.Tasks.ValueTask CompleteAsync(System.Exception? exception = null) { throw null; }
protected internal virtual System.Threading.Tasks.Task CopyFromAsync(System.IO.Stream source, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.IO.Pipelines.PipeWriter Create(System.IO.Stream stream, System.IO.Pipelines.StreamPipeWriterOptions writerOptions = null) { throw null; }
public static System.IO.Pipelines.PipeWriter Create(System.IO.Stream stream, System.IO.Pipelines.StreamPipeWriterOptions? writerOptions = null) { throw null; }
public abstract System.Threading.Tasks.ValueTask<System.IO.Pipelines.FlushResult> FlushAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
public abstract System.Memory<byte> GetMemory(int sizeHint = 0);
public abstract System.Span<byte> GetSpan(int sizeHint = 0);
[System.ObsoleteAttribute("OnReaderCompleted may not be invoked on all implementations of PipeWriter. This will be removed in a future release.")]
public virtual void OnReaderCompleted(System.Action<System.Exception, object> callback, object state) { }
public virtual void OnReaderCompleted(System.Action<System.Exception?, object> callback, object state) { }
public virtual System.Threading.Tasks.ValueTask<System.IO.Pipelines.FlushResult> WriteAsync(System.ReadOnlyMemory<byte> source, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
public readonly partial struct ReadResult
Expand All @@ -95,15 +95,15 @@ public static partial class StreamPipeExtensions
}
public partial class StreamPipeReaderOptions
{
public StreamPipeReaderOptions(System.Buffers.MemoryPool<byte> pool = null, int bufferSize = -1, int minimumReadSize = -1, bool leaveOpen = false) { }
public StreamPipeReaderOptions(System.Buffers.MemoryPool<byte>? pool = null, int bufferSize = -1, int minimumReadSize = -1, bool leaveOpen = false) { }
public int BufferSize { get { throw null; } }
public bool LeaveOpen { get { throw null; } }
public int MinimumReadSize { get { throw null; } }
public System.Buffers.MemoryPool<byte> Pool { get { throw null; } }
}
public partial class StreamPipeWriterOptions
{
public StreamPipeWriterOptions(System.Buffers.MemoryPool<byte> pool = null, int minimumBufferSize = -1, bool leaveOpen = false) { }
public StreamPipeWriterOptions(System.Buffers.MemoryPool<byte>? pool = null, int minimumBufferSize = -1, bool leaveOpen = false) { }
public bool LeaveOpen { get { throw null; } }
public int MinimumBufferSize { get { throw null; } }
public System.Buffers.MemoryPool<byte> Pool { get { throw null; } }
Expand Down
1 change: 1 addition & 0 deletions src/System.IO.Pipelines/ref/System.IO.Pipelines.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configurations>netstandard-Debug;netstandard-Release</Configurations>
<Nullable>enable</Nullable>
<!-- We only plan to use this ref in netcoreapp. For all other netstandard compatible frameworks we should use the lib
asset instead. -->
<PackageTargetFramework Condition="'$(TargetGroup)' == 'netstandard'">netcoreapp2.0</PackageTargetFramework>
Expand Down
3 changes: 3 additions & 0 deletions src/System.IO.Pipelines/src/System.IO.Pipelines.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configurations>netcoreapp-Debug;netcoreapp-Release;netcoreapp3.0-Debug;netcoreapp3.0-Release;netstandard-Debug;netstandard-Release</Configurations>
<DefineConstants>$(DefineConstants);INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonPath)\CoreLib\System\Threading\Tasks\TaskToApm.cs">
Expand Down Expand Up @@ -43,6 +45,7 @@
<Compile Include="System\IO\Pipelines\StreamExtensions.netstandard.cs" />
<Compile Include="System\IO\Pipelines\ThreadPoolScheduler.netstandard.cs" />
<Compile Include="System\IO\Pipelines\CancellationTokenExtensions.netstandard.cs" />
<Compile Include="$(CommonPath)\CoreLib\System\Diagnostics\CodeAnalysis\NullableAttributes.cs" Link="System\Diagnostics\CodeAnalysis\NullableAttributes.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Buffers" />
Expand Down
11 changes: 6 additions & 5 deletions src/System.IO.Pipelines/src/System/IO/Pipelines/BufferSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace System.IO.Pipelines
{
internal sealed class BufferSegment : ReadOnlySequenceSegment<byte>
{
private object _memoryOwner;
private BufferSegment _next;
private object? _memoryOwner;
private BufferSegment? _next;
private int _end;

/// <summary>
Expand All @@ -37,7 +37,7 @@ public int End
/// working memory. The "active" memory is grown when bytes are copied in, End is increased, and Next is assigned. The "active"
/// memory is shrunk when bytes are consumed, Start is increased, and blocks are returned to the pool.
/// </summary>
public BufferSegment NextSegment
public BufferSegment? NextSegment
{
get => _next;
set
Expand Down Expand Up @@ -67,6 +67,7 @@ public void ResetMemory()
}
else
{
Debug.Assert(_memoryOwner != null);
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
byte[] poolArray = (byte[])_memoryOwner;
ArrayPool<byte>.Shared.Return(poolArray);
}
Expand All @@ -83,7 +84,7 @@ public void ResetMemory()
}

// Exposed for testing
internal object MemoryOwner => _memoryOwner;
internal object? MemoryOwner => _memoryOwner;

public Memory<byte> AvailableMemory { get; private set; }

Expand All @@ -106,7 +107,7 @@ public void SetNext(BufferSegment segment)

while (segment.Next != null)
{
segment.NextSegment.RunningIndex = segment.RunningIndex + segment.Length;
segment.NextSegment!.RunningIndex = segment.RunningIndex + segment.Length;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidfowl, is it correct that this is accessing NextSegment but the while above is using Next? It's not clear to me what the difference is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems at least better assert it instead bang.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually confused me as well 😄 . I think they are the same. I'm not sure why we're using Next for the null check.

Copy link
Member Author

@buyaa-n buyaa-n Oct 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidfowl do you want to change segment.Next null check with segment.NextSegment with this PR or its fine as is?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to leave it as is and merge this PR, we can revisit later if needed

segment = segment.NextSegment;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Diagnostics.CodeAnalysis;

namespace System.IO.Pipelines
{
Expand All @@ -22,7 +23,7 @@ public BufferSegmentStack(int size)

public int Count => _size;

public bool TryPop(out BufferSegment result)
public bool TryPop([NotNullWhen(true)] out BufferSegment? result)
{
int size = _size - 1;
SegmentAsValueType[] array = _array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace System.IO.Pipelines
{
internal readonly struct CompletionData
{
public Action<object> Completion { get; }
public object CompletionState { get; }
public ExecutionContext ExecutionContext { get; }
public SynchronizationContext SynchronizationContext { get; }
public Action<object?> Completion { get; }
public object? CompletionState { get; }
public ExecutionContext? ExecutionContext { get; }
public SynchronizationContext? SynchronizationContext { get; }

public CompletionData(Action<object> completion, object completionState, ExecutionContext executionContext, SynchronizationContext synchronizationContext)
public CompletionData(Action<object?> completion, object? completionState, ExecutionContext? executionContext, SynchronizationContext? synchronizationContext)
{
Completion = completion;
CompletionState = completionState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace System.IO.Pipelines
{
internal sealed class InlineScheduler : PipeScheduler
{
public override void Schedule(Action<object> action, object state)
public override void Schedule(Action<object?> action, object? state)
{
action(state);
}

internal override void UnsafeSchedule(Action<object> action, object state)
internal override void UnsafeSchedule(Action<object?> action, object? state)
{
action(state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public DefaultPipeReader(Pipe pipe)

public override void CancelPendingRead() => _pipe.CancelPendingRead();

public override void Complete(Exception exception = null) => _pipe.CompleteReader(exception);
public override void Complete(Exception? exception = null) => _pipe.CompleteReader(exception);

#pragma warning disable CS0672 // Member overrides obsolete member
public override void OnWriterCompleted(Action<Exception, object> callback, object state) => _pipe.OnWriterCompleted(callback, state);
public override void OnWriterCompleted(Action<Exception?, object?> callback, object? state) => _pipe.OnWriterCompleted(callback, state);
#pragma warning restore CS0672 // Member overrides obsolete member

public ValueTaskSourceStatus GetStatus(short token) => _pipe.GetReadAsyncStatus();

public ReadResult GetResult(short token) => _pipe.GetReadAsyncResult();

public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) => _pipe.OnReadAsyncCompleted(continuation, state, flags);
public void OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) => _pipe.OnReadAsyncCompleted(continuation, state, flags);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public DefaultPipeWriter(Pipe pipe)
_pipe = pipe;
}

public override void Complete(Exception exception = null) => _pipe.CompleteWriter(exception);
public override void Complete(Exception? exception = null) => _pipe.CompleteWriter(exception);

public override void CancelPendingFlush() => _pipe.CancelPendingFlush();

#pragma warning disable CS0672 // Member overrides obsolete member
public override void OnReaderCompleted(Action<Exception, object> callback, object state) => _pipe.OnReaderCompleted(callback, state);
public override void OnReaderCompleted(Action<Exception?, object?> callback, object? state) => _pipe.OnReaderCompleted(callback, state);
#pragma warning restore CS0672 // Member overrides obsolete member

public override ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken = default) => _pipe.FlushAsync(cancellationToken);
Expand All @@ -42,7 +42,7 @@ public DefaultPipeWriter(Pipe pipe)

public FlushResult GetResult(short token) => _pipe.GetFlushAsyncResult();

public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) => _pipe.OnFlushAsyncCompleted(continuation, state, flags);
public void OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags) => _pipe.OnFlushAsyncCompleted(continuation, state, flags);

public override ValueTask<FlushResult> WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default)
{
Expand Down
Loading