From 94b32566582a3069feb653c2035b655092fd05d2 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 7 Nov 2024 21:49:38 +1100 Subject: [PATCH] Add ConcurrentQueue Clear (#246) --- apiCount.include.md | 2 +- api_list.include.md | 5 +++++ readme.md | 7 ++++++- src/Consume/Consume.cs | 6 ++++++ src/Polyfill/Polyfill_ConcurrentBag.cs | 5 ----- src/Polyfill/Polyfill_ConcurrentQueue.cs | 23 ++++++++++++++++++++++ src/Tests/PolyfillTests_ConcurrentQueue.cs | 11 +++++++++++ 7 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 src/Polyfill/Polyfill_ConcurrentQueue.cs create mode 100644 src/Tests/PolyfillTests_ConcurrentQueue.cs diff --git a/apiCount.include.md b/apiCount.include.md index be9f51c3..70e26978 100644 --- a/apiCount.include.md +++ b/apiCount.include.md @@ -1 +1 @@ -**API count: 401** \ No newline at end of file +**API count: 402** \ No newline at end of file diff --git a/api_list.include.md b/api_list.include.md index 8ea86b1e..380d3ab8 100644 --- a/api_list.include.md +++ b/api_list.include.md @@ -32,6 +32,11 @@ * `TValue GetOrAdd(TKey, Func, TArg) where TKey : notnull` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.getoradd#system-collections-concurrent-concurrentdictionary-2-getoradd-1(-0-system-func((-0-0-1))-0)) +#### ConcurrentQueue + + * `void Clear()` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentqueue-1.clear) + + #### DateOnly * `bool TryFormat(Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.dateonly.tryformat) diff --git a/readme.md b/readme.md index fd6216fe..bf9db3c4 100644 --- a/readme.md +++ b/readme.md @@ -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: 401** +**API count: 402** **See [Milestones](../../milestones?state=closed) for release notes.** @@ -485,6 +485,11 @@ The class `Polyfill` includes the following extension methods: * `TValue GetOrAdd(TKey, Func, TArg) where TKey : notnull` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.getoradd#system-collections-concurrent-concurrentdictionary-2-getoradd-1(-0-system-func((-0-0-1))-0)) +#### ConcurrentQueue + + * `void Clear()` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentqueue-1.clear) + + #### DateOnly * `bool TryFormat(Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.dateonly.tryformat) diff --git a/src/Consume/Consume.cs b/src/Consume/Consume.cs index 1485e9b8..187695df 100644 --- a/src/Consume/Consume.cs +++ b/src/Consume/Consume.cs @@ -228,6 +228,12 @@ void ConcurrentBag_Methods() bag.Clear(); } + void ConcurrentQueue_Methods() + { + var bag = new ConcurrentQueue(); + bag.Clear(); + } + void Dictionary_Methods() { var dictionary = new Dictionary { { "key", "value" } }; diff --git a/src/Polyfill/Polyfill_ConcurrentBag.cs b/src/Polyfill/Polyfill_ConcurrentBag.cs index dfd30bd1..ec748079 100644 --- a/src/Polyfill/Polyfill_ConcurrentBag.cs +++ b/src/Polyfill/Polyfill_ConcurrentBag.cs @@ -17,11 +17,6 @@ static partial class Polyfill [Link("https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentbag-1.clear")] public static void Clear(this ConcurrentBag target) { - if (target is null) - { - throw new ArgumentNullException(nameof(target)); - } - while (!target.IsEmpty) { target.TryTake(out _); diff --git a/src/Polyfill/Polyfill_ConcurrentQueue.cs b/src/Polyfill/Polyfill_ConcurrentQueue.cs new file mode 100644 index 00000000..6916a13c --- /dev/null +++ b/src/Polyfill/Polyfill_ConcurrentQueue.cs @@ -0,0 +1,23 @@ +// +#pragma warning disable + +#if NETFRAMEWORK || NETSTANDARD2_0 + +namespace Polyfills; + +using System; +using System.Collections.Concurrent; +using Link = System.ComponentModel.DescriptionAttribute; + +static partial class Polyfill +{ + /// + /// Removes all values from the . + /// + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentqueue-1.clear")] + public static void Clear(this ConcurrentQueue target) + { + while (target.TryDequeue(out _)); + } +} +#endif \ No newline at end of file diff --git a/src/Tests/PolyfillTests_ConcurrentQueue.cs b/src/Tests/PolyfillTests_ConcurrentQueue.cs new file mode 100644 index 00000000..8bc6062b --- /dev/null +++ b/src/Tests/PolyfillTests_ConcurrentQueue.cs @@ -0,0 +1,11 @@ +partial class PolyfillTests +{ + [Test] + public void ConcurrentQueueClear() + { + var bag = new ConcurrentQueue(); + bag.Enqueue("Hello"); + bag.Clear(); + Assert.AreEqual(0, bag.Count); + } +} \ No newline at end of file