Repository: thrift Updated Branches: refs/heads/master 1f6e380c5 -> e84c58253
THRIFT-2916 Add default toHash method to 'class' and 'struct' to meet D's associative arrays requirement. Client: D Patch: Phongphan Phuttha <[email protected]> This closes #503 Project: http://git-wip-us.apache.org/repos/asf/thrift/repo Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/e84c5825 Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/e84c5825 Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/e84c5825 Branch: refs/heads/master Commit: e84c582531a78b41f81165816e0c03d9cbbb5b8c Parents: 1f6e380 Author: Jens Geyer <[email protected]> Authored: Mon May 18 22:44:42 2015 +0200 Committer: Jens Geyer <[email protected]> Committed: Tue May 19 01:33:23 2015 +0200 ---------------------------------------------------------------------- lib/d/src/thrift/codegen/base.d | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/thrift/blob/e84c5825/lib/d/src/thrift/codegen/base.d ---------------------------------------------------------------------- diff --git a/lib/d/src/thrift/codegen/base.d b/lib/d/src/thrift/codegen/base.d index 35e566a..9061d0b 100644 --- a/lib/d/src/thrift/codegen/base.d +++ b/lib/d/src/thrift/codegen/base.d @@ -419,6 +419,10 @@ mixin template TStructHelpers(alias fieldMetaData = cast(TFieldMeta[])null) if ( return (cast()super).opEquals(other); } + + size_t toHash() const { + return thriftToHashImpl(); + } } else { string toString() const { return thriftToStringImpl(); @@ -427,6 +431,10 @@ mixin template TStructHelpers(alias fieldMetaData = cast(TFieldMeta[])null) if ( bool opEquals(ref const This other) const { return thriftOpEqualsImpl(other); } + + size_t toHash() const @safe nothrow { + return thriftToHashImpl(); + } } private string thriftToStringImpl() const { @@ -459,6 +467,15 @@ mixin template TStructHelpers(alias fieldMetaData = cast(TFieldMeta[])null) if ( return true; } + private size_t thriftToHashImpl() const @trusted nothrow { + size_t hash = 0; + foreach (name; FieldNames!This) { + auto val = mixin("this." ~ name); + hash += typeid(val).getHash(&val); + } + return hash; + } + static if (any!`!a.defaultValue.empty`(mergeFieldMeta!(This, fieldMetaData))) { static if (is(This _ == class)) { this() { @@ -941,6 +958,29 @@ unittest { static assert(__traits(compiles, { Test t; t.write(cast(TProtocol)null); })); } +// Ensure opEquals and toHash consistency. +unittest { + struct TestEquals { + int a1; + + mixin TStructHelpers!([ + TFieldMeta("a1", 1, TReq.OPT_IN_REQ_OUT), + ]); + } + + TestEquals a, b; + assert(a == b); + assert(a.toHash() == b.toHash()); + + a.a1 = 42; + assert(a != b); + assert(a.toHash() != b.toHash()); + + b.a1 = 42; + assert(a == b); + assert(a.toHash() == b.toHash()); +} + private { /* * Returns a D code string containing the matching TType value for a passed
