From dc78dfe4d69b24fdc7946f3a61a59dc9263cc4a9 Mon Sep 17 00:00:00 2001 From: Skye Jensen Date: Sat, 13 Jun 2020 20:11:48 -0400 Subject: [PATCH] Add debug feature to print latency --- Cargo.toml | 2 ++ src/async_iter.rs | 1 - src/tile.rs | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 19e0c58..fde926f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,8 @@ version = "0.1.0" authors = ["Artemis Tosini "] edition = "2018" +[features] +check_latency = [] [dependencies] async-trait = "0.1" diff --git a/src/async_iter.rs b/src/async_iter.rs index cd11cc8..9aeb3d9 100644 --- a/src/async_iter.rs +++ b/src/async_iter.rs @@ -17,7 +17,6 @@ pub trait AsyncIter { } } - #[cfg(test)] mod test { use super::*; diff --git a/src/tile.rs b/src/tile.rs index d277ab6..1540658 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -2,6 +2,8 @@ use serde::{ser::Serializer, Serialize}; use smart_default::SmartDefault; use std::fmt::Debug; use std::sync::Arc; +#[cfg(feature = "check_latency")] +use std::time::Instant; //use tokio::sync::mpsc::{error::SendError, Sender}; #[allow(unused)] @@ -40,6 +42,16 @@ where serializer.serialize_str(arc) } +#[cfg(feature = "check_latency")] +fn time_diff(&instant: &Instant, serializer: S) -> Result +where + S: Serializer, +{ + let duration = Instant::now() - instant; + //let duration = duration.as_secs_f32(); + serializer.serialize_str(&format!("{:?}", duration)) +} + #[derive(Clone, Serialize, Debug, SmartDefault)] pub struct Block { pub full_text: Box, @@ -75,6 +87,10 @@ pub struct Block { pub separator_block_width: Option, #[serde(skip_serializing_if = "Option::is_none")] pub markup: Option, + #[serde(serialize_with = "time_diff")] + #[default(Instant::now())] + #[cfg(feature = "check_latency")] + pub latency: Instant, } #[derive(Clone, Debug)]