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

make {Index, IndexMut} take their keys by value #13

Merged
merged 2 commits into from
Mar 26, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
//! # Examples
//!
//! ```
//! # extern crate "linked-hash-map" as linked_hash_map;
//! # extern crate linked_hash_map;
//! # fn main() {
//! use linked_hash_map::LinkedHashMap;
//!
//! let mut map = LinkedHashMap::new();
//! map.insert(2, 20);
//! map.insert(1, 10);
//! map.insert(3, 30);
//! assert_eq!(map[1], 10);
//! assert_eq!(map[2], 20);
//! assert_eq!(map[3], 30);
//! assert_eq!(map[&1], 10);
//! assert_eq!(map[&2], 20);
//! assert_eq!(map[&3], 30);
//!
//! let items: Vec<(i32, i32)> = map.iter().map(|t| (*t.0, *t.1)).collect();
//! assert_eq!(vec![(2, 20), (1, 10), (3, 30)], items);
Expand Down Expand Up @@ -161,15 +161,15 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
/// # Examples
///
/// ```
/// # extern crate "linked-hash-map" as linked_hash_map;
/// # extern crate linked_hash_map;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the externs necessary anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alas, yes. I'm filing a bug now.

/// # fn main() {
/// use linked_hash_map::LinkedHashMap;
/// let mut map = LinkedHashMap::new();
///
/// map.insert(1, "a");
/// map.insert(2, "b");
/// assert_eq!(map[1], "a");
/// assert_eq!(map[2], "b");
/// assert_eq!(map[&1], "a");
/// assert_eq!(map[&2], "b");
/// # }
/// ```
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
Expand Down Expand Up @@ -210,7 +210,7 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
/// # Examples
///
/// ```
/// # extern crate "linked-hash-map" as linked_hash_map;
/// # extern crate linked_hash_map;
/// # fn main() {
/// use linked_hash_map::LinkedHashMap;
/// let mut map = LinkedHashMap::new();
Expand All @@ -233,7 +233,7 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
/// # Examples
///
/// ```
/// # extern crate "linked-hash-map" as linked_hash_map;
/// # extern crate linked_hash_map;
/// # fn main() {
/// use linked_hash_map::LinkedHashMap;
/// let mut map = LinkedHashMap::new();
Expand All @@ -257,7 +257,7 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
/// # Examples
///
/// ```
/// # extern crate "linked-hash-map" as linked_hash_map;
/// # extern crate linked_hash_map;
/// # fn main() {
/// use linked_hash_map::LinkedHashMap;
/// let mut map = LinkedHashMap::new();
Expand Down Expand Up @@ -294,7 +294,7 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
/// # Examples
///
/// ```
/// # extern crate "linked-hash-map" as linked_hash_map;
/// # extern crate linked_hash_map;
/// # fn main() {
/// use linked_hash_map::LinkedHashMap;
/// let mut map = LinkedHashMap::new();
Expand All @@ -321,7 +321,7 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
/// # Examples
///
/// ```
/// # extern crate "linked-hash-map" as linked_hash_map;
/// # extern crate linked_hash_map;
/// # fn main() {
/// use linked_hash_map::LinkedHashMap;
/// let mut map: LinkedHashMap<i32, &str> = LinkedHashMap::new();
Expand All @@ -339,7 +339,7 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
/// # Examples
///
/// ```
/// # extern crate "linked-hash-map" as linked_hash_map;
/// # extern crate linked_hash_map;
/// # fn main() {
/// use linked_hash_map::LinkedHashMap;
/// let mut map = LinkedHashMap::new();
Expand Down Expand Up @@ -379,7 +379,7 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
///
/// # Examples
/// ```
/// # extern crate "linked-hash-map" as linked_hash_map;
/// # extern crate linked_hash_map;
/// # fn main() {
/// use linked_hash_map::LinkedHashMap;
///
Expand Down Expand Up @@ -408,7 +408,7 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
/// Iterator element type is `(&'a K, &'a mut V)`
/// # Examples
/// ```
/// # extern crate "linked-hash-map" as linked_hash_map;
/// # extern crate linked_hash_map;
/// # fn main() {
/// use linked_hash_map::LinkedHashMap;
///
Expand Down Expand Up @@ -440,7 +440,7 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
///
/// # Examples
/// ```
/// # extern crate "linked-hash-map" as linked_hash_map;
/// # extern crate linked_hash_map;
/// # fn main() {
/// use linked_hash_map::LinkedHashMap;
///
Expand All @@ -467,7 +467,7 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
///
/// # Examples
/// ```
/// # extern crate "linked-hash-map" as linked_hash_map;
/// # extern crate linked_hash_map;
/// # fn main() {
/// use linked_hash_map::LinkedHashMap;
///
Expand All @@ -491,20 +491,20 @@ impl<K: Hash + Eq, V, S: HashState> LinkedHashMap<K, V, S> {
}
}

