From bd09432ba96813a14ae8749c0f7d0d44193dc76d Mon Sep 17 00:00:00 2001 From: Skye Jensen Date: Tue, 2 Jun 2020 22:51:24 -0400 Subject: [PATCH] Replace `Arc` with `Box` on the tiles --- src/main.rs | 17 ++++++++--------- src/tile.rs | 2 +- src/tiles/hostname.rs | 17 +++++++---------- src/tiles/load.rs | 17 +++++++---------- src/tiles/memory.rs | 17 +++++++---------- src/tiles/time.rs | 19 ++++++++----------- 6 files changed, 38 insertions(+), 51 deletions(-) diff --git a/src/main.rs b/src/main.rs index 88c7f24..48e5d7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ pub mod tile; pub mod tiles; use dbus_tokio::connection::new_session_sync; -use std::sync::Arc; use tile::Tile; use tokio::sync::mpsc::channel; use uuid::Uuid; @@ -21,33 +20,33 @@ async fn main() -> Result<(), Box> { let (sender, receiver) = channel(1024); - let tiles: Vec> = vec![ - Arc::new(tiles::Load::new( + let tiles: Vec> = vec![ + Box::new(tiles::Load::new( 0, sender.clone(), Uuid::new_v4().to_string().into(), )), - Arc::new(tiles::Memory::new( + Box::new(tiles::Memory::new( 1, sender.clone(), Uuid::new_v4().to_string().into(), )), - Arc::new(tiles::Hostname::new( + Box::new(tiles::Hostname::new( 2, sender.clone(), Uuid::new_v4().to_string().into(), )), - Arc::new(tiles::Time::new( + Box::new(tiles::Time::new( 3, sender, Uuid::new_v4().to_string().into(), )), ]; - for tile in &tiles { - tile.clone().spawn(); + let num_tiles = tiles.len(); + for tile in tiles { + tile.spawn(); } - let num_tiles = tiles.len(); match output::launch(num_tiles, receiver).await? {} } diff --git a/src/tile.rs b/src/tile.rs index ee80be0..864836f 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -81,5 +81,5 @@ pub struct TileData { } pub trait Tile: Send + std::fmt::Debug { - fn spawn(self: Arc) -> JoinHandle>>; + fn spawn(self: Box) -> JoinHandle>>; } diff --git a/src/tiles/hostname.rs b/src/tiles/hostname.rs index 640cae5..55eef21 100644 --- a/src/tiles/hostname.rs +++ b/src/tiles/hostname.rs @@ -3,13 +3,12 @@ use std::sync::Arc; use tokio::fs::File; use tokio::prelude::*; use tokio::sync::mpsc::{error::SendError, Sender}; -use tokio::sync::RwLock; use tokio::task::JoinHandle; #[derive(Debug)] pub struct Hostname { sender_id: usize, - sender: RwLock>, + sender: Sender, instance: Arc, } @@ -17,17 +16,16 @@ impl Hostname { pub fn new(sender_id: usize, sender: Sender, instance: Arc) -> Hostname { Hostname { sender_id, - sender: RwLock::new(sender), + sender, instance, } } - async fn send(&self, data: TileData) -> Result<(), SendError> { - let mut sender = self.sender.write().await; - sender.send(data).await + async fn send(&mut self, data: TileData) -> Result<(), SendError> { + self.sender.send(data).await } - async fn run(&self) -> Result<(), Box> { + async fn run(&mut self) -> Result<(), Box> { let mut raw = String::new(); File::open("/proc/sys/kernel/hostname") .await? @@ -50,10 +48,9 @@ impl Hostname { } impl Tile for Hostname { - fn spawn(self: Arc) -> JoinHandle>> { + fn spawn(mut self: Box) -> JoinHandle>> { tokio::spawn(async move { - let instance = self; - instance.run().await + self.run().await }) } } diff --git a/src/tiles/load.rs b/src/tiles/load.rs index 44b4aa6..a940358 100644 --- a/src/tiles/load.rs +++ b/src/tiles/load.rs @@ -4,14 +4,13 @@ use std::time::Duration; use tokio::fs::File; use tokio::prelude::*; use tokio::sync::mpsc::{error::SendError, Sender}; -use tokio::sync::RwLock; use tokio::task::JoinHandle; use tokio::time::interval; #[derive(Debug)] pub struct Load { sender_id: usize, - sender: RwLock>, + sender: Sender, instance: Arc, } @@ -19,17 +18,16 @@ impl Load { pub fn new(sender_id: usize, sender: Sender, instance: Arc) -> Load { Load { sender_id, - sender: RwLock::new(sender), + sender, instance, } } - async fn send(&self, data: TileData) -> Result<(), SendError> { - let mut sender = self.sender.write().await; - sender.send(data).await + async fn send(&mut self, data: TileData) -> Result<(), SendError> { + self.sender.send(data).await } - async fn run(&self) -> Result<(), Box> { + async fn run(&mut self) -> Result<(), Box> { let mut timer = interval(Duration::from_secs(5)); loop { timer.tick().await; @@ -55,10 +53,9 @@ impl Load { } impl Tile for Load { - fn spawn(self: Arc) -> JoinHandle>> { + fn spawn(mut self: Box) -> JoinHandle>> { tokio::spawn(async move { - let instance = self; - instance.run().await + self.run().await }) } } diff --git a/src/tiles/memory.rs b/src/tiles/memory.rs index 7730ae8..25772e7 100644 --- a/src/tiles/memory.rs +++ b/src/tiles/memory.rs @@ -6,14 +6,13 @@ use std::time::Duration; use tokio::fs::File; use tokio::prelude::*; use tokio::sync::mpsc::{error::SendError, Sender}; -use tokio::sync::RwLock; use tokio::task::JoinHandle; use tokio::time::interval; #[derive(Debug)] pub struct Memory { sender_id: usize, - sender: RwLock>, + sender: Sender, instance: Arc, } @@ -21,14 +20,13 @@ impl Memory { pub fn new(sender_id: usize, sender: Sender, instance: Arc) -> Memory { Memory { sender_id, - sender: RwLock::new(sender), + sender, instance, } } - async fn send(&self, data: TileData) -> Result<(), SendError> { - let mut sender = self.sender.write().await; - sender.send(data).await + async fn send(&mut self, data: TileData) -> Result<(), SendError> { + self.sender.send(data).await } fn prettify_kib(kib: u64) -> Box { @@ -67,7 +65,7 @@ impl Memory { .parse()?) } - async fn run(&self) -> Result<(), Box> { + async fn run(&mut self) -> Result<(), Box> { let mut timer = interval(Duration::from_secs(5)); let mut raw = [0u8; 256]; loop { @@ -110,10 +108,9 @@ impl Memory { } impl Tile for Memory { - fn spawn(self: Arc) -> JoinHandle>> { + fn spawn(mut self: Box) -> JoinHandle>> { tokio::spawn(async move { - let instance = self; - let result = instance.run().await; + let result = self.run().await; eprintln!("Error in Memory: {:?}", result); result }) diff --git a/src/tiles/time.rs b/src/tiles/time.rs index 8c753d7..4cf9c3b 100644 --- a/src/tiles/time.rs +++ b/src/tiles/time.rs @@ -4,14 +4,13 @@ use chrono::DateTime; use std::sync::Arc; use std::time::Duration; use tokio::sync::mpsc::{error::SendError, Sender}; -use tokio::sync::RwLock; use tokio::task::JoinHandle; use tokio::time::delay_for; #[derive(Debug)] pub struct Time { sender_id: usize, - sender: RwLock>, + sender: Sender, instance: Arc, format: Box, short_format: Box, @@ -21,19 +20,18 @@ impl Time { pub fn new(sender_id: usize, sender: Sender, instance: Arc) -> Time { Time { sender_id, - sender: RwLock::new(sender), + sender, instance, format: "%Y-%m-%d %H:%M:%S".into(), short_format: "%H:%M:%S".into(), } } - async fn send(&self, data: TileData) -> Result<(), SendError> { - let mut sender = self.sender.write().await; - sender.send(data).await + async fn send(&mut self, data: TileData) -> Result<(), SendError> { + self.sender.send(data).await } - async fn send_time(&self, time: DateTime) -> Result<(), SendError> { + async fn send_time(&mut self, time: DateTime) -> Result<(), SendError> { let block = Block { full_text: time.format(&self.format).to_string().into(), short_text: Some(time.format(&self.short_format).to_string().into()), @@ -48,7 +46,7 @@ impl Time { self.send(data).await } - async fn run(&self) -> Result<(), Box> { + async fn run(&mut self) -> Result<(), Box> { let mut time = Local::now(); loop { self.send_time(time).await?; @@ -61,10 +59,9 @@ impl Time { } impl Tile for Time { - fn spawn(self: Arc) -> JoinHandle>> { + fn spawn(mut self: Box) -> JoinHandle>> { tokio::spawn(async move { - let instance = self; - instance.run().await + self.run().await }) } }