After a lucky guess it looks like the answer is just to implement the IEquatable<T>
interface like demonstrated below. HashSet<T>
(or at least the Mono implementation) then takes an allocation-free approach to its Contains
method by using a different comparer implementation.
public struct TestStruct : IEquatable<TestStruct> {
...
public bool Equals(TestStruct other) {
return a == other.a && b == other.b;
}
}
// No more pain!
if (hashset.Contains(someValue)) { ... }