typecons.Option: Implement toHash forwarder

This commit is contained in:
Eugen Wissner 2018-09-18 22:27:54 +02:00
parent b0dc7b59e5
commit 180c4d3956

View File

@ -338,6 +338,24 @@ struct Option(T)
return this; 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; alias get this;
} }
@ -452,3 +470,23 @@ struct Option(T)
assert(((ref e) => e)(Option!int().or(i)) == 5); 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);
}
}