Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 4ad638f

Browse files
authored
Merge pull request #2547 from n8sh/delegateHash
hashOf of delegate: allow CTFE when null plus don't need to use bytesHash merged-on-behalf-of: Nicholas Wilson <[email protected]>
2 parents 4f258a2 + 2c667f7 commit 4ad638f

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/core/internal/hash.d

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,13 +531,16 @@ if (!is(T == enum) && (is(T == struct) || is(T == union))
531531
mixin(_hashOfStruct);
532532
}
533533

534-
//delegate hash. CTFE unsupported
534+
//delegate hash. CTFE only if null.
535535
@trusted @nogc nothrow pure
536536
size_t hashOf(T)(scope const T val, size_t seed = 0) if (!is(T == enum) && is(T == delegate))
537537
{
538-
assert(!__ctfe, "unable to compute hash of "~T.stringof~" at compile time");
539-
const(ubyte)[] bytes = (cast(const(ubyte)*)&val)[0 .. T.sizeof];
540-
return bytesHashWithExactSizeAndAlignment!T(bytes, seed);
538+
if (__ctfe)
539+
{
540+
if (val is null) return hashOf(size_t(0), hashOf(size_t(0), seed));
541+
assert(0, "unable to compute hash of "~T.stringof~" at compile time");
542+
}
543+
return hashOf(val.ptr, hashOf(cast(void*) val.funcptr, seed));
541544
}
542545

543546
//address-based class hash. CTFE only if null.

0 commit comments

Comments
 (0)