Make InternSet::intern accept Into<Rc<T>>. Add a test. r=nalexander
This commit is contained in:
parent
92cdd72500
commit
8ae8466cf9
1 changed files with 22 additions and 3 deletions
|
@ -34,12 +34,31 @@ impl<T> InternSet<T> where T: Eq + Hash {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Intern a value, providing a ref-counted handle to the interned value.
|
/// Intern a value, providing a ref-counted handle to the interned value.
|
||||||
pub fn intern(&mut self, value: T) -> Rc<T> {
|
///
|
||||||
let key = Rc::new(value);
|
/// ```
|
||||||
|
/// use std::rc::Rc;
|
||||||
|
/// use mentat_core::intern_set::InternSet;
|
||||||
|
///
|
||||||
|
/// let mut s = InternSet::new();
|
||||||
|
///
|
||||||
|
/// let one = "foo".to_string();
|
||||||
|
/// let two = Rc::new("foo".to_string());
|
||||||
|
///
|
||||||
|
/// let out_one = s.intern(one);
|
||||||
|
/// assert_eq!(out_one, two);
|
||||||
|
/// // assert!(!&out_one.ptr_eq(&two)); // Nightly-only.
|
||||||
|
///
|
||||||
|
/// let out_two = s.intern(two);
|
||||||
|
/// assert_eq!(out_one, out_two);
|
||||||
|
/// assert_eq!(1, s.inner.len());
|
||||||
|
/// // assert!(&out_one.ptr_eq(&out_two)); // Nightly-only.
|
||||||
|
/// ```
|
||||||
|
pub fn intern<R: Into<Rc<T>>>(&mut self, value: R) -> Rc<T> {
|
||||||
|
let key: Rc<T> = value.into();
|
||||||
if self.inner.insert(key.clone()) {
|
if self.inner.insert(key.clone()) {
|
||||||
key
|
key
|
||||||
} else {
|
} else {
|
||||||
self.inner.get(&key).unwrap().clone()
|
self.inner.get(&key).unwrap().clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue