Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return ValueTask instead of Task for read/write APIs. #2953

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/Microsoft.OData.Client/Microsoft.OData.Client.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0</TargetFrameworks>
<AssemblyName>Microsoft.OData.Client</AssemblyName>
<RootNamespace>Microsoft.OData.Client</RootNamespace>
<DocumentationFile>$(AssemblyName).xml</DocumentationFile>
Expand Down
16 changes: 8 additions & 8 deletions src/Microsoft.OData.Core/Json/BufferingJsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public bool Read()
/// <returns>A task that represents the asynchronous read operation.
/// The value of the TResult parameter contains the node value of the last buffered read if buffering is on or off;
/// otherwise the node value of the last unbuffered read.</returns>
public Task<object> GetValueAsync()
public ValueTask<object> GetValueAsync()
{
this.AssertAsynchronous();

Expand All @@ -257,11 +257,11 @@ public Task<object> GetValueAsync()
if (this.isBuffering)
{
Debug.Assert(this.currentBufferedNode != null, "this.currentBufferedNode != null");
return Task.FromResult(this.currentBufferedNode.Value);
return ValueTask.FromResult(this.currentBufferedNode.Value);
}

// In non-buffering mode if we have buffered nodes satisfy the request from the first node there
return Task.FromResult(this.bufferedNodesHead.Value);
return ValueTask.FromResult(this.bufferedNodesHead.Value);
}

