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

Unified Diff: lib/result.dart

Issue 1648963002: Add reactive-inspired stream transformers: Base URL: https://github.com/dart-lang/async@master
Patch Set: Created 4 years, 11 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
Index: lib/result.dart
diff --git a/lib/result.dart b/lib/result.dart
index 13e3dd94b288d8e579f3cc8a77eb88430f179356..8e90c5006f51c938290c720ec50cfc62966c4198 100644
--- a/lib/result.dart
+++ b/lib/result.dart
@@ -129,6 +129,19 @@ abstract class Result<T> {
/// Creates a future completed with this result as a value or an error.
Future<T> get asFuture;
+
+ /// This result is equal to another result with the same value or error.
+ ///
+ /// If this result is a value result, it is equal to another value result
+ /// with an equal value.
+ ///
+ /// If this result is an error result, it is equal to another error result
+ /// with equal error object and stack trace.
+ /// Notice that error objecs and stack traces rarely override equality.
nweiz 2016/01/29 22:19:44 "objecs" -> "objects"
Lasse Reichstein Nielsen 2016/02/25 16:18:09 Done.
+ bool operator==(Object other);
+
+ /// A hash code compatible with [operator==].
+ int get hashCode;
}
/// A result representing a returned value.
@@ -146,6 +159,18 @@ class ValueResult<T> implements Result<T> {
sink.add(value);
}
Future<T> get asFuture => new Future.value(value);
+
+ bool operator==(Object other) {
+ if (identical(this, other)) return true;
+ if (other is ValueResult) {
+ return value == other.value;
+ }
+ return false;
+ }
+
+ int get hashCode => value.hashCode;
+
+ String toString() => "Value($value)";
}
/// A result representing a thrown error.
@@ -178,6 +203,19 @@ class ErrorResult implements Result {
errorHandler(error);
}
}
+
+ bool operator==(Object other) {
+ if (identical(this, other)) return true;
+ if (other is ErrorResult) {
+ return error == other.error && stackTrace == other.stackTrace;
+ }
+ return false;
+ }
+
+ int get hashCode =>
+ ((error.hashCode * 31) + stackTrace.hashCode) & 0x3FFFFFFF;
+
+ String toString() => "Error($error${stackTrace == null ? "" : ", ..."})";
}
/// A stream transformer that captures a stream of events into [Result]s.

Powered by Google App Engine
This is Rietveld 408576698