Skip to content

Commit 34cece9

Browse files
committed
replace with_find_ref() with find_ref(), which is just nicer to use
1 parent 6a3756b commit 34cece9

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

src/libcore/send_map.rs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -275,40 +275,31 @@ mod linear {
275275
}
276276
}
277277

278-
/*
279-
FIXME(#3148)--region inference fails to capture needed deps
280-
281-
fn find_ref(&self, k: &K) -> option<&self/V> {
282-
match self.bucket_for_key(self.buckets, k) {
283-
FoundEntry(idx) => {
284-
match check self.buckets[idx] {
285-
some(ref bkt) => some(&bkt.value)
286-
}
287-
}
288-
TableFull | FoundHole(_) => {
289-
none
290-
}
291-
}
292-
}
293-
*/
294-
295-
fn with_find_ref<T>(&self, k: &K, blk: fn(Option<&V>) -> T) -> T {
278+
fn find_ref(&self, k: &K) -> Option<&self/V> {
296279
match self.bucket_for_key(self.buckets, k) {
297280
FoundEntry(idx) => {
298281
match self.buckets[idx] {
299-
Some(bkt) => blk(Some(&bkt.value)),
300-
None => fail ~"LinearMap::find: internal logic error"
282+
Some(ref bkt) => {
283+
let ptr = unsafe {
284+
// FIXME(#3148)--region inference
285+
// fails to capture needed deps.
286+
// Here, the bucket value is known to
287+
// live as long as self, because self
288+
// is immutable. But the region
289+
// inference stupidly infers a
290+
// lifetime for `ref bkt` that is
291+
// shorter than it needs to be.
292+
unsafe::copy_lifetime(self, &bkt.value)
293+
};
294+
Some(ptr)
295+
}
296+
None => {
297+
fail ~"LinearMap::find: internal logic error"
298+
}
301299
}
302300
}
303-
TableFull | FoundHole(_) => blk(None),
304-
}
305-
}
306-
307-
fn with_get_ref<T>(&self, k: &K, blk: fn(v: &V) -> T) -> T {
308-
do self.with_find_ref(k) |v| {
309-
match v {
310-
Some(v) => blk(v),
311-
None => fail fmt!("No entry found for key: %?", k),
301+
TableFull | FoundHole(_) => {
302+
None
312303
}
313304
}
314305
}
@@ -451,10 +442,13 @@ mod test {
451442
}
452443

453444
#[test]
454-
fn with_find_ref() {
445+
fn find_ref() {
455446
let mut m = ~LinearMap();
456-
m.with_find_ref(&1, |v| assert v.is_none());
447+
assert m.find_ref(&1).is_none();
457448
m.insert(1, 2);
458-
m.with_find_ref(&1, |v| assert *v.get() == 2);
449+
match m.find_ref(&1) {
450+
None => fail,
451+
Some(v) => assert *v == 2
452+
}
459453
}
460454
}

0 commit comments

Comments
 (0)