From cc5975f55d6ccb7837a310653d17ef6f97793749 Mon Sep 17 00:00:00 2001 From: threadexio Date: Tue, 7 May 2024 15:59:13 +0300 Subject: [PATCH] refactor: add StatisticsImpl --- channels/src/statistics.rs | 110 +++++++++++++++++++++++++++---------- stress-tests/src/lib.rs | 4 +- 2 files changed, 82 insertions(+), 32 deletions(-) diff --git a/channels/src/statistics.rs b/channels/src/statistics.rs index 3586913..b722638 100644 --- a/channels/src/statistics.rs +++ b/channels/src/statistics.rs @@ -10,20 +10,14 @@ use crate::io::{ mod real { use core::fmt; - /// IO statistic information. - #[derive(Clone, PartialEq, Eq, Hash)] - pub struct Statistics { + #[derive(Clone, Default, PartialEq, Eq, Hash)] + pub(super) struct StatisticsImpl { total_bytes: u64, packets: u64, ops: u64, } - #[allow(dead_code)] - impl Statistics { - pub(crate) const fn new() -> Self { - Self { total_bytes: 0, packets: 0, ops: 0 } - } - + impl StatisticsImpl { #[inline] pub(crate) fn add_total_bytes(&mut self, n: u64) { self.total_bytes += n; @@ -38,33 +32,24 @@ mod real { pub(crate) fn inc_ops(&mut self) { self.ops += 1; } - } - #[allow(dead_code)] - impl Statistics { - /// Returns the number of bytes transferred through this reader/writer. #[inline] - #[must_use] pub fn total_bytes(&self) -> u64 { self.total_bytes } - /// Returns the number of packets transferred through this reader/writer. #[inline] - #[must_use] pub fn packets(&self) -> u64 { self.packets } - /// Returns the total number of `send`/`recv` operations. #[inline] - #[must_use] pub fn ops(&self) -> u64 { self.ops } } - impl fmt::Debug for Statistics { + impl fmt::Debug for StatisticsImpl { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Statistics") .field("total_bytes", &self.total_bytes) @@ -77,25 +62,91 @@ mod real { #[cfg(not(feature = "statistics"))] mod mock { - #[derive(Clone, PartialEq, Eq, Hash)] - pub(crate) struct Statistics; - - impl Statistics { - pub(crate) const fn new() -> Self { - Self - } + #[derive(Clone, Default, PartialEq, Eq, Hash)] + pub(super) struct StatisticsImpl; + impl StatisticsImpl { pub(crate) fn add_total_bytes(&mut self, _: u64) {} pub(crate) fn inc_packets(&mut self) {} pub(crate) fn inc_ops(&mut self) {} + + pub(crate) fn total_bytes(&self) -> u64 { + 0 + } + + pub(crate) fn packets(&self) -> u64 { + 0 + } + + pub(crate) fn ops(&self) -> u64 { + 0 + } } } #[cfg(feature = "statistics")] -pub use self::real::Statistics; +use self::real::StatisticsImpl; #[cfg(not(feature = "statistics"))] -pub use self::mock::Statistics; +use self::mock::StatisticsImpl; + +/// IO statistic information. +#[derive(Clone, PartialEq, Eq, Hash)] +pub struct Statistics { + inner: StatisticsImpl, +} + +impl Statistics { + pub(crate) fn new() -> Self { + Self { inner: StatisticsImpl::default() } + } + + #[inline] + pub(crate) fn add_total_bytes(&mut self, n: u64) { + self.inner.add_total_bytes(n); + } + + #[inline] + pub(crate) fn inc_packets(&mut self) { + self.inner.inc_packets(); + } + + #[inline] + pub(crate) fn inc_ops(&mut self) { + self.inner.inc_ops(); + } + + /// Returns the number of bytes transferred through this reader/writer. + #[inline] + #[must_use] + pub fn total_bytes(&self) -> u64 { + self.inner.total_bytes() + } + + /// Returns the number of packets transferred through this reader/writer. + #[inline] + #[must_use] + pub fn packets(&self) -> u64 { + self.inner.packets() + } + + /// Returns the total number of `send`/`recv` operations. + #[inline] + #[must_use] + pub fn ops(&self) -> u64 { + self.inner.ops() + } +} + +impl fmt::Debug for Statistics { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Statistics") + .field("total_bytes", &self.total_bytes()) + .field("packets", &self.packets()) + .field("ops", &self.ops()) + .finish() + } +} #[derive(Clone, PartialEq, Eq, Hash)] pub struct StatIO { @@ -115,7 +166,6 @@ impl fmt::Debug for StatIO { } } -#[allow(unused_variables, clippy::unused_self)] impl StatIO { #[inline] pub fn new(reader: R) -> Self { @@ -186,7 +236,7 @@ impl Read for StatIO { } } -impl AsyncRead for StatIO { +impl AsyncRead for StatIO { type Error = R::Error; fn poll_read( diff --git a/stress-tests/src/lib.rs b/stress-tests/src/lib.rs index 064a700..37cc258 100644 --- a/stress-tests/src/lib.rs +++ b/stress-tests/src/lib.rs @@ -2,7 +2,7 @@ use std::fmt; use std::thread; use std::time::{Duration, Instant}; -use channels::Statistics; +use channels::StatisticsImpl; pub mod units; @@ -73,7 +73,7 @@ where pub struct TestResults<'a> { pub duration: Duration, - pub stats: &'a Statistics, + pub stats: &'a StatisticsImpl, } impl fmt::Display for TestResults<'_> {