impl<K, V, S, Q: ?Sized> Index<Q> for LinkedHashMap<K, V, S>
impl<'a, K, V, S, Q: ?Sized> Index<&'a Q> for LinkedHashMap<K, V, S>
where K: Hash + Eq + Borrow<Q>, S: HashState, Q: Eq + Hash
{
type Output = V;

fn index(&self, index: &Q) -> &V {
fn index(&self, index: &'a Q) -> &V {
self.get(index).expect("no entry found for key")
}
}

impl<K, V, S, Q: ?Sized> IndexMut<Q> for LinkedHashMap<K, V, S>
impl<'a, K, V, S, Q: ?Sized> IndexMut<&'a Q> for LinkedHashMap<K, V, S>
where K: Hash + Eq + Borrow<Q>, S: HashState, Q: Eq + Hash
{
fn index_mut(&mut self, index: &Q) -> &mut V {
fn index_mut(&mut self, index: &'a Q) -> &mut V {
self.get_mut(index).expect("no entry found for key")
}
}
Expand Down Expand Up @@ -789,9 +789,9 @@ mod tests {
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
assert_eq!(10, map[1]);
map[2] = 22;
assert_eq!(22, map[2]);
assert_eq!(10, map[&1]);
map[&2] = 22;
assert_eq!(22, map[&2]);
}

#[test]
Expand Down Expand Up @@ -913,8 +913,8 @@ mod tests {
assert_eq!(None, iter.next());
}

assert_eq!(17, map["a"]);
assert_eq!(23, map["b"]);
assert_eq!(17, map[&"a"]);
assert_eq!(23, map[&"b"]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aw, that sucks. &str maps are probably exceptional anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually the result of another bug: rust-lang/rust#23649. We should be able to go back to map["a"] once it's fixed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really? It makes sense to me. It's a Map<&str, i32>. Seems to follow the &&str is necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Index and get use Borrow, and &str: Borrow<str>. (For what it's worth, the crate type-checks with just map["a"], but that bug prevents it from being built.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I might be thinking back to BorrowFrom. Cool that that works!

}

#[test]
Expand Down Expand Up @@ -950,10 +950,10 @@ mod tests {
assert_eq!(map.get_mut(&Foo(Bar(1))), Some(&mut "a"));
assert_eq!(map.get_mut(&Foo(Bar(2))), Some(&mut "b"));

assert_eq!(map[Bar(1)], "a");
assert_eq!(map[Bar(2)], "b");
assert_eq!(map[Foo(Bar(1))], "a");
assert_eq!(map[Foo(Bar(2))], "b");
assert_eq!(map[&Bar(1)], "a");
assert_eq!(map[&Bar(2)], "b");
assert_eq!(map[&Foo(Bar(1))], "a");
assert_eq!(map[&Foo(Bar(2))], "b");

assert_eq!(map.remove(&Bar(1)), Some("a"));
assert_eq!(map.remove(&Bar(2)), Some("b"));
Expand Down