return this.innerReader.GetValueAsync();
Expand All @@ -272,7 +272,7 @@ public Task<object> GetValueAsync()
/// </summary>
/// <returns>A task that represents the asynchronous read operation.
/// The value of the TResult parameter contains true if a new node was found, or false if end of input was reached.</returns>
public Task<bool> ReadAsync()
public ValueTask<bool> ReadAsync()
{
Debug.Assert(!this.parsingInStreamError, "!this.parsingInStreamError");
this.AssertAsynchronous();
Expand All @@ -286,7 +286,7 @@ public Task<bool> ReadAsync()
/// </summary>
/// <returns>A task that represents the asynchronous operation.
/// true if the current value can be streamed; otherwise false.</returns>
public virtual async Task<bool> CanStreamAsync()
public virtual async ValueTask<bool> CanStreamAsync()
{
this.AssertAsynchronous();

Expand All @@ -307,7 +307,7 @@ public virtual async Task<bool> CanStreamAsync()
/// <returns>A task that represents the asynchronous read operation.
/// The value of the TResult parameter contains a <see cref="Stream"/> used to read a stream value.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1825:Avoid zero-length array allocations.", Justification = "<Pending>")]
public virtual async Task<Stream> CreateReadStreamAsync()
public virtual async ValueTask<Stream> CreateReadStreamAsync()
{
this.AssertAsynchronous();

Expand Down Expand Up @@ -342,7 +342,7 @@ await this.innerReader.ReadAsync()
/// </summary>
/// <returns>A task that represents the asynchronous read operation.
/// The value of the TResult parameter contains a <see cref="TextReader"/> for reading the text value.</returns>
public virtual async Task<TextReader> CreateTextReaderAsync()
public virtual async ValueTask<TextReader> CreateTextReaderAsync()
{
this.AssertAsynchronous();

Expand Down Expand Up @@ -682,7 +682,7 @@ protected virtual void ProcessObjectValue()
/// represents an in-stream error. If so, it throws an <see cref="ODataErrorException"/>. If false, this check will not happen.
/// This parsingInStremError field is set to true when trying to parse an in-stream error; in normal operation it is false.
/// </remarks>
protected async Task<bool> ReadInternalAsync()
protected async ValueTask<bool> ReadInternalAsync()
{
this.AssertAsynchronous();

Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.OData.Core/Json/IJsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public interface IJsonReader
/// </summary>
/// <returns>A task that represents the asynchronous operation.
/// The value of the TResult parameter contains true if a new node was found, or false if end of input was reached.</returns>
Task<bool> ReadAsync();
ValueTask<bool> ReadAsync();

/// <summary>
/// Asynchronously returns the value of the last reported node.
Expand All @@ -85,27 +85,27 @@ public interface IJsonReader
/// - Double if a number which doesn't fit into Int32 was found.
/// If the last node is a Property this property returns a string which is the name of the property.
/// </remarks>
Task<object> GetValueAsync();
ValueTask<object> GetValueAsync();

/// <summary>
/// Asynchronously creates a Stream for reading a binary value.
/// </summary>
/// <returns>A task that represents the asynchronous operation.
/// The value of the TResult parameter contains a <see cref="Stream"/> for reading the binary value.</returns>
Task<Stream> CreateReadStreamAsync();
ValueTask<Stream> CreateReadStreamAsync();

/// <summary>
/// Asynchronously creates a TextReader for reading a string value.
/// </summary>
/// <returns>A task that represents the asynchronous operation.
/// The value of the TResult parameter contains a <see cref="TextReader"/> for reading the string value.</returns>
Task<TextReader> CreateTextReaderAsync();
ValueTask<TextReader> CreateTextReaderAsync();

/// <summary>
/// Asynchronously checks whether or not the current value can be streamed.
/// </summary>
/// <returns>A task that represents the asynchronous operation.
/// true if the current value can be streamed; otherwise false.</returns>
Task<bool> CanStreamAsync();
ValueTask<bool> CanStreamAsync();
}
}
60 changes: 30 additions & 30 deletions src/Microsoft.OData.Core/Json/IJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,165 +213,165 @@ public interface IJsonWriter
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Obsolete("This will be dropped in the 9.x release.")]
Task StartPaddingFunctionScopeAsync();
ValueTask StartPaddingFunctionScopeAsync();

/// <summary>
/// Asynchronously ends the padding function scope.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Obsolete("This will be dropped in the 9.x release.")]
Task EndPaddingFunctionScopeAsync();
ValueTask EndPaddingFunctionScopeAsync();

/// <summary>
/// Asynchronously starts the object scope.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
Task StartObjectScopeAsync();
ValueTask StartObjectScopeAsync();

/// <summary>
/// Asynchronously ends the current object scope.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
Task EndObjectScopeAsync();
ValueTask EndObjectScopeAsync();

/// <summary>
/// Asynchronously starts the array scope.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
Task StartArrayScopeAsync();
ValueTask StartArrayScopeAsync();

/// <summary>
/// Asynchronously ends the current array scope.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
Task EndArrayScopeAsync();
ValueTask EndArrayScopeAsync();

/// <summary>
/// Asynchronously writes the name for the object property.
/// </summary>
/// <param name="name">Name of the object property.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteNameAsync(string name);
ValueTask WriteNameAsync(string name);

/// <summary>
/// Asynchronously writes a function name for JSON padding.
/// </summary>
/// <param name="functionName">Name of the padding function to write.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
[Obsolete("This will be dropped in the 9.x release.")]
Task WritePaddingFunctionNameAsync(string functionName);
ValueTask WritePaddingFunctionNameAsync(string functionName);

/// <summary>
/// Asynchronously writes a boolean value.
/// </summary>
/// <param name="value">Boolean value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(bool value);
ValueTask WriteValueAsync(bool value);

/// <summary>
/// Asynchronously writes an integer value.
/// </summary>
/// <param name="value">Integer value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(int value);
ValueTask WriteValueAsync(int value);

/// <summary>
/// Asynchronously writes a float value.
/// </summary>
/// <param name="value">Float value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(float value);
ValueTask WriteValueAsync(float value);

/// <summary>
/// Asynchronously writes a short value.
/// </summary>
/// <param name="value">Short value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(short value);
ValueTask WriteValueAsync(short value);

/// <summary>
/// Asynchronously writes a long value.
/// </summary>
/// <param name="value">Long value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(long value);
ValueTask WriteValueAsync(long value);

/// <summary>
/// Asynchronously writes a double value.
/// </summary>
/// <param name="value">Double value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(double value);
ValueTask WriteValueAsync(double value);

/// <summary>
/// Asynchronously writes a Guid value.
/// </summary>
/// <param name="value">Guid value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(Guid value);
ValueTask WriteValueAsync(Guid value);

/// <summary>
/// Asynchronously writes a decimal value
/// </summary>
/// <param name="value">Decimal value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(decimal value);
ValueTask WriteValueAsync(decimal value);

/// <summary>
/// Asynchronously writes a DateTimeOffset value
/// </summary>
/// <param name="value">DateTimeOffset value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(DateTimeOffset value);
ValueTask WriteValueAsync(DateTimeOffset value);

/// <summary>
/// Asynchronously writes a TimeSpan value
/// </summary>
/// <param name="value">TimeSpan value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(TimeSpan value);
ValueTask WriteValueAsync(TimeSpan value);

/// <summary>
/// Asynchronously writes a byte value.
/// </summary>
/// <param name="value">Byte value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(byte value);
ValueTask WriteValueAsync(byte value);

/// <summary>
/// Asynchronously writes an sbyte value.
/// </summary>
/// <param name="value">SByte value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(sbyte value);
ValueTask WriteValueAsync(sbyte value);

/// <summary>
/// Asynchronously writes a string value.
/// </summary>
/// <param name="value">String value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(string value);
ValueTask WriteValueAsync(string value);

/// <summary>
/// Asynchronously writes a byte array.
/// </summary>
/// <param name="value">Byte array to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(byte[] value);
ValueTask WriteValueAsync(byte[] value);

/// <summary>
/// Asynchronously writes a Date value
/// </summary>
/// <param name="value">Date value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(Date value);
ValueTask WriteValueAsync(Date value);

/// <summary>
/// Asynchronously writes a TimeOfDay value
/// </summary>
/// <param name="value">TimeOfDay value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(TimeOfDay value);
ValueTask WriteValueAsync(TimeOfDay value);

#if NETCOREAPP

Expand All @@ -380,15 +380,15 @@ public interface IJsonWriter
/// </summary>
/// <param name="value">The <see cref="JsonElement"/> value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteValueAsync(JsonElement value);
ValueTask WriteValueAsync(JsonElement value);
#endif

/// <summary>
/// Asynchronously writes a raw value.
/// </summary>
/// <param name="rawValue">Raw value to be written.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
Task WriteRawValueAsync(string rawValue);
ValueTask WriteRawValueAsync(string rawValue);

/// <summary>
/// Asynchronously clears all buffers for the current writer.
Expand All @@ -401,26 +401,26 @@ public interface IJsonWriter
/// </summary>
/// <returns>A task that represents the asynchronous operation.
/// The value of the TResult parameter contains the stream to write the property value to.</returns>
Task<Stream> StartStreamValueScopeAsync();
ValueTask<Stream> StartStreamValueScopeAsync();

/// <summary>
/// Asynchronously starts the TextWriter value scope.
/// </summary>
/// <param name="contentType">ContentType of the string being written.</param>
/// <returns>A task that represents the asynchronous operation.
/// The value of the TResult parameter contains the textwriter to write the text property value to.</returns>
Task<TextWriter> StartTextWriterValueScopeAsync(string contentType);
ValueTask<TextWriter> StartTextWriterValueScopeAsync(string contentType);

/// <summary>
/// Asynchronously ends the current stream property value scope.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
Task EndStreamValueScopeAsync();
ValueTask EndStreamValueScopeAsync();

/// <summary>
/// Asynchronously ends the TextWriter value scope.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
Task EndTextWriterValueScopeAsync();
ValueTask EndTextWriterValueScopeAsync();
}
}
Loading