Skip to content

Commit

Permalink
Merge pull request rust-lang#10 from Dushistov/master
Browse files Browse the repository at this point in the history
implement Borrow<str> to make possible search str in HashMap<SmolStr
  • Loading branch information
jD91mZM2 authored Mar 10, 2019
2 parents 65484e0 + ce98026 commit eacd72d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt, hash, ops::Deref, sync::Arc};
use std::{borrow::Borrow, fmt, hash, ops::Deref, sync::Arc};

/// A `SmolStr` is a string type that has the following properties:
///
Expand Down Expand Up @@ -148,6 +148,12 @@ impl From<SmolStr> for String {
}
}

impl Borrow<str> for SmolStr {
fn borrow(&self) -> &str {
self.as_str()
}
}

const INLINE_CAP: usize = 22;
const N_NEWLINES: usize = 32;
const N_SPACES: usize = 128;
Expand Down
7 changes: 7 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@ fn test_serde() {
let s: SmolStr = serde_json::from_str(&s).unwrap();
assert_eq!(s, "Hello, World");
}

#[test]
fn test_search_in_hashmap() {
let mut m = ::std::collections::HashMap::<SmolStr, i32>::new();
m.insert("aaa".into(), 17);
assert_eq!(17, *m.get("aaa").unwrap());
}

0 comments on commit eacd72d

Please sign in to comment.