make TrackChannel clone work like it should
use crate::{AbstractChannel, Channel};
use std::io::{Read, Result, Write};
+use std::sync::{Arc, Mutex};
/// A channel for tracking the number of bits read/written.
-pub struct TrackChannel<R, W> {
+pub struct TrackChannel<R, W>(Arc<Mutex<InternalTrackChannel<R,W>>>);
+
+struct InternalTrackChannel<R, W> {
channel: Channel<R, W>,
nbits_read: usize,
nbits_written: usize,
@@ -18,32 +21,35 @@ impl<R: Read, W: Write> TrackChannel<R, W> {
/// Make a new `TrackChannel` from a `reader` and a `writer`.
pub fn new(reader: R, writer: W) -> Self {
let channel = Channel::new(reader, writer);
- Self {
+ let internal = InternalTrackChannel {
channel,
nbits_read: 0,
nbits_written: 0,
- }
+ };
+ Self(Arc::new(Mutex::new(internal)))
}
/// Clear the number of bits read/written.
pub fn clear(&mut self) {
- self.nbits_read = 0;
- self.nbits_written = 0;
+ let mut int = self.0.lock().unwrap();
+ int.nbits_read = 0;
+ int.nbits_written = 0;
}
/// Return the number of kilobits written to the channel.
pub fn kilobits_written(&self) -> f64 {
- self.nbits_written as f64 / 1000.0
+ self.0.lock().unwrap().nbits_written as f64 / 1000.0
}
/// Return the number of kilobits read from the channel.
pub fn kilobits_read(&self) -> f64 {
- self.nbits_read as f64 / 1000.0
+ self.0.lock().unwrap().nbits_read as f64 / 1000.0
}
/// Return the total amount of communication on the channel.
pub fn total_kilobits(&self) -> f64 {
- self.kilobits_written() + self.kilobits_read()
+ let int = self.0.lock().unwrap();
+ (int.nbits_written + int.nbits_read) as f64 / 1000.0
}
/// Return the total amount of communication on the channel as kilobytes.
@@ -53,29 +59,23 @@ impl<R: Read, W: Write> TrackChannel<R, W> {
}
impl<R: Read, W: Write> AbstractChannel for TrackChannel<R, W> {
- #[inline]
fn write_bytes(&mut self, bytes: &[u8]) -> Result<()> {
- self.nbits_written += bytes.len() * 8;
- self.channel.write_bytes(bytes)
+ let mut int = self.0.lock().unwrap();
+ int.nbits_written += bytes.len() * 8;
+ int.channel.write_bytes(bytes)
}
- #[inline]
fn read_bytes(&mut self, mut bytes: &mut [u8]) -> Result<()> {
- self.nbits_read += bytes.len() * 8;
- self.channel.read_bytes(&mut bytes)
+ let mut int = self.0.lock().unwrap();
+ int.nbits_read += bytes.len() * 8;
+ int.channel.read_bytes(&mut bytes)
}
- #[inline]
fn flush(&mut self) -> Result<()> {
- self.channel.flush()
+ self.0.lock().unwrap().channel.flush()
}
- #[inline]
fn clone(&self) -> Self {
- Self {
- channel: self.channel.clone(),
- nbits_written: self.nbits_written,
- nbits_read: self.nbits_read,
- }
+ Self(self.0.clone())
}
}
