Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Unified Diff: lib/src/stream_channel_transformer.dart

Issue 1966853003: Add type-coercion functions. (Closed) Base URL: git@github.com:dart-lang/stream_channel.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | lib/src/transformer/typed.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/stream_channel_transformer.dart
diff --git a/lib/src/stream_channel_transformer.dart b/lib/src/stream_channel_transformer.dart
index ac980859daa2f48564a2ebc391cedd3537c3ddd8..ca09ea16bec34d6fb00dc0cd29f72848a7bb04ce 100644
--- a/lib/src/stream_channel_transformer.dart
+++ b/lib/src/stream_channel_transformer.dart
@@ -8,6 +8,7 @@ import 'dart:convert';
import 'package:async/async.dart';
import '../stream_channel.dart';
+import 'transformer/typed.dart';
/// A [StreamChannelTransformer] transforms the events being passed to and
/// emitted by a [StreamChannel].
@@ -24,6 +25,21 @@ class StreamChannelTransformer<S, T> {
/// The transformer to use on the channel's sink.
final StreamSinkTransformer<S, T> _sinkTransformer;
+ /// Creates a wrapper that coerces the type of [transformer].
+ ///
+ /// This soundly converts a [StreamChannelTransformer] to a
+ /// `StreamChannelTransformer<S, T>`, regardless of its original generic type,
+ /// by asserting that the events emitted by the transformed channel's stream
+ /// are instances of `T` whenever they're provided. If they're not, the stream
+ /// throws a [CastError]. This also means that calls to [StreamSink.add] on
+ /// the transformed channel's sink may throw a [CastError] if the argument
+ /// type doesn't match the reified type of the sink.
+ static StreamChannelTransformer/*<S, T>*/ typed/*<S, T>*/(
+ StreamChannelTransformer transformer) =>
+ transformer is StreamChannelTransformer/*<S, T>*/
+ ? transformer
+ : new TypeSafeStreamChannelTransformer(transformer);
+
/// Creates a [StreamChannelTransformer] from existing stream and sink
/// transformers.
const StreamChannelTransformer(
« no previous file with comments | « CHANGELOG.md ('k') | lib/src/transformer/typed.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698