aboutsummaryrefslogtreecommitdiff
path: root/src/result.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/result.hpp')
-rw-r--r--src/result.hpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/result.hpp b/src/result.hpp
new file mode 100644
index 0000000000..6c9f35c0b6
--- /dev/null
+++ b/src/result.hpp
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2018 Andrew Kelley
+ *
+ * This file is part of zig, which is MIT licensed.
+ * See http://opensource.org/licenses/MIT
+ */
+
+#ifndef ZIG_RESULT_HPP
+#define ZIG_RESULT_HPP
+
+#include "error.hpp"
+
+#include <assert.h>
+
+static inline void assertNoError(Error err) {
+ assert(err == ErrorNone);
+}
+
+template<typename T>
+struct Result {
+ T data;
+ Error err;
+
+ Result(T x) : data(x), err(ErrorNone) {}
+
+ Result(Error err) : err(err) {
+ assert(err != ErrorNone);
+ }
+
+ T unwrap() {
+ assert(err == ErrorNone);
+ return data;
+ }
+};
+
+#endif