From d70520eb38c3f39823186c3b352efe4c910417f1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 30 Jun 2019 14:40:01 +0300 Subject: print memory usage for queries --- crates/ra_prof/src/memory_usage.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'crates/ra_prof/src') 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 { } } -#[derive(Default)] +#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)] pub struct Bytes(usize); impl fmt::Display for Bytes { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let bytes = self.0; - if bytes < 4096 { - return write!(f, "{} bytes", bytes); + let mut value = bytes; + let mut suffix = "b"; + if value > 4096 { + value /= 1024; + suffix = "kb"; + if value > 4096 { + value /= 1024; + suffix = "mb"; + } } - let kb = bytes / 1024; - if kb < 4096 { - return write!(f, "{}kb", kb); - } - let mb = kb / 1024; - write!(f, "{}mb", mb) + f.pad(&format!("{}{}", value, suffix)) } } @@ -50,3 +52,10 @@ impl std::ops::AddAssign for Bytes { self.0 += x; } } + +impl std::ops::Sub for Bytes { + type Output = Bytes; + fn sub(self, rhs: Bytes) -> Bytes { + Bytes(self.0 - rhs.0) + } +} -- cgit v1.2.3