1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
use std::time::{Duration, Instant};
use rustc_hash::FxHashMap;
#[derive(Debug)]
pub struct CompletedRequest {
pub id: u64,
pub method: String,
pub duration: Duration,
}
#[derive(Debug)]
pub(crate) struct PendingRequest {
pub(crate) id: u64,
pub(crate) method: String,
pub(crate) received: Instant,
}
impl From<PendingRequest> for CompletedRequest {
fn from(pending: PendingRequest) -> CompletedRequest {
CompletedRequest {
id: pending.id,
method: pending.method,
duration: pending.received.elapsed(),
}
}
}
#[derive(Debug, Default)]
pub(crate) struct PendingRequests {
map: FxHashMap<u64, PendingRequest>,
}
impl PendingRequests {
pub(crate) fn start(&mut self, request: PendingRequest) {
let id = request.id;
let prev = self.map.insert(id, request);
assert!(prev.is_none(), "duplicate request with id {}", id);
}
pub(crate) fn cancel(&mut self, id: u64) -> bool {
self.map.remove(&id).is_some()
}
pub(crate) fn finish(&mut self, id: u64) -> Option<CompletedRequest> {
self.map.remove(&id).map(CompletedRequest::from)
}
}
const N_COMPLETED_REQUESTS: usize = 10;
#[derive(Debug, Default)]
pub struct LatestRequests {
// hand-rolling VecDeque here to print things in a nicer way
buf: [Option<CompletedRequest>; N_COMPLETED_REQUESTS],
idx: usize,
}
impl LatestRequests {
pub(crate) fn record(&mut self, request: CompletedRequest) {
// special case: don't track status request itself
if request.method == "rust-analyzer/analyzerStatus" {
return;
}
let idx = self.idx;
self.buf[idx] = Some(request);
self.idx = (idx + 1) % N_COMPLETED_REQUESTS;
}
pub(crate) fn iter(&self) -> impl Iterator<Item = (bool, &CompletedRequest)> {
let idx = self.idx;
self.buf.iter().enumerate().filter_map(move |(i, req)| Some((i == idx, req.as_ref()?)))
}
}
|