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

Side by Side Diff: lib/src/result.dart

Issue 1841223002: Fix most strong mode warnings. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: Created 4 years, 8 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import 'dart:async'; 5 import 'dart:async';
6 6
7 import 'result/capture_transformer.dart'; 7 import 'result/capture_transformer.dart';
8 import 'result/error.dart'; 8 import 'result/error.dart';
9 import 'result/release_transformer.dart'; 9 import 'result/release_transformer.dart';
10 import 'result/value.dart'; 10 import 'result/value.dart';
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 /// 65 ///
66 /// Alias for [ValueResult.ValueResult]. 66 /// Alias for [ValueResult.ValueResult].
67 factory Result.value(T value) = ValueResult<T>; 67 factory Result.value(T value) = ValueResult<T>;
68 68
69 /// Create a `Result` holding an error. 69 /// Create a `Result` holding an error.
70 /// 70 ///
71 /// Alias for [ErrorResult.ErrorResult]. 71 /// Alias for [ErrorResult.ErrorResult].
72 factory Result.error(Object error, [StackTrace stackTrace]) => 72 factory Result.error(Object error, [StackTrace stackTrace]) =>
73 new ErrorResult(error, stackTrace); 73 new ErrorResult(error, stackTrace);
74 74
75 // Helper functions.
76 static _captureValue(value) => new ValueResult(value);
77 static _captureError(error, stack) => new ErrorResult(error, stack);
78 static _release(Result v) {
79 if (v.isValue) return v.asValue.value; // Avoid wrapping in future.
80 return v.asFuture;
81 }
82
83 /// Capture the result of a future into a `Result` future. 75 /// Capture the result of a future into a `Result` future.
84 /// 76 ///
85 /// The resulting future will never have an error. 77 /// The resulting future will never have an error.
86 /// Errors have been converted to an [ErrorResult] value. 78 /// Errors have been converted to an [ErrorResult] value.
87 static Future<Result> capture(Future future) { 79 static Future<Result/*<T>*/> capture/*<T>*/(Future/*<T>*/ future) {
88 return future.then(_captureValue, onError: _captureError); 80 return future.then((value) => new ValueResult(value),
Lasse Reichstein Nielsen 2016/03/29 21:53:59 No <T> on the new ValueResult?
nweiz 2016/03/30 00:57:18 It's inferred, but now that you mention it there s
81 onError: (error, stackTrace) => new ErrorResult(error, stackTrace));
89 } 82 }
90 83
91 /// Release the result of a captured future. 84 /// Release the result of a captured future.
92 /// 85 ///
93 /// Converts the [Result] value of the given [future] to a value or error 86 /// Converts the [Result] value of the given [future] to a value or error
94 /// completion of the returned future. 87 /// completion of the returned future.
95 /// 88 ///
96 /// If [future] completes with an error, the returned future completes with 89 /// If [future] completes with an error, the returned future completes with
97 /// the same error. 90 /// the same error.
98 static Future release(Future<Result> future) { 91 static Future/*<T>*/ release/*<T>*/(Future<Result/*<T>*/> future) async =>
99 return future.then(_release); 92 (await future).asFuture;
100 }
101 93
102 /// Capture the results of a stream into a stream of [Result] values. 94 /// Capture the results of a stream into a stream of [Result] values.
103 /// 95 ///
104 /// The returned stream will not have any error events. 96 /// The returned stream will not have any error events.
105 /// Errors from the source stream have been converted to [ErrorResult]s. 97 /// Errors from the source stream have been converted to [ErrorResult]s.
106 /// 98 ///
107 /// Shorthand for transforming the stream using [captureStreamTransformer]. 99 /// Shorthand for transforming the stream using [captureStreamTransformer].
Lasse Reichstein Nielsen 2016/03/29 21:53:59 That is not longer true, so remove this comment li
nweiz 2016/03/30 00:57:18 Done.
108 static Stream<Result> captureStream(Stream source) { 100 static Stream<Result/*<T>*/> captureStream/*<T>*/(Stream/*<T>*/ source) =>
109 return source.transform(captureStreamTransformer); 101 source.transform(new CaptureStreamTransformer<T>());
110 }
111 102
112 /// Release a stream of [result] values into a stream of the results. 103 /// Release a stream of [result] values into a stream of the results.
113 /// 104 ///
114 /// `Result` values of the source stream become value or error events in 105 /// `Result` values of the source stream become value or error events in
115 /// the returned stream as appropriate. 106 /// the returned stream as appropriate.
116 /// Errors from the source stream become errors in the returned stream. 107 /// Errors from the source stream become errors in the returned stream.
117 /// 108 ///
118 /// Shorthand for transforming the stream using [releaseStreamTransformer]. 109 /// Shorthand for transforming the stream using [releaseStreamTransformer].
119 static Stream releaseStream(Stream<Result> source) { 110 static Stream/*<T>*/ releaseStream/*<T>*/(Stream<Result/*<T>*/> source) =>
120 return source.transform(releaseStreamTransformer); 111 source.transform(new ReleaseStreamTransformer<T>());
121 }
122 112
123 /// Converts a result of a result to a single result. 113 /// Converts a result of a result to a single result.
124 /// 114 ///
125 /// If the result is an error, or it is a `Result` value 115 /// If the result is an error, or it is a `Result` value
126 /// which is then an error, then a result with that error is returned. 116 /// which is then an error, then a result with that error is returned.
127 /// Otherwise both levels of results are value results, and a single 117 /// Otherwise both levels of results are value results, and a single
128 /// result with the value is returned. 118 /// result with the value is returned.
129 static Result flatten(Result<Result> result) { 119 static Result/*<T>*/ flatten/*<T>*/(Result<Result/*<T>*/> result) {
130 if (result.isError) return result; 120 if (result.isValue) return result.asValue.value;
131 return result.asValue.value; 121 return new ErrorResult/*<T>*/(
122 result.asError.error, result.asError.stackTrace);
132 } 123 }
133 124
134 /// Whether this result is a value result. 125 /// Whether this result is a value result.
135 /// 126 ///
136 /// Always the opposite of [isError]. 127 /// Always the opposite of [isError].
137 bool get isValue; 128 bool get isValue;
138 129
139 /// Whether this result is an error result. 130 /// Whether this result is an error result.
140 /// 131 ///
141 /// Always the opposite of [isValue]. 132 /// Always the opposite of [isValue].
142 bool get isError; 133 bool get isError;
143 134
144 /// If this is a value result, return itself. 135 /// If this is a value result, return itself.
145 /// 136 ///
146 /// Otherwise return `null`. 137 /// Otherwise return `null`.
147 ValueResult<T> get asValue; 138 ValueResult<T> get asValue;
148 139
149 /// If this is an error result, return itself. 140 /// If this is an error result, return itself.
150 /// 141 ///
151 /// Otherwise return `null`. 142 /// Otherwise return `null`.
152 ErrorResult get asError; 143 ErrorResult<T> get asError;
153 144
154 /// Complete a completer with this result. 145 /// Complete a completer with this result.
155 void complete(Completer<T> completer); 146 void complete(Completer<T> completer);
156 147
157 /// Add this result to an [EventSink]. 148 /// Add this result to an [EventSink].
158 /// 149 ///
159 /// Calls the sink's `add` or `addError` method as appropriate. 150 /// Calls the sink's `add` or `addError` method as appropriate.
160 void addTo(EventSink<T> sink); 151 void addTo(EventSink<T> sink);
161 152
162 /// Creates a future completed with this result as a value or an error. 153 /// Creates a future completed with this result as a value or an error.
163 Future<T> get asFuture; 154 Future<T> get asFuture;
164 } 155 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698