aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_prof/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_prof/src')
-rw-r--r--crates/ra_prof/src/memory_usage.rs27
1 files changed, 18 insertions, 9 deletions
diff --git a/crates/ra_prof/src/memory_usage.rs b/crates/ra_prof/src/memory_usage.rs
index 2bde8fb5f..7f857942d 100644
--- a/crates/ra_prof/src/memory_usage.rs
+++ b/crates/ra_prof/src/memory_usage.rs
@@ -27,21 +27,23 @@ impl fmt::Display for MemoryUsage {
27 } 27 }
28} 28}
29 29
30#[derive(Default)] 30#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
31pub struct Bytes(usize); 31pub struct Bytes(usize);
32 32
33impl fmt::Display for Bytes { 33impl fmt::Display for Bytes {
34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35 let bytes = self.0; 35 let bytes = self.0;
36 if bytes < 4096 { 36 let mut value = bytes;
37 return write!(f, "{} bytes", bytes); 37 let mut suffix = "b";
38 if value > 4096 {
39 value /= 1024;
40 suffix = "kb";
41 if value > 4096 {
42 value /= 1024;
43 suffix = "mb";
44 }
38 } 45 }
39 let kb = bytes / 1024; 46 f.pad(&format!("{}{}", value, suffix))
40 if kb < 4096 {
41 return write!(f, "{}kb", kb);
42 }
43 let mb = kb / 1024;
44 write!(f, "{}mb", mb)
45 } 47 }
46} 48}
47 49
@@ -50,3 +52,10 @@ impl std::ops::AddAssign<usize> for Bytes {
50 self.0 += x; 52 self.0 += x;
51 } 53 }
52} 54}
55
56impl std::ops::Sub for Bytes {
57 type Output = Bytes;
58 fn sub(self, rhs: Bytes) -> Bytes {
59 Bytes(self.0 - rhs.0)
60 }
61}