Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document replacement behavior of map and set insertion methods #26888

Closed
apasel422 opened this issue Jul 8, 2015 · 4 comments
Closed

Document replacement behavior of map and set insertion methods #26888

apasel422 opened this issue Jul 8, 2015 · 4 comments

Comments

@apasel422
Copy link
Contributor

The behavior of the following methods is undocumented when the key/item being inserted is equivalent to one already in the collection:

The following program demonstrates that the maps and sets do not replace their keys/items in this scenario:

use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::hash::{Hash, Hasher};

#[derive(Debug)]
struct Foo {
    a: u32,
    b: &'static str,
}

impl PartialEq for Foo {
    fn eq(&self, other: &Self) -> bool { self.a == other.a }
}

impl Eq for Foo {}

impl Hash for Foo {
    fn hash<H: Hasher>(&self, h: &mut H) { self.a.hash(h); }
}

impl PartialOrd for Foo {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.a.partial_cmp(&other.a) }
}

impl Ord for Foo {
    fn cmp(&self, other: &Self) -> Ordering { self.a.cmp(&other.a) }
}

fn main() {
    println!("BTreeMap:");
    let mut map = BTreeMap::new();
    map.insert(Foo { a: 1, b: "baz" }, ());
    map.insert(Foo { a: 1, b: "xyz" }, ());
    for foo in map.keys() { println!("{:?}", foo); }

    println!("HashMap:");
    let mut map = HashMap::new();
    map.insert(Foo { a: 1, b: "baz" }, ());
    map.insert(Foo { a: 1, b: "xyz" }, ());
    for foo in map.keys() { println!("{:?}", foo); }

    println!("BTreeSet:");
    let mut set = BTreeSet::new();
    set.insert(Foo { a: 1, b: "baz" });
    set.insert(Foo { a: 1, b: "xyz" });
    for foo in &set { println!("{:?}", foo); }

    println!("HashSet:");
    let mut set = HashSet::new();
    set.insert(Foo { a: 1, b: "baz" });
    set.insert(Foo { a: 1, b: "xyz" });
    for foo in &set { println!("{:?}", foo); }
}

Output:

BTreeMap:
Foo { a: 1, b: "baz" }
HashMap:
Foo { a: 1, b: "baz" }
BTreeSet:
Foo { a: 1, b: "baz" }
HashSet:
Foo { a: 1, b: "baz" }
@reem
Copy link
Contributor

reem commented Jul 10, 2015

This is... unexpected.

@steveklabnik
Copy link
Member

/cc @rust-lang/libs @gankro

is this behavior intentional and should it be documented?

@Gankra
Copy link
Contributor

Gankra commented Aug 5, 2015

It's intentional. The justification being "our API says you're not allowed to care about keys, and swapping keys is more expensive, so do the less expensive thing".

It's not necessarily the right choice, but at very least it's not an accident. It's unclear if anyone is relying on this (we settled on a uniform behaviour in case anyone did).

@apasel422
Copy link
Contributor Author

I think we should document that they don't replace the key. rust-lang/rfcs#1194 provides an additional replace method that does replace them, and it's most flexible to provide both forms.

steveklabnik added a commit to steveklabnik/rust that referenced this issue Aug 18, 2015
{BTree,Hash}{Map,Set} will not update their key if it already exists, which
can matter with more complex keys. This behavior is now documented.

Fixes rust-lang#26888
steveklabnik added a commit to steveklabnik/rust that referenced this issue Oct 23, 2015
{BTree,Hash}{Map,Set} will not update their key if it already exists, which
can matter with more complex keys. This behavior is now documented.

Fixes rust-lang#26888
bors added a commit that referenced this issue Oct 23, 2015
{BTree,Hash}{Map,Set} will not update their key if it already exists, which
can matter with more complex keys. This behavior is now documented.

Fixes #26888
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants