summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--source/tanya/typecons.d38
1 files changed, 38 insertions, 0 deletions
diff --git a/source/tanya/typecons.d b/source/tanya/typecons.d
index a990414..772e979 100644
--- a/source/tanya/typecons.d
+++ b/source/tanya/typecons.d
@@ -338,6 +338,24 @@ struct Option(T)
return this;
}
+ version (D_Ddoc)
+ {
+ /**
+ * If $(D_PARAM T) has a `toHash()` method, $(D_PSYMBOL Option) defines
+ * `toHash()` which returns `T.toHash()` if it is set or 0 otherwise.
+ *
+ * Returns: Hash value.
+ */
+ size_t toHash() const;
+ }
+ else static if (is(typeof(T.init.toHash()) == size_t))
+ {
+ size_t toHash() const
+ {
+ return isNothing ? 0U : this.value.toHash();
+ }
+ }
+
alias get this;
}
@@ -452,3 +470,23 @@ struct Option(T)
assert(((ref e) => e)(Option!int().or(i)) == 5);
}
}
+
+// Implements toHash() for nothing
+@nogc nothrow pure @safe unittest
+{
+ static struct ToHash
+ {
+ size_t toHash() const @nogc nothrow pure @safe
+ {
+ return 1U;
+ }
+ }
+ {
+ Option!ToHash toHash;
+ assert(toHash.toHash() == 0U);
+ }
+ {
+ auto toHash = Option!ToHash(ToHash());
+ assert(toHash.toHash() == 1U);
+ }
+}