Skip to content

Violating the set invariant with HashSet::get_or_insert_with #399

Open
@JustForFun88

Description

@JustForFun88

The implementation of HashSet::get_or_insert_with is not good. This method make it quite easy to break the invariant of set that all elements are repeated in set only once. Let's say this test passes:

#[test]
fn some_invalid_insert() {
    let mut set = HashSet::new();
    set.insert(1);
    set.get_or_insert_with(&2, |_| 1);
    set.get_or_insert_with(&3, |_| 1);
    assert_eq!(vec![&1, &1, &1], set.iter().collect::<Vec<_>>());
}

May be we need document it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions