Skip to content

Commit 18484c6

Browse files
committed
accept anything that can be converted (using Into) to a key
Convenient because the key type for a table is often a newtype of something else.
1 parent f1caa07 commit 18484c6

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/unify/mod.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,10 @@ impl<K: UnifyKey> UnificationTable<K> {
262262
}
263263

264264
/// Returns an iterator over all keys unioned with `key`.
265-
pub fn unioned_keys(&mut self, key: K) -> UnionedKeys<K> {
265+
pub fn unioned_keys<K1>(&mut self, key: K1) -> UnionedKeys<K>
266+
where K1: Into<K>
267+
{
268+
let key = key.into();
266269
let root_key = self.get_root_key(key);
267270
UnionedKeys {
268271
table: self,
@@ -455,35 +458,45 @@ impl<'tcx, K, V> UnificationTable<K>
455458
/// Unions two keys without the possibility of failure; only
456459
/// applicable when unify values use `NoError` as their error
457460
/// type.
458-
pub fn union(&mut self, a_id: K, b_id: K)
459-
where V: UnifyValue<Error = NoError>
461+
pub fn union<K1,K2>(&mut self, a_id: K1, b_id: K2)
462+
where K1: Into<K>, K2: Into<K>, V: UnifyValue<Error = NoError>,
460463
{
461464
self.unify_var_var(a_id, b_id).unwrap();
462465
}
463466

464467
/// Unions a key and a value without the possibility of failure;
465468
/// only applicable when unify values use `NoError` as their error
466469
/// type.
467-
pub fn union_value(&mut self, id: K, value: V)
468-
where V: UnifyValue<Error = NoError>
470+
pub fn union_value<K1>(&mut self, id: K1, value: V)
471+
where K1: Into<K>, V: UnifyValue<Error = NoError>,
469472
{
470473
self.unify_var_value(id, value).unwrap();
471474
}
472475

473476
/// Given two keys, indicates whether they have been unioned together.
474-
pub fn unioned(&mut self, a_id: K, b_id: K) -> bool {
477+
pub fn unioned<K1,K2>(&mut self, a_id: K1, b_id: K2) -> bool
478+
where K1: Into<K>, K2: Into<K>,
479+
{
475480
self.find(a_id) == self.find(b_id)
476481
}
477482

478483
/// Given a key, returns the (current) root key.
479-
pub fn find(&mut self, id: K) -> K {
484+
pub fn find<K1>(&mut self, id: K1) -> K
485+
where K1: Into<K>
486+
{
487+
let id = id.into();
480488
self.get_root_key(id)
481489
}
482490

483491
/// Unions together two variables, merging their values. If
484492
/// merging the values fails, the error is propagated and this
485493
/// method has no effect.
486-
pub fn unify_var_var(&mut self, a_id: K, b_id: K) -> Result<(), V::Error> {
494+
pub fn unify_var_var<K1,K2>(&mut self, a_id: K1, b_id: K2) -> Result<(), V::Error>
495+
where K1: Into<K>, K2: Into<K>,
496+
{
497+
let a_id = a_id.into();
498+
let b_id = b_id.into();
499+
487500
let root_a = self.get_root_key(a_id);
488501
let root_b = self.get_root_key(b_id);
489502

@@ -498,7 +511,10 @@ impl<'tcx, K, V> UnificationTable<K>
498511

499512
/// Sets the value of the key `a_id` to `b`, attempting to merge
500513
/// with the previous value.
501-
pub fn unify_var_value(&mut self, a_id: K, b: V) -> Result<(), V::Error> {
514+
pub fn unify_var_value<K1>(&mut self, a_id: K1, b: V) -> Result<(), V::Error>
515+
where K1: Into<K>
516+
{
517+
let a_id = a_id.into();
502518
let root_a = self.get_root_key(a_id);
503519
let value = V::unify_values(&self.value(root_a).value, &b)?;
504520
self.update_value(root_a, |node| node.value = value);
@@ -507,7 +523,10 @@ impl<'tcx, K, V> UnificationTable<K>
507523

508524
/// Returns the current value for the given key. If the key has
509525
/// been union'd, this will give the value from the current root.
510-
pub fn probe_value(&mut self, id: K) -> V {
526+
pub fn probe_value<K1>(&mut self, id: K1) -> V
527+
where K1: Into<K>
528+
{
529+
let id = id.into();
511530
let id = self.get_root_key(id);
512531
self.value(id).value.clone()
513532
}

0 commit comments

Comments
 (0)