Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: SimonCropp/Polyfill
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6.3.0
Choose a base ref
...
head repository: SimonCropp/Polyfill
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 6.4.0
Choose a head ref
  • 14 commits
  • 17 files changed
  • 1 contributor

Commits on Aug 16, 2024

  1. docs

    SimonCropp committed Aug 16, 2024
    Copy the full SHA
    ea9ea18 View commit details
  2. Update readme.md

    SimonCropp committed Aug 16, 2024
    Copy the full SHA
    d43262f View commit details
  3. docs

    SimonCropp committed Aug 16, 2024
    Copy the full SHA
    4c64dda View commit details
  4. Update readme.md

    SimonCropp committed Aug 16, 2024
    Copy the full SHA
    dfc2868 View commit details
  5. Update readme.md

    SimonCropp committed Aug 16, 2024
    Copy the full SHA
    52ceed7 View commit details
  6. docs

    SimonCropp committed Aug 16, 2024
    Copy the full SHA
    324aa04 View commit details
  7. add RequiresPreviewFeaturesAttribute

    SimonCropp committed Aug 16, 2024
    Copy the full SHA
    bf797bd View commit details
  8. Update RequiresPreviewFeaturesAttribute .cs

    SimonCropp authored Aug 16, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    6458b3b View commit details
  9. Merge pull request #208 from SimonCropp/add-RequiresPreviewFeaturesAt…

    …tribute-
    
    add RequiresPreviewFeaturesAttribute
    SimonCropp authored Aug 16, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    a7c118d View commit details
  10. cleanup

    SimonCropp committed Aug 16, 2024
    Copy the full SHA
    7f99487 View commit details

Commits on Aug 17, 2024

  1. Update NullabilityInfo.cs

    SimonCropp committed Aug 17, 2024
    Copy the full SHA
    44de6a4 View commit details

Commits on Aug 20, 2024

  1. add TextWriter WriteAsync and Write for StringBuilder

    SimonCropp committed Aug 20, 2024
    Copy the full SHA
    0055d1d View commit details
  2. Merge pull request #209 from SimonCropp/add-TextWriter-WriteAsync-and…

    …-Write-for-StringBuilder
    
    add TextWriter WriteAsync and Write for StringBuilder
    SimonCropp authored Aug 20, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    63223fd View commit details
  3. docs

    SimonCropp committed Aug 20, 2024
    Copy the full SHA
    c4465c1 View commit details
2 changes: 1 addition & 1 deletion apiCount.include.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**API count: 329**
**API count: 332**
2 changes: 2 additions & 0 deletions api_list.include.md
Original file line number Diff line number Diff line change
@@ -344,7 +344,9 @@

#### TextWriter

