Skip to content

Commit b1146e7

Browse files
authored
Merge pull request #1357 from dotnet/dev/bartde/rx_nullable_part14
Enable #nullable for push-pull adapters.
2 parents 3eac6e4 + 5753528 commit b1146e7

File tree

5 files changed

+27
-31
lines changed

5 files changed

+27
-31
lines changed

Rx.NET/Source/src/System.Reactive/Linq/Observable/Collect.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT License.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
5+
using System.Diagnostics.CodeAnalysis;
66

77
namespace System.Reactive.Linq.ObservableImpl
88
{
@@ -37,7 +37,7 @@ public _(Func<TResult, TSource, TResult> merge, Func<TResult, TResult> getNewCol
3737
}
3838

3939
private TResult _collector;
40-
private Exception _error;
40+
private Exception? _error;
4141
private bool _hasCompleted;
4242
private bool _done;
4343

@@ -78,15 +78,15 @@ public override void OnCompleted()
7878
}
7979
}
8080

81-
public override bool TryMoveNext(out TResult current)
81+
public override bool TryMoveNext([MaybeNullWhen(false)] out TResult current)
8282
{
8383
lock (_gate)
8484
{
8585
var error = _error;
8686
if (error != null)
8787
{
8888
current = default;
89-
_collector = default;
89+
_collector = default!;
9090
error.Throw();
9191
}
9292
else
@@ -96,7 +96,7 @@ public override bool TryMoveNext(out TResult current)
9696
if (_done)
9797
{
9898
current = default;
99-
_collector = default;
99+
_collector = default!;
100100
return false;
101101
}
102102

Rx.NET/Source/src/System.Reactive/Linq/Observable/Latest.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT License.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
5+
using System.Diagnostics.CodeAnalysis;
76
using System.Threading;
87

98
namespace System.Reactive.Linq.ObservableImpl
@@ -33,8 +32,8 @@ public _()
3332

3433
private bool _notificationAvailable;
3534
private NotificationKind _kind;
36-
private TSource _value;
37-
private Exception _error;
35+
private TSource? _value;
36+
private Exception? _error;
3837

3938
public override void OnNext(TSource value)
4039
{
@@ -90,7 +89,7 @@ public override void OnCompleted()
9089
}
9190
}
9291

93-
public override bool TryMoveNext(out TSource current)
92+
public override bool TryMoveNext([MaybeNullWhen(false)] out TSource current)
9493
{
9594
NotificationKind kind;
9695

@@ -119,10 +118,10 @@ public override bool TryMoveNext(out TSource current)
119118
switch (kind)
120119
{
121120
case NotificationKind.OnNext:
122-
current = value;
121+
current = value!;
123122
return true;
124123
case NotificationKind.OnError:
125-
error.Throw();
124+
error!.Throw();
126125
break;
127126
case NotificationKind.OnCompleted:
128127
break;

Rx.NET/Source/src/System.Reactive/Linq/Observable/MostRecent.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT License.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
5+
using System.Diagnostics.CodeAnalysis;
66

77
namespace System.Reactive.Linq.ObservableImpl
88
{
@@ -31,7 +31,7 @@ public _(TSource initialValue)
3131

3232
private volatile NotificationKind _kind;
3333
private TSource _value;
34-
private Exception _error;
34+
private Exception? _error;
3535

3636
public override void OnNext(TSource value)
3737
{
@@ -54,7 +54,7 @@ public override void OnCompleted()
5454
_kind = NotificationKind.OnCompleted; // Write last!
5555
}
5656

57-
public override bool TryMoveNext(out TSource current)
57+
public override bool TryMoveNext([MaybeNullWhen(false)]out TSource current)
5858
{
5959
//
6060
// Notice the _kind field is marked volatile and read before the other fields.
@@ -68,7 +68,7 @@ public override bool TryMoveNext(out TSource current)
6868
current = _value;
6969
return true;
7070
case NotificationKind.OnError:
71-
_error.Throw();
71+
_error!.Throw();
7272
break;
7373
case NotificationKind.OnCompleted:
7474
break;

Rx.NET/Source/src/System.Reactive/Linq/Observable/Next.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT License.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
5+
using System.Diagnostics.CodeAnalysis;
76
using System.Threading;
87

98
namespace System.Reactive.Linq.ObservableImpl
@@ -15,10 +14,7 @@ public Next(IObservable<TSource> source)
1514
{
1615
}
1716

18-
protected override PushToPullSink<TSource, TSource> Run()
19-
{
20-
return new _();
21-
}
17+
protected override PushToPullSink<TSource, TSource> Run() => new _();
2218

2319
private sealed class _ : PushToPullSink<TSource, TSource>
2420
{
@@ -33,8 +29,8 @@ public _()
3329

3430
private bool _waiting;
3531
private NotificationKind _kind;
36-
private TSource _value;
37-
private Exception _error;
32+
private TSource? _value;
33+
private Exception? _error;
3834

3935
public override void OnNext(TSource value)
4036
{
@@ -92,7 +88,7 @@ public override void OnCompleted()
9288
}
9389
}
9490

95-
public override bool TryMoveNext(out TSource current)
91+
public override bool TryMoveNext([MaybeNullWhen(false)] out TSource current)
9692
{
9793
var done = false;
9894

@@ -127,10 +123,10 @@ public override bool TryMoveNext(out TSource current)
127123
switch (_kind)
128124
{
129125
case NotificationKind.OnNext:
130-
current = _value;
126+
current = _value!;
131127
return true;
132128
case NotificationKind.OnError:
133-
_error.Throw();
129+
_error!.Throw();
134130
break;
135131
case NotificationKind.OnCompleted:
136132
break;

Rx.NET/Source/src/System.Reactive/Linq/Observable/PushToPullAdapter.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT License.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
using System.Collections;
86
using System.Collections.Generic;
7+
using System.Diagnostics.CodeAnalysis;
98
using System.Reactive.Disposables;
109

1110
namespace System.Reactive.Linq.ObservableImpl
@@ -33,13 +32,13 @@ public IEnumerator<TResult> GetEnumerator()
3332

3433
internal abstract class PushToPullSink<TSource, TResult> : IObserver<TSource>, IEnumerator<TResult>
3534
{
36-
private IDisposable _upstream;
35+
private IDisposable? _upstream;
3736

3837
public abstract void OnNext(TSource value);
3938
public abstract void OnError(Exception error);
4039
public abstract void OnCompleted();
4140

42-
public abstract bool TryMoveNext(out TResult current);
41+
public abstract bool TryMoveNext([MaybeNullWhen(false)] out TResult current);
4342

4443
private bool _done;
4544

@@ -60,13 +59,15 @@ public bool MoveNext()
6059
return false;
6160
}
6261

62+
#nullable disable // NB: Matches the protocol around accessing Current only if MoveNext returns true.
6363
public TResult Current
6464
{
6565
get;
6666
private set;
6767
}
6868

6969
object IEnumerator.Current => Current;
70+
#nullable restore
7071

7172
public void Reset()
7273
{

0 commit comments

Comments
 (0)