-
Notifications
You must be signed in to change notification settings - Fork 735
/
Cast.cs
44 lines (37 loc) · 1.4 KB
/
Cast.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
namespace System.Reactive.Linq.ObservableImpl
{
internal sealed class Cast<TSource, TResult> : Producer<TResult, Cast<TSource, TResult>._> /* Could optimize further by deriving from Select<TResult> and providing Combine<TResult2>. We're not doing this (yet) for debuggability. */
{
private readonly IObservable<TSource> _source;
public Cast(IObservable<TSource> source)
{
_source = source;
}
protected override _ CreateSink(IObserver<TResult> observer) => new _(observer);
protected override void Run(_ sink) => sink.Run(_source);
internal sealed class _ : Sink<TSource, TResult>
{
public _(IObserver<TResult> observer)
: base(observer)
{
}
public override void OnNext(TSource value)
{
TResult result;
try
{
result = (TResult)(object)value;
}
catch (Exception exception)
{
ForwardOnError(exception);
return;
}
ForwardOnNext(result);
}
}
}
}