about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/lib/result.rs86
-rw-r--r--src/lib/std.rc1
2 files changed, 87 insertions, 0 deletions
diff --git a/src/lib/result.rs b/src/lib/result.rs
new file mode 100644
index 00000000000..ba5ead5d4db
--- /dev/null
+++ b/src/lib/result.rs
@@ -0,0 +1,86 @@
+/*
+Module: result
+
+A type representing either success or failure
+*/
+
+/* Section: Types */
+
+/*
+Tag: t
+
+The result type
+*/
+tag t<T, U> {
+    /*
+    Variant: ok
+
+    Contains the result value
+    */
+    ok(T);
+    /*
+    Variant: error
+
+    Contains the error value
+    */
+    error(U);
+}
+
+/* Section: Operations */
+
+/*
+Function: get
+
+Get the value out of a successful result
+
+Failure:
+
+If the result is an error
+*/
+fn get<T, U>(res: t<T, U>) -> T {
+    alt res {
+      ok(t) { t }
+      error(_) {
+        fail "get called on error result";
+      }
+    }
+}
+
+/*
+Function: get
+
+Get the value out of an error result
+
+Failure:
+
+If the result is not an error
+*/
+fn get_error<T, U>(res: t<T, U>) -> U {
+    alt res {
+      error(u) { u }
+      ok(_) {
+        fail "get_error called on ok result";
+      }
+    }
+}
+
+/*
+Function: success
+
+Returns true if the result is <ok>
+*/
+fn success<T, U>(res: t<T, U>) -> bool {
+    alt res {
+      ok(_) { true }
+      error(_) { false }
+    }
+}
+
+/*
+Function: failure
+
+Returns true if the result is <error>
+*/
+fn failure<T, U>(res: t<T, U>) -> bool {
+    !success(res)
+}
\ No newline at end of file
diff --git a/src/lib/std.rc b/src/lib/std.rc
index 7f2539fd4c5..fa9a74fc808 100644
--- a/src/lib/std.rc
+++ b/src/lib/std.rc
@@ -97,6 +97,7 @@ mod test;
 mod unsafe;
 mod term;
 mod math;
+mod result;
 
 #[cfg(unicode)]
 mod unicode;