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

test(borrow): add a test to ensure borrow works #6

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ impl<S: AsRef<str>> Hash for UniCase<S> {
#[cfg(test)]
mod test {
use super::UniCase;
use std::borrow::Cow;
use std::collections::HashMap;
use std::hash::{Hash, Hasher, SipHasher};

fn hash<T: Hash>(t: &T) -> u64 {
Expand All @@ -108,4 +110,23 @@ mod test {
assert_eq!(a, b);
assert_eq!(hash(&a), hash(&b));
}

#[test]
fn test_borrow_impl() {
let owned: Cow<'static, str> = Cow::Owned(String::from("hElLO"));
let static_borrow: Cow<'static, str> = "Hello".into();

let mut map = HashMap::new();
let val = 42;
map.insert(UniCase(owned), val);

assert_eq!(map.get(&UniCase(static_borrow)), Some(&val));

fn get_shorter<'map, 'key>(map: &'map HashMap<UniCase<Cow<'static, str>>, u32>, s: &'key str) -> Option<&'map u32>{
let fn_borrow: Cow<'key, str> = s.into();
map.get::<UniCase<Cow<'key, str>>>(&UniCase(fn_borrow))
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 get works with and without the type-hint on stable (1.4), fails in both cases on beta and nightly.

}

assert_eq!(get_shorter(&map, "hello"), Some(&val))
}
}