Summary
Add element-recovery methods to the set types in std.
Motivation
Sets are sometimes used as a cache keyed on a certain property of a type, but programs may need to
access the type's other properties for efficiency or functionality. The sets in std do not expose
their elements (by reference or by value), making this use-case impossible.
Consider the following example:
use HashSet;
use ;
// The `Widget` type has two fields that are inseparable.
;
;
// Widgets are normally considered equal if all their corresponding fields are equal, but we would
// also like to maintain a set of widgets keyed only on their `bar` field. To this end, we create a
// new type with custom `{PartialEq, Hash}` impls.
;
Detailed design
Add the following element-recovery methods to std::collections::{BTreeSet, HashSet}:
Drawbacks
This complicates the collection APIs.
Alternatives
Do nothing.