gateway g9i: fix shadow compile errors — lookup returns owned snapshot (borrow across loop mutation), bind temp in test

This commit is contained in:
2026-10-13 15:14:05 +08:00
parent 8279d3d6df
commit ba3318ffbd
@@ -128,14 +128,14 @@ impl PrefixTable {
/// Returns `(depth, entry)` where depth is the number of table probes
/// spent (1 = deepest boundary hit immediately). Hits refresh
/// `last_seen`; expired entries are removed on encounter and count as
/// misses.
pub fn lookup(&mut self, chain: &[u64]) -> Option<(usize, &PrefixEntry)> {
/// misses. The entry is CLONED on return (a few dozen bytes): returning
/// a borrow would pin `self.map` across the loop's mutation (expire-
/// on-encounter), which the borrow checker rejects — and a clone makes
/// the API honest about the entry being a snapshot anyway.
pub fn lookup(&mut self, chain: &[u64]) -> Option<(usize, PrefixEntry)> {
let mut depth = 0usize;
for &key in chain.iter().rev() {
depth += 1;
// Resolve presence/freshness first, then borrow mutably in a
// separate statement — splitting keeps the borrow checker
// happy without NLL edge cases (match-guard borrows).
enum Probe {
Miss,
Expired,
@@ -156,7 +156,8 @@ impl PrefixTable {
Probe::Fresh => {
if let Some(e) = self.map.get_mut(&key) {
e.last_seen = Instant::now();
return Some((depth, e));
let snapshot = e.clone();
return Some((depth, snapshot));
}
}
}
@@ -1107,7 +1108,8 @@ mod tests {
// injected details field is removed from it.
let (usage, forward) = process_sse_chunk(chunk, false, true);
assert!(usage.is_some());
let fwd_str = String::from_utf8_lossy(&forward.unwrap());
let fwd = forward.unwrap();
let fwd_str = String::from_utf8_lossy(&fwd);
assert!(fwd_str.contains("\"prompt_tokens\":42"));
assert!(!fwd_str.contains("cached_tokens_details"));