diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2012-09-14 15:19:54 -0700 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2012-09-14 15:22:15 -0700 |
| commit | 34cece99cce373546d62aed06a7ec55d04aaa124 (patch) | |
| tree | 561d0767ce8a8254e523c7c198e0adc5d09c606f /src | |
| parent | 6a3756b0ba3dbab0c1957a7c829e09e9505c7574 (diff) | |
| download | rust-34cece99cce373546d62aed06a7ec55d04aaa124.tar.gz rust-34cece99cce373546d62aed06a7ec55d04aaa124.zip | |
replace with_find_ref() with find_ref(), which is just nicer to use
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/send_map.rs | 58 |
1 files changed, 26 insertions, 32 deletions
diff --git a/src/libcore/send_map.rs b/src/libcore/send_map.rs index 49a376bb273..a2808b66b59 100644 --- a/src/libcore/send_map.rs +++ b/src/libcore/send_map.rs @@ -275,40 +275,31 @@ mod linear { } } - /* - FIXME(#3148)--region inference fails to capture needed deps - - fn find_ref(&self, k: &K) -> option<&self/V> { - match self.bucket_for_key(self.buckets, k) { - FoundEntry(idx) => { - match check self.buckets[idx] { - some(ref bkt) => some(&bkt.value) - } - } - TableFull | FoundHole(_) => { - none - } - } - } - */ - - fn with_find_ref<T>(&self, k: &K, blk: fn(Option<&V>) -> T) -> T { + fn find_ref(&self, k: &K) -> Option<&self/V> { match self.bucket_for_key(self.buckets, k) { FoundEntry(idx) => { match self.buckets[idx] { - Some(bkt) => blk(Some(&bkt.value)), - None => fail ~"LinearMap::find: internal logic error" + Some(ref bkt) => { + let ptr = unsafe { + // FIXME(#3148)--region inference + // fails to capture needed deps. + // Here, the bucket value is known to + // live as long as self, because self + // is immutable. But the region + // inference stupidly infers a + // lifetime for `ref bkt` that is + // shorter than it needs to be. + unsafe::copy_lifetime(self, &bkt.value) + }; + Some(ptr) + } + None => { + fail ~"LinearMap::find: internal logic error" + } } } - TableFull | FoundHole(_) => blk(None), - } - } - - fn with_get_ref<T>(&self, k: &K, blk: fn(v: &V) -> T) -> T { - do self.with_find_ref(k) |v| { - match v { - Some(v) => blk(v), - None => fail fmt!("No entry found for key: %?", k), + TableFull | FoundHole(_) => { + None } } } @@ -451,10 +442,13 @@ mod test { } #[test] - fn with_find_ref() { + fn find_ref() { let mut m = ~LinearMap(); - m.with_find_ref(&1, |v| assert v.is_none()); + assert m.find_ref(&1).is_none(); m.insert(1, 2); - m.with_find_ref(&1, |v| assert *v.get() == 2); + match m.find_ref(&1) { + None => fail, + Some(v) => assert *v == 2 + } } } |
