diff options
| author | Eugen Wissner <belka@caraus.de> | 2018-09-18 22:27:54 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2018-09-18 22:27:54 +0200 |
| commit | 180c4d3956bf4004299f1fa759513b7c39fc9158 (patch) | |
| tree | a9b1eca4282cde2a39ed23df585935492782d978 | |
| parent | b0dc7b59e570849e55894ae99a9d162247ccc329 (diff) | |
| download | tanya-180c4d3956bf4004299f1fa759513b7c39fc9158.tar.gz | |
typecons.Option: Implement toHash forwarder
| -rw-r--r-- | source/tanya/typecons.d | 38 |
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); + } +} |
