Skip to content

Commit c66483e

Browse files
author
Oren (electricessence)
committed
More flexible and useful type inferrence.
1 parent dbdc904 commit c66483e

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

Open.ChannelExtensions/Extensions.Transform.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,28 @@ public override ValueTask<bool> WaitToReadAsync(CancellationToken cancellationTo
5050
/// <returns>A channel reader representing the tranformed results.</returns>
5151
public static ChannelReader<TResult> Transform<T, TResult>(this ChannelReader<T> source, Func<T, TResult> transform)
5252
=> new TransformingChannelReader<T, TResult>(source, transform);
53+
54+
/// <summary>
55+
/// Transforms the
56+
/// </summary>
57+
/// <typeparam name="TWrite">Specifies the type of data that may be written to the channel.</typeparam>
58+
/// <typeparam name="TRead">Specifies the type of data read from the source channel.</typeparam>
59+
/// <typeparam name="TResult">Specifies the type of data that may be read from the channel.</typeparam>
60+
/// <param name="source">The source channel reader.</param>
61+
/// <param name="transform">The transform function.</param>
62+
/// <returns>A channel reader representing the tranformed results.</returns>
63+
public static TransformChannel<TWrite, TRead, TResult> Transform<TWrite, TRead, TResult>(this Channel<TWrite, TRead> source, Func<TRead, TResult> transform)
64+
=> new TransformChannel<TWrite, TRead, TResult>(source, transform);
65+
66+
/// <summary>
67+
/// Transforms the
68+
/// </summary>
69+
/// <typeparam name="T">Specifies the type of data that may be written to the channel.</typeparam>
70+
/// <typeparam name="TResult">Specifies the type of data that may be read from the channel.</typeparam>
71+
/// <param name="source">The source channel reader.</param>
72+
/// <param name="transform">The transform function.</param>
73+
/// <returns>A channel reader representing the tranformed results.</returns>
74+
public static TransformChannel<T, TResult> Transform<T, TResult>(this Channel<T> source, Func<T, TResult> transform)
75+
=> new TransformChannel<T, TResult>(source, transform);
5376
}
5477
}

Open.ChannelExtensions/TransformChannel.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ namespace Open.ChannelExtensions
88
/// A channel wrapper that takes the provided channel and transforms them on demand when being read.
99
/// </summary>
1010
/// <typeparam name="TWrite">Specifies the type of data that may be written to the channel.</typeparam>
11-
/// <typeparam name="TRead">Specifies the type of data that may be read from the channel.</typeparam>
12-
public class TransformChannel<TWrite, TRead> : Channel<TWrite, TRead>
11+
/// <typeparam name="TRead">Specifies the type of data read from the source channel.</typeparam>
12+
/// <typeparam name="TResult">Specifies the type of data that may be read from the channel.</typeparam>
13+
public class TransformChannel<TWrite, TRead, TResult> : Channel<TWrite, TResult>
1314
{
1415
/// <summary>
1516
/// Creates a channel wrapper that takes the provided channel and transforms them on demand when being read.
1617
/// </summary>
1718
/// <param name="source">The channel containing the source data.</param>
1819
/// <param name="transform">The transform function to be applied to the results when being read.</param>
19-
public TransformChannel(Channel<TWrite, TWrite> source, Func<TWrite, TRead> transform)
20+
public TransformChannel(Channel<TWrite, TRead> source, Func<TRead, TResult> transform)
2021
{
2122
if (source is null) throw new ArgumentNullException(nameof(source));
2223
if (transform is null) throw new ArgumentNullException(nameof(transform));
@@ -26,4 +27,22 @@ public TransformChannel(Channel<TWrite, TWrite> source, Func<TWrite, TRead> tran
2627
Reader = source.Reader.Transform(transform);
2728
}
2829
}
30+
31+
/// <summary>
32+
/// A channel wrapper that takes the provided channel and transforms them on demand when being read.
33+
/// </summary>
34+
/// <typeparam name="T">Specifies the type of data that may be written to the channel.</typeparam>
35+
/// <typeparam name="TResult">Specifies the type of data that may be read from the channel.</typeparam>
36+
public class TransformChannel<T, TResult> : TransformChannel<T, T, TResult>
37+
{
38+
/// <summary>
39+
/// Creates a channel wrapper that takes the provided channel and transforms them on demand when being read.
40+
/// </summary>
41+
/// <param name="source">The channel containing the source data.</param>
42+
/// <param name="transform">The transform function to be applied to the results when being read.</param>
43+
public TransformChannel(Channel<T, T> source, Func<T, TResult> transform)
44+
:base(source, transform)
45+
{
46+
}
47+
}
2948
}

0 commit comments

Comments
 (0)