diff --git a/mfbt/Tuple.h b/mfbt/Tuple.h index 33342a09ea3c..85ff3f314e15 100644 --- a/mfbt/Tuple.h +++ b/mfbt/Tuple.h @@ -67,6 +67,19 @@ struct CheckConvertibility, Group> sizeof...(SourceTypes) == sizeof...(TargetTypes)> {}; +/* + * Helper type for Tie(args...) to allow ignoring specific elements + * during Tie unpacking. Supports assignment from any type. + * + * Not for direct usage; instead, use mozilla::Ignore in calls to Tie. + */ +struct IgnoreImpl { + template + constexpr const IgnoreImpl& operator=(const T&) const { + return *this; + } +}; + /* * TupleImpl is a helper class used to implement mozilla::Tuple. * It represents one node in a recursive inheritance hierarchy. @@ -463,6 +476,22 @@ inline Tuple...> MakeTuple(Elements&&... aElements) { return Tuple...>(std::forward(aElements)...); } +/** + * A helper placholder to allow ignoring specific elements during Tie unpacking. + * Can be used with any type and any number of elements in a call to Tie. + * + * Usage of Ignore with Tie is equivalent to using std::ignore with + * std::tie. + * + * Example: + * + * int i; + * float f; + * char c; + * Tie(i, Ignore, f, c, Ignore) = FunctionThatReturnsATuple(); + */ +constexpr detail::IgnoreImpl Ignore; + /** * A convenience function for constructing a tuple of references to a * sequence of variables. Since assignments to the elements of the tuple diff --git a/mfbt/tests/TestTuple.cpp b/mfbt/tests/TestTuple.cpp index 735354847d95..6626b0aa7535 100644 --- a/mfbt/tests/TestTuple.cpp +++ b/mfbt/tests/TestTuple.cpp @@ -281,6 +281,19 @@ static bool TestTie() { return true; } +static bool TestTieIgnore() { + int i; + char c; + Tuple rhs1(42, 0.5f, 'c'); + + Tie(i, mozilla::Ignore, c) = rhs1; + + CHECK(i == Get<0>(rhs1)); + CHECK(c == Get<2>(rhs1)); + + return true; +} + int main() { TestConstruction(); TestConstructionFromMozPair(); @@ -291,6 +304,7 @@ int main() { TestGet(); TestMakeTuple(); TestTie(); + TestTieIgnore(); TestTieMozPair(); return 0; }