Skip to content

Commit

Permalink
Merge pull request #1298 from dotnet/dev/bartde/modernize_code
Browse files Browse the repository at this point in the history
Modernize some code
  • Loading branch information
bartdesmet committed Sep 25, 2020
2 parents 3a66b0b + e592c18 commit 22cbcb1
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 76 deletions.
15 changes: 5 additions & 10 deletions Rx.NET/Source/src/System.Reactive/Internal/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,12 @@ internal static class Helpers
{
public static int? GetLength<T>(IEnumerable<T> source)
{
if (source is T[] array)
return source switch
{
return array.Length;
}

if (source is IList<T> list)
{
return list.Count;
}

return null;
T[] array => array.Length,
IList<T> list => list.Count,
_ => null
};
}

public static IObservable<T> Unpack<T>(IObservable<T> source)
Expand Down
2 changes: 0 additions & 2 deletions Rx.NET/Source/src/System.Reactive/Internal/Lookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public Lookup(IEqualityComparer<K> comparer)

public void Add(K key, E element)
{

if (!_dictionary.TryGetValue(key, out var list))
{
_dictionary[key] = list = new List<E>();
Expand All @@ -36,7 +35,6 @@ public IEnumerable<E> this[K key]
{
get
{

if (!_dictionary.TryGetValue(key, out var list))
{
return Enumerable.Empty<E>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,8 @@ public static class PlatformEnlightenmentProvider
[Obsolete("This mechanism will be removed in the next major version", false)]
public static IPlatformEnlightenmentProvider Current
{
get
{
return _current;
}

set
{
_current = value ?? throw new ArgumentNullException(nameof(value));
}
get => _current;
set => _current = value ?? throw new ArgumentNullException(nameof(value));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ private void Dispatch(ICancelable cancel)
return;
}

var next = default(T);
while (_queue.TryDequeue(out next))
while (_queue.TryDequeue(out var next))
{
try
{
Expand Down Expand Up @@ -205,7 +204,8 @@ private void EnsureActiveSlow()

private void Run(object state, Action<object> recurse)
{
var next = default(T);
T next;

while (!_queue.TryDequeue(out next))
{
if (_failed)
Expand Down
25 changes: 5 additions & 20 deletions Rx.NET/Source/src/System.Reactive/Internal/Sink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,9 @@ public virtual void Run(IObservable<TSource> source)

public abstract void OnNext(TSource value);

public virtual void OnError(Exception error)
{
ForwardOnError(error);
}
public virtual void OnError(Exception error) => ForwardOnError(error);

public virtual void OnCompleted()
{
ForwardOnCompleted();
}
public virtual void OnCompleted() => ForwardOnCompleted();

public IObserver<TTarget> GetForwarder() => new _(this);

Expand All @@ -112,20 +106,11 @@ public _(Sink<TSource, TTarget> forward)
_forward = forward;
}

public void OnNext(TTarget value)
{
_forward.ForwardOnNext(value);
}
public void OnNext(TTarget value) => _forward.ForwardOnNext(value);

public void OnError(Exception error)
{
_forward.ForwardOnError(error);
}
public void OnError(Exception error) => _forward.ForwardOnError(error);

public void OnCompleted()
{
_forward.ForwardOnCompleted();
}
public void OnCompleted() => _forward.ForwardOnCompleted();
}
}
}
5 changes: 1 addition & 4 deletions Rx.NET/Source/src/System.Reactive/Internal/StopwatchImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ internal class /*Default*/StopwatchImpl : IStopwatch
{
private readonly Stopwatch _sw;

public StopwatchImpl()
{
_sw = Stopwatch.StartNew();
}
public StopwatchImpl() => _sw = Stopwatch.StartNew();

public TimeSpan Elapsed => _sw.Elapsed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ private void Drain()
var currentEnumerator = _stack.Peek();

var currentObservable = default(IObservable<TSource>);
var next = default(IObservable<TSource>);

try
{
Expand All @@ -84,14 +83,14 @@ private void Drain()
continue;
}

IObservable<TSource> next;

try
{
next = Helpers.Unpack(currentObservable);

}
catch (Exception ex)
{
next = null;
if (!Fail(ex))
{
Volatile.Write(ref _isDisposed, true);
Expand Down
5 changes: 1 addition & 4 deletions Rx.NET/Source/src/System.Reactive/ListObservable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@ public int Count
/// <summary>
/// Gets a value that indicates whether the ListObservable is read-only.
/// </summary>
public bool IsReadOnly
{
get { return false; }
}
public bool IsReadOnly => false;

/// <summary>
/// Removes the first occurrence of a specific object from the ListObservable.
Expand Down
2 changes: 1 addition & 1 deletion Rx.NET/Source/src/System.Reactive/Notification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ public override TResult Accept<TResult>(Func<T, TResult> onNext, Func<Exception,
return true;
}

if ((object)left == null || (object)right == null)
if (left is null || right is null)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void SetException(Exception exception)
/// <summary>
/// Gets the observable sequence for this builder.
/// </summary>
public ITaskObservable<T> Task => _inner ?? (_inner = new TaskObservable());
public ITaskObservable<T> Task => _inner ??= new TaskObservable();

/// <summary>
/// Schedules the state machine to proceed to the next action when the specified awaiter completes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Linq.ObservableImpl;
using System.Reactive.Subjects;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -77,7 +76,9 @@ public IDisposable Subscribe(IObserver<TResult> observer)
var options = GetTaskContinuationOptions(_scheduler);

if (_scheduler == null)
{
_task.ContinueWith((t, subjectObject) => t.EmitTaskResult((IObserver<TResult>)subjectObject), observer, cts.Token, options, TaskScheduler.Current);
}
else
{
_task.ContinueWithState(
Expand Down Expand Up @@ -136,17 +137,14 @@ private static IObservable<Unit> ToObservableImpl(Task task, IScheduler schedule
{
if (task.IsCompleted)
{
scheduler = scheduler ?? ImmediateScheduler.Instance;
scheduler ??= ImmediateScheduler.Instance;

switch (task.Status)
return task.Status switch
{
case TaskStatus.Faulted:
return new Throw<Unit>(task.Exception.InnerException, scheduler);
case TaskStatus.Canceled:
return new Throw<Unit>(new TaskCanceledException(task), scheduler);
}

return new Return<Unit>(Unit.Default, scheduler);
TaskStatus.Faulted => new Throw<Unit>(task.Exception.InnerException, scheduler),
TaskStatus.Canceled => new Throw<Unit>(new TaskCanceledException(task), scheduler),
_ => new Return<Unit>(Unit.Default, scheduler)
};
}

return new SlowTaskObservable(task, scheduler);
Expand Down Expand Up @@ -235,17 +233,14 @@ private static IObservable<TResult> ToObservableImpl<TResult>(Task<TResult> task
{
if (task.IsCompleted)
{
scheduler = scheduler ?? ImmediateScheduler.Instance;
scheduler ??= ImmediateScheduler.Instance;

switch (task.Status)
return task.Status switch
{
case TaskStatus.Faulted:
return new Throw<TResult>(task.Exception.InnerException, scheduler);
case TaskStatus.Canceled:
return new Throw<TResult>(new TaskCanceledException(task), scheduler);
}

return new Return<TResult>(task.Result, scheduler);
TaskStatus.Faulted => new Throw<TResult>(task.Exception.InnerException, scheduler),
TaskStatus.Canceled => new Throw<TResult>(new TaskCanceledException(task), scheduler),
_ => new Return<TResult>(task.Result, scheduler)
};
}

return new SlowTaskObservable<TResult>(task, scheduler);
Expand Down

0 comments on commit 22cbcb1

Please sign in to comment.