Skip to content

Commit

Permalink
Fix compiler diagnostics in AsyncRx.NET (#1899)
Browse files Browse the repository at this point in the history
There were various warnings and messages due either to inconsistent style, or because in modern C#, newer idioms are preferred. This changes the code to be consistent with current standards.

In a couple of cases, the warnings were spurious, and have been suppressed.
  • Loading branch information
idg10 authored Mar 17, 2023
1 parent 1eda704 commit 2a8c658
Show file tree
Hide file tree
Showing 35 changed files with 128 additions and 140 deletions.
4 changes: 2 additions & 2 deletions AsyncRx.NET/ApiCompare/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace ApiCompare
{
class Program
internal class Program
{
static void Main()
private static void Main()
{
var observable = typeof(Observable).GetMethods(BindingFlags.Public | BindingFlags.Static).Select(m => m.Name).Distinct();
var asyncObservable = typeof(AsyncObservable).GetMethods(BindingFlags.Public | BindingFlags.Static).Select(m => m.Name).Distinct();
Expand Down
66 changes: 33 additions & 33 deletions AsyncRx.NET/Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.

// The intention is that people will uncomment whichever method call in Main they want to try.
// The following suppressions prevent warnings due to 'unused' members, and the fact that all of
// the await statements in Main are commented out to start with
#pragma warning disable IDE0051, CS1998

using System;
using System.Linq;
using System.Reactive;
Expand All @@ -12,16 +17,9 @@

namespace Playground
{
static class Program
internal static class Program
{
static void Main()
{
MainAsync().GetAwaiter().GetResult();

Console.ReadLine();
}

static async Task MainAsync()
private static async Task Main()
{
//await AggregateAsync();
//await AllAsync();
Expand All @@ -45,34 +43,36 @@ static async Task MainAsync()
//await TakeUntilAsync();
//await TimerAsync();
//await WhileAsync();

Console.ReadLine();
}

static async Task AggregateAsync()
private static async Task AggregateAsync()
{
await AsyncObservable.Range(0, 10).Aggregate(0, (sum, x) => sum + x).SubscribeAsync(Print<int>());
}

static async Task AllAsync()
private static async Task AllAsync()
{
await AsyncObservable.Range(0, 10).All(x => x < 10).SubscribeAsync(Print<bool>());
}

static async Task AnyAsync()
private static async Task AnyAsync()
{
await AsyncObservable.Range(0, 10).Any(x => x == 5).SubscribeAsync(Print<bool>());
}

static async Task AppendAsync()
private static async Task AppendAsync()
{
await AsyncObservable.Range(0, 10).Append(42).SubscribeAsync(Print<int>());
}

static async Task AwaitAsync()
private static async Task AwaitAsync()
{
Console.WriteLine(await AsyncObservable.Range(0, 10));
}

static async Task BufferTimeHoppingAsync()
private static async Task BufferTimeHoppingAsync()
{
await
AsyncObservable
Expand All @@ -82,7 +82,7 @@ static async Task BufferTimeHoppingAsync()
.SubscribeAsync(Print<string>()); // TODO: Use ForEachAsync.
}

static async Task BufferTimeSlidingAsync()
private static async Task BufferTimeSlidingAsync()
{
await
AsyncObservable
Expand All @@ -93,7 +93,7 @@ static async Task BufferTimeSlidingAsync()
.SubscribeAsync(Print<string>()); // TODO: Use ForEachAsync.
}

static async Task CombineLatestAsync()
private static async Task CombineLatestAsync()
{
await
AsyncObservable.CombineLatest(
Expand All @@ -104,7 +104,7 @@ static async Task CombineLatestAsync()
.SubscribeAsync(Print<string>()); // TODO: Use ForEachAsync.
}

static async Task ConcatAsync()
private static async Task ConcatAsync()
{
await
AsyncObservable.Concat(
Expand All @@ -116,7 +116,7 @@ static async Task ConcatAsync()
.SubscribeAsync(Print<int>()); // TODO: Use ForEachAsync.
}

static async Task DelayAsync()
private static async Task DelayAsync()
{
await
AsyncObservable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1))
Expand All @@ -127,7 +127,7 @@ static async Task DelayAsync()
.SubscribeAsync(Print<string>()); // TODO: Use ForEachAsync.
}

static async Task GroupByAsync()
private static async Task GroupByAsync()
{
await
AsyncObservable.Interval(TimeSpan.FromMilliseconds(250))
Expand All @@ -140,7 +140,7 @@ static async Task GroupByAsync()
});
}

static async Task GroupBySelectManyAsync()
private static async Task GroupBySelectManyAsync()
{
await
AsyncObservable.Interval(TimeSpan.FromMilliseconds(250))
Expand All @@ -151,7 +151,7 @@ static async Task GroupBySelectManyAsync()
.SubscribeAsync(Print<string>());
}

static async Task MergeAsync()
private static async Task MergeAsync()
{
var subject = new SequentialSimpleAsyncSubject<IAsyncObservable<int>>();

Expand All @@ -167,17 +167,17 @@ static async Task MergeAsync()
await subject.OnCompletedAsync();
}

static async Task PrependAsync()
private static async Task PrependAsync()
{
await AsyncObservable.Range(0, 10).Prepend(42).SubscribeAsync(Print<int>());
}

static async Task RangeAsync()
private static async Task RangeAsync()
{
await AsyncObservable.Range(0, 10).SubscribeAsync(PrintAsync<int>()); // TODO: Use ForEachAsync.
}

static async Task ReplaySubjectAsync()
private static async Task ReplaySubjectAsync()
{
var sub = new SequentialReplayAsyncSubject<int>(5);

Expand Down Expand Up @@ -208,12 +208,12 @@ static async Task ReplaySubjectAsync()
await sub.OnNextAsync(47);
}

static async Task ReturnAsync()
private static async Task ReturnAsync()
{
await AsyncObservable.Return(42).SubscribeAsync(Print<int>());
}

static async Task SelectManyAsync()
private static async Task SelectManyAsync()
{
var res = from i in AsyncObservable.Range(0, 10)
from j in AsyncObservable.Range(i * 10, 10)
Expand All @@ -222,7 +222,7 @@ from j in AsyncObservable.Range(i * 10, 10)
await res.SubscribeAsync(Print<string>());
}

static async Task SubjectAsync()
private static async Task SubjectAsync()
{
var subject = new SequentialSimpleAsyncSubject<int>();

Expand All @@ -238,24 +238,24 @@ static async Task SubjectAsync()
await subject.OnCompletedAsync();
}

static async Task TakeUntilAsync()
private static async Task TakeUntilAsync()
{
await AsyncObservable.Range(0, int.MaxValue).TakeUntil(DateTimeOffset.Now.AddSeconds(5)).SubscribeAsync(Print<int>()); // TODO: Use ForEachAsync.
}

static async Task TimerAsync()
private static async Task TimerAsync()
{
await AsyncObservable.Timer(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2)).Take(5).Select(_ => DateTimeOffset.Now).SubscribeAsync(Print<DateTimeOffset>()); // TODO: Use ForEachAsync.
}

static async Task WhileAsync()
private static async Task WhileAsync()
{
var i = 0;

await AsyncObservable.While(() => ++i < 5, AsyncObservable.Range(0, 5)).SubscribeAsync(Print<int>()); // TODO: Use ForEachAsync.
}

static IAsyncObserver<T> Print<T>()
private static IAsyncObserver<T> Print<T>()
{
return AsyncObserver.Create<T>(
x =>
Expand All @@ -276,7 +276,7 @@ static IAsyncObserver<T> Print<T>()
);
}

static IAsyncObserver<T> PrintAsync<T>()
private static IAsyncObserver<T> PrintAsync<T>()
{
return AsyncObserver.Create<T>(
async x =>
Expand Down
4 changes: 2 additions & 2 deletions AsyncRx.NET/System.Reactive.Async/AsyncObservableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async ValueTask<IAsyncDisposable> SubscribeAsync(IAsyncObserver<T> observ
private sealed class AutoDetachAsyncObserver : AsyncObserverBase<T>, IAsyncDisposable
{
private readonly IAsyncObserver<T> _observer;
private readonly object _gate = new object();
private readonly object _gate = new();

private IAsyncDisposable _subscription;
private ValueTask _task;
Expand Down Expand Up @@ -62,7 +62,7 @@ public async ValueTask AssignAsync(IAsyncDisposable subscription)

public async ValueTask DisposeAsync()
{
var task = default(ValueTask);
ValueTask task;
var subscription = default(IAsyncDisposable);

lock (_gate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public RendezVousAwaitable(IAsyncScheduler scheduler, CancellationToken token)
_token = token;
}

public RendezVousAwaiter GetAwaiter() => new RendezVousAwaiter(_scheduler, _token);
public RendezVousAwaiter GetAwaiter() => new(_scheduler, _token);

public sealed class RendezVousAwaiter : INotifyCompletion
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected virtual async ValueTask<IAsyncDisposable> ScheduleAsyncCore(Func<Cance

private sealed class CancellationAsyncDisposable : IAsyncDisposable
{
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
private readonly CancellationTokenSource _cts = new();

public CancellationToken Token => _cts.Token;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class ImmediateAsyncScheduler : AsyncSchedulerBase

private ImmediateAsyncScheduler() { }

protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new ValueTask(Task.Delay(dueTime));
protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new(Task.Delay(dueTime));

protected override ValueTask ScheduleAsyncCore(Func<CancellationToken, ValueTask> action, CancellationToken token) => action(token);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public SynchronizationContextAsyncScheduler(SynchronizationContext context)
_context = context ?? throw new ArgumentNullException(nameof(context));
}

protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new ValueTask(Task.Delay(dueTime, token));
protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new(Task.Delay(dueTime, token));

protected override ValueTask ScheduleAsyncCore(Func<CancellationToken, ValueTask> action, CancellationToken token)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public TaskPoolAsyncScheduler(TaskFactory factory)
_factory = factory ?? throw new ArgumentNullException(nameof(factory));
}

protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new ValueTask(Task.Delay(dueTime, token));
protected override ValueTask Delay(TimeSpan dueTime, CancellationToken token) => new(Task.Delay(dueTime, token));

protected override ValueTask ScheduleAsyncCore(Func<CancellationToken, ValueTask> action, CancellationToken token)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace System.Reactive.Disposables
{
public sealed class CompositeAsyncDisposable : IAsyncDisposable
{
private readonly AsyncGate _gate = new AsyncGate();
private readonly AsyncGate _gate = new();
private readonly List<IAsyncDisposable> _disposables;
private bool _disposed;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace System.Reactive.Disposables
{
public sealed class RefCountAsyncDisposable : IAsyncDisposable
{
private readonly AsyncGate _gate = new AsyncGate();
private readonly AsyncGate _gate = new();
private IAsyncDisposable _disposable;
private bool _primaryDisposed;
private int _count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace System.Reactive.Disposables
{
public sealed class SerialAsyncDisposable : IAsyncDisposable
{
private readonly AsyncGate _gate = new AsyncGate();
private readonly AsyncGate _gate = new();

private IAsyncDisposable _disposable;
private bool _disposed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace System.Reactive
{
internal sealed class AsyncQueueLockAsyncObserver<T> : AsyncObserverBase<T>
{
private readonly AsyncQueueLock _gate = new AsyncQueueLock();
private readonly AsyncQueueLock _gate = new();
private readonly IAsyncObserver<T> _observer;

public AsyncQueueLockAsyncObserver(IAsyncObserver<T> observer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ protected void Remove(Delegate handler)
}
}

if (d != null)
{
d.DisposeAsync().GetAwaiter().GetResult();
}
d?.DisposeAsync().GetAwaiter().GetResult();
}
}
}
5 changes: 1 addition & 4 deletions AsyncRx.NET/System.Reactive.Async/Internal/EventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ private void Remove(Delegate handler)
}
}

if (d != null)
{
d.DisposeAsync().GetAwaiter().GetResult();
}
d?.DisposeAsync().GetAwaiter().GetResult();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace System.Reactive
{
internal sealed class FastImmediateAsyncObserver<T> : ScheduledAsyncObserverBase<T>
{
private readonly CancellationAsyncDisposable _disposable = new CancellationAsyncDisposable();
private readonly CancellationAsyncDisposable _disposable = new();

public FastImmediateAsyncObserver(IAsyncObserver<T> observer)
: base(observer)
Expand All @@ -19,9 +19,9 @@ public FastImmediateAsyncObserver(IAsyncObserver<T> observer)

public override ValueTask DisposeAsync() => _disposable.DisposeAsync();

protected override ValueTaskAwaitable RendezVous(ValueTask task) => new ValueTaskAwaitable(task, continueOnCapturedContext: false, scheduler: null, CancellationToken.None);
protected override ValueTaskAwaitable RendezVous(ValueTask task) => new(task, continueOnCapturedContext: false, scheduler: null, CancellationToken.None);

protected override ValueTaskAwaitable<R> RendezVous<R>(ValueTask<R> task) => new ValueTaskAwaitable<R>(task, continueOnCapturedContext: false, scheduler: null, CancellationToken.None);
protected override ValueTaskAwaitable<R> RendezVous<R>(ValueTask<R> task) => new(task, continueOnCapturedContext: false, scheduler: null, CancellationToken.None);

protected override ValueTask ScheduleAsync() => RunAsync(_disposable.Token);
}
Expand Down
Loading

0 comments on commit 2a8c658

Please sign in to comment.