* `void Write(StringBuilder)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.write#system-io-textwriter-write(system-text-stringbuilder))
* `void Write(ReadOnlySpan<Char>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.write#system-io-textwriter-write(system-readonlyspan((system-char))))
* `Task WriteAsync(StringBuilder, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.writeasync#system-io-textwriter-writeasync(system-readonlymemory((system-char))-system-threading-cancellationtoken))
* `ValueTask WriteAsync(ReadOnlyMemory<Char>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.writeasync#system-io-textwriter-writeasync(system-readonlymemory((system-char))-system-threading-cancellationtoken))
* `void WriteLine(ReadOnlySpan<Char>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.writeline#system-io-textwriter-writeline(system-readonlyspan((system-char))))
* `ValueTask WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.writelineasync#system-io-textwriter-writelineasync(system-readonlymemory((system-char))-system-threading-cancellationtoken))
56 changes: 54 additions & 2 deletions contributing.md
Original file line number Diff line number Diff line change
@@ -189,10 +189,12 @@ Example:
// <auto-generated />
#pragma warning disable

#if FeatureMemory && (NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0)
#if FeatureMemory

namespace Polyfills;

using System;
using System.Text;
using System.Buffers;
using System.IO;
using System.Runtime.InteropServices;
@@ -202,7 +204,56 @@ using System.Threading.Tasks;

static partial class Polyfill
{
#if !NETCOREAPP3_0_OR_GREATER
/// <summary>
/// Equivalent to Write(stringBuilder.ToString()) however it uses the
/// StringBuilder.GetChunks() method to avoid creating the intermediate string
/// </summary>
/// <param name="value">The string (as a StringBuilder) to write to the stream</param>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.write#system-io-textwriter-write(system-text-stringbuilder)")]
public static void Write(this TextWriter target, StringBuilder? value)
{
if (value != null)
{
foreach (ReadOnlyMemory<char> chunk in value.GetChunks())
{
target.Write(chunk.Span);
}
}
}

/// <summary>
/// Equivalent to WriteAsync(stringBuilder.ToString()) however it uses the
/// StringBuilder.GetChunks() method to avoid creating the intermediate string
/// </summary>
/// <param name="value">The string (as a StringBuilder) to write to the stream</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.writeasync#system-io-textwriter-writeasync(system-readonlymemory((system-char))-system-threading-cancellationtoken)")]
public static Task WriteAsync(this TextWriter target, StringBuilder? value, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

if (value == null)
{
return Task.CompletedTask;
}

return WriteAsyncCore(value, cancellationToken);

async Task WriteAsyncCore(StringBuilder builder, CancellationToken cancel)
{
foreach (ReadOnlyMemory<char> chunk in builder.GetChunks())
{
await target.WriteAsync(chunk, cancel).ConfigureAwait(false);
}
}
}
#endif

#if NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0
#if FeatureValueTask

/// <summary>
@@ -304,10 +355,11 @@ static partial class Polyfill
pool.Return(array);
}
}
#endif
}
#endif
```
<sup><a href='/src/Polyfill/Polyfill_TextWriter.cs#L1-L120' title='Snippet source file'>snippet source</a> | <a href='#snippet-Polyfill_TextWriter.cs' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Polyfill/Polyfill_TextWriter.cs#L1-L172' title='Snippet source file'>snippet source</a> | <a href='#snippet-Polyfill_TextWriter.cs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


52 changes: 51 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ The package targets `netstandard2.0` and is designed to support the following ru
* `net5.0`, `net6.0`, `net7.0`, `net8.0`, `net9.0`


**API count: 329**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
**API count: 332**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->


**See [Milestones](../../milestones?state=closed) for release notes.**
@@ -275,6 +275,47 @@ class IndexRangeSample
<!-- endSnippet -->


### OverloadResolutionPriority

* [Release Notes](https://github.com/dotnet/core/blob/main/release-notes/9.0/preview/preview7/csharp.md#prioritize-better-overloads-with-overloadresolutionpriority-attribute)

> C# introduces a new attribute, System.Runtime.CompilerServices.OverloadResolutionPriority, that can be used by API authors to adjust the relative priority of overloads within a single type as a means of steering API consumers to use specific APIs, even if those APIs would normally be considered ambiguous or otherwise not be chosen by C#'s overload resolution rules. This helps framework and library authors guide API usage as they APIs as they develop new and better patterns.
>
> The OverloadResolutionPriorityAttribute can be used in conjunction with the ObsoleteAttribute. A library author may mark properties, methods, types and other programming elements as obsolete, while leaving them in place for backwards compatibility. Using programming elements marked with the ObsoleteAttribute will result in compiler warnings or errors. However, the type or member is still visible to overload resolution and may be selected over a better overload or cause an ambiguity failure. The OverloadResolutionPriorityAttribute lets library authors fix these problems by lowering the priority of obsolete members when there are better alternatives.
* [API on learn](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.overloadresolutionpriorityattribute.-ctor)
* [API Proposal](https://github.com/dotnet/runtime/issues/102173)


#### Usage

<!-- snippet: OverloadResolutionPriority -->
<a id='snippet-OverloadResolutionPriority'></a>
```cs
[TestFixture]
public class OverloadResolutionPriorityAttributeTests
{
[Test]
public void Run()
{
int[] arr = [1, 2, 3];
//Prints "Span" because resolution priority is higher
Method(arr);
}

[OverloadResolutionPriority(2)]
static void Method(ReadOnlySpan<int> list) =>
Console.WriteLine("Span");

[OverloadResolutionPriority(1)]
static void Method(int[] list) =>
Console.WriteLine("Array");
}
```
<sup><a href='/src/Tests/OverloadResolutionPriorityAttributeTests.cs#L4-L26' title='Snippet source file'>snippet source</a> | <a href='#snippet-OverloadResolutionPriority' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


### UnscopedRefAttribute

* [UnscopedRefAttribute](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.unscopedrefattribute)
@@ -301,6 +342,13 @@ struct UnscopedRefUsage
<!-- endSnippet -->


### RequiresPreviewFeaturesAttribute

* [RequiresPreviewFeatures](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.versioning.requirespreviewfeaturesattribute)
* [Design](https://github.com/dotnet/designs/blob/main/accepted/2021/preview-features/preview-features.md)
* [API Proposal](https://github.com/dapr/dotnet-sdk/issues/1219)


### CallerArgumentExpressionAttribute

* [CallerArgumentExpressionAttribute](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerargumentexpressionattribute)
@@ -753,7 +801,9 @@ The class `Polyfill` includes the following extension methods:

#### TextWriter

* `void Write(StringBuilder)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.write#system-io-textwriter-write(system-text-stringbuilder))
* `void Write(ReadOnlySpan<Char>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.write#system-io-textwriter-write(system-readonlyspan((system-char))))
* `Task WriteAsync(StringBuilder, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.writeasync#system-io-textwriter-writeasync(system-readonlymemory((system-char))-system-threading-cancellationtoken))
* `ValueTask WriteAsync(ReadOnlyMemory<Char>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.writeasync#system-io-textwriter-writeasync(system-readonlymemory((system-char))-system-threading-cancellationtoken))
* `void WriteLine(ReadOnlySpan<Char>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.writeline#system-io-textwriter-writeline(system-readonlyspan((system-char))))
* `ValueTask WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.writelineasync#system-io-textwriter-writelineasync(system-readonlymemory((system-char))-system-threading-cancellationtoken))
31 changes: 31 additions & 0 deletions src/Consume/Consume.cs
Original file line number Diff line number Diff line change
@@ -482,6 +482,17 @@ void NonGenericTaskCompletionSource()
}

#if FeatureMemory
async Task TextWriter()
{
TextWriter target = new StringWriter();
target.Write(new StringBuilder());
target.WriteAsync(new StringBuilder());
target.WriteLine("a".AsSpan());
await target.WriteLineAsync("a".AsMemory());
target.Write("a".AsSpan());
await target.WriteAsync("a".AsMemory());
}

void RandomNextBytesSpan()
{
var random = new Random();
@@ -510,4 +521,24 @@ public void SortedList()
var key = list.GetKeyAtIndex(0);
var value = list.GetValueAtIndex(0);
}

[OverloadResolutionPriority(1)]
public void Method(int x)
{
}

[OverloadResolutionPriority(2)]
public void Method(string x)
{
}

[OverloadResolutionPriority(3)]
public void Method(object x)
{
}

[RequiresPreviewFeatures("This method uses a preview feature.")]
public void UsePreviewFeature()
{
}
}
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>6.3.0</Version>
<Version>6.4.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Polyfill</PackageTags>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
4 changes: 1 addition & 3 deletions src/Polyfill/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -16,8 +16,6 @@ namespace System.Runtime.CompilerServices;
#if PolyPublic
public
#endif
static class IsExternalInit
{
}
static class IsExternalInit;

#endif
2 changes: 2 additions & 0 deletions src/Polyfill/Nullability/NullabilityInfo.cs
Original file line number Diff line number Diff line change
@@ -20,12 +20,14 @@ namespace System.Reflection;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Link = System.ComponentModel.DescriptionAttribute;

/// <summary>
/// A class that represents nullability info
/// </summary>
[DebuggerNonUserCode]
[ExcludeFromCodeCoverage]
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.reflection.nullabilityinfo?view=net-8.0")]
#if PolyPublic
public
#endif
6 changes: 4 additions & 2 deletions src/Polyfill/OverloadResolutionPriorityAttribute.cs
Original file line number Diff line number Diff line change
@@ -22,12 +22,14 @@ namespace System.Runtime.CompilerServices;
#if PolyPublic
public
#endif
sealed class OverloadResolutionPriorityAttribute : Attribute
sealed class OverloadResolutionPriorityAttribute :
Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="OverloadResolutionPriorityAttribute"/> class.
/// </summary>
/// <param name="priority">The priority of the attributed member. Higher numbers are prioritized, lower numbers are deprioritized. 0 is the default if no attribute is present.</param>
/// <param name="priority">The priority of the attributed member. Higher numbers are prioritized,
/// lower numbers are deprioritized. 0 is the default if no attribute is present.</param>
public OverloadResolutionPriorityAttribute(int priority) =>
Priority = priority;

4 changes: 1 addition & 3 deletions src/Polyfill/Polyfill.cs
Original file line number Diff line number Diff line change
@@ -14,6 +14,4 @@ namespace Polyfills;
#if PolyPublic
public
#endif
static partial class Polyfill
{
}
static partial class Polyfill;
54 changes: 53 additions & 1 deletion src/Polyfill/Polyfill_TextWriter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// <auto-generated />
#pragma warning disable

#if FeatureMemory && (NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0)
#if FeatureMemory

namespace Polyfills;

using System;
using System.Text;
using System.Buffers;
using System.IO;
using System.Runtime.InteropServices;
@@ -14,7 +16,56 @@ namespace Polyfills;

static partial class Polyfill
{
#if !NETCOREAPP3_0_OR_GREATER
/// <summary>
/// Equivalent to Write(stringBuilder.ToString()) however it uses the
/// StringBuilder.GetChunks() method to avoid creating the intermediate string
/// </summary>
/// <param name="value">The string (as a StringBuilder) to write to the stream</param>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.write#system-io-textwriter-write(system-text-stringbuilder)")]
public static void Write(this TextWriter target, StringBuilder? value)
{
if (value != null)
{
foreach (ReadOnlyMemory<char> chunk in value.GetChunks())
{
target.Write(chunk.Span);
}
}
}

/// <summary>
/// Equivalent to WriteAsync(stringBuilder.ToString()) however it uses the
/// StringBuilder.GetChunks() method to avoid creating the intermediate string
/// </summary>
/// <param name="value">The string (as a StringBuilder) to write to the stream</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.io.textwriter.writeasync#system-io-textwriter-writeasync(system-readonlymemory((system-char))-system-threading-cancellationtoken)")]
public static Task WriteAsync(this TextWriter target, StringBuilder? value, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

if (value == null)
{
return Task.CompletedTask;
}

return WriteAsyncCore(value, cancellationToken);

async Task WriteAsyncCore(StringBuilder builder, CancellationToken cancel)
{
foreach (ReadOnlyMemory<char> chunk in builder.GetChunks())
{
await target.WriteAsync(chunk, cancel).ConfigureAwait(false);
}
}
}
#endif

#if NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0
#if FeatureValueTask

/// <summary>
@@ -116,5 +167,6 @@ public static void WriteLine(
pool.Return(array);
}
}
#endif
}
#endif
10 changes: 4 additions & 6 deletions src/Polyfill/Polyfill_Type.cs
Original file line number Diff line number Diff line change
@@ -20,15 +20,13 @@ public static bool HasSameMetadataDefinitionAs(this MemberInfo target, MemberInf
/// Gets a value that indicates whether the current Type represents a type parameter in the definition of a generic method.
/// </summary>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.type.isgenericmethodparameter")]
public static bool IsGenericMethodParameter(this Type target)
{
public static bool IsGenericMethodParameter(this Type target) =>
#if NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0
return target.IsGenericParameter &&
target.DeclaringMethod != null;
target.IsGenericParameter &&
target.DeclaringMethod != null;
#else
return target.IsGenericMethodParameter;
target.IsGenericMethodParameter;
#endif
}

/// <summary>
/// Generic version of Type.IsAssignableTo https://learn.microsoft.com/en-us/dotnet/api/system.type.isassignableto.
Loading