Replace Arc with Box on the tiles

This commit is contained in:
Skye Jensen 2020-06-02 22:51:24 -04:00
parent 46d0f41f0f
commit bd09432ba9
6 changed files with 38 additions and 51 deletions

View file

@ -3,7 +3,6 @@ pub mod tile;
pub mod tiles; pub mod tiles;
use dbus_tokio::connection::new_session_sync; use dbus_tokio::connection::new_session_sync;
use std::sync::Arc;
use tile::Tile; use tile::Tile;
use tokio::sync::mpsc::channel; use tokio::sync::mpsc::channel;
use uuid::Uuid; use uuid::Uuid;
@ -21,33 +20,33 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (sender, receiver) = channel(1024); let (sender, receiver) = channel(1024);
let tiles: Vec<Arc<dyn Tile>> = vec![ let tiles: Vec<Box<dyn Tile>> = vec![
Arc::new(tiles::Load::new( Box::new(tiles::Load::new(
0, 0,
sender.clone(), sender.clone(),
Uuid::new_v4().to_string().into(), Uuid::new_v4().to_string().into(),
)), )),
Arc::new(tiles::Memory::new( Box::new(tiles::Memory::new(
1, 1,
sender.clone(), sender.clone(),
Uuid::new_v4().to_string().into(), Uuid::new_v4().to_string().into(),
)), )),
Arc::new(tiles::Hostname::new( Box::new(tiles::Hostname::new(
2, 2,
sender.clone(), sender.clone(),
Uuid::new_v4().to_string().into(), Uuid::new_v4().to_string().into(),
)), )),
Arc::new(tiles::Time::new( Box::new(tiles::Time::new(
3, 3,
sender, sender,
Uuid::new_v4().to_string().into(), Uuid::new_v4().to_string().into(),
)), )),
]; ];
for tile in &tiles { let num_tiles = tiles.len();
tile.clone().spawn(); for tile in tiles {
tile.spawn();
} }
let num_tiles = tiles.len();
match output::launch(num_tiles, receiver).await? {} match output::launch(num_tiles, receiver).await? {}
} }

View file

@ -81,5 +81,5 @@ pub struct TileData {
} }
pub trait Tile: Send + std::fmt::Debug { pub trait Tile: Send + std::fmt::Debug {
fn spawn(self: Arc<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>>; fn spawn(self: Box<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>>;
} }

View file

@ -3,13 +3,12 @@ use std::sync::Arc;
use tokio::fs::File; use tokio::fs::File;
use tokio::prelude::*; use tokio::prelude::*;
use tokio::sync::mpsc::{error::SendError, Sender}; use tokio::sync::mpsc::{error::SendError, Sender};
use tokio::sync::RwLock;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
#[derive(Debug)] #[derive(Debug)]
pub struct Hostname { pub struct Hostname {
sender_id: usize, sender_id: usize,
sender: RwLock<Sender<TileData>>, sender: Sender<TileData>,
instance: Arc<str>, instance: Arc<str>,
} }
@ -17,17 +16,16 @@ impl Hostname {
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Hostname { pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Hostname {
Hostname { Hostname {
sender_id, sender_id,
sender: RwLock::new(sender), sender,
instance, instance,
} }
} }
async fn send(&self, data: TileData) -> Result<(), SendError<TileData>> { async fn send(&mut self, data: TileData) -> Result<(), SendError<TileData>> {
let mut sender = self.sender.write().await; self.sender.send(data).await
sender.send(data).await
} }
async fn run(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { async fn run(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let mut raw = String::new(); let mut raw = String::new();
File::open("/proc/sys/kernel/hostname") File::open("/proc/sys/kernel/hostname")
.await? .await?
@ -50,10 +48,9 @@ impl Hostname {
} }
impl Tile for Hostname { impl Tile for Hostname {
fn spawn(self: Arc<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>> { fn spawn(mut self: Box<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>> {
tokio::spawn(async move { tokio::spawn(async move {
let instance = self; self.run().await
instance.run().await
}) })
} }
} }

View file

@ -4,14 +4,13 @@ use std::time::Duration;
use tokio::fs::File; use tokio::fs::File;
use tokio::prelude::*; use tokio::prelude::*;
use tokio::sync::mpsc::{error::SendError, Sender}; use tokio::sync::mpsc::{error::SendError, Sender};
use tokio::sync::RwLock;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tokio::time::interval; use tokio::time::interval;
#[derive(Debug)] #[derive(Debug)]
pub struct Load { pub struct Load {
sender_id: usize, sender_id: usize,
sender: RwLock<Sender<TileData>>, sender: Sender<TileData>,
instance: Arc<str>, instance: Arc<str>,
} }
@ -19,17 +18,16 @@ impl Load {
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Load { pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Load {
Load { Load {
sender_id, sender_id,
sender: RwLock::new(sender), sender,
instance, instance,
} }
} }
async fn send(&self, data: TileData) -> Result<(), SendError<TileData>> { async fn send(&mut self, data: TileData) -> Result<(), SendError<TileData>> {
let mut sender = self.sender.write().await; self.sender.send(data).await
sender.send(data).await
} }
async fn run(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { async fn run(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let mut timer = interval(Duration::from_secs(5)); let mut timer = interval(Duration::from_secs(5));
loop { loop {
timer.tick().await; timer.tick().await;
@ -55,10 +53,9 @@ impl Load {
} }
impl Tile for Load { impl Tile for Load {
fn spawn(self: Arc<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>> { fn spawn(mut self: Box<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>> {
tokio::spawn(async move { tokio::spawn(async move {
let instance = self; self.run().await
instance.run().await
}) })
} }
} }

View file

@ -6,14 +6,13 @@ use std::time::Duration;
use tokio::fs::File; use tokio::fs::File;
use tokio::prelude::*; use tokio::prelude::*;
use tokio::sync::mpsc::{error::SendError, Sender}; use tokio::sync::mpsc::{error::SendError, Sender};
use tokio::sync::RwLock;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tokio::time::interval; use tokio::time::interval;
#[derive(Debug)] #[derive(Debug)]
pub struct Memory { pub struct Memory {
sender_id: usize, sender_id: usize,
sender: RwLock<Sender<TileData>>, sender: Sender<TileData>,
instance: Arc<str>, instance: Arc<str>,
} }
@ -21,14 +20,13 @@ impl Memory {
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Memory { pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Memory {
Memory { Memory {
sender_id, sender_id,
sender: RwLock::new(sender), sender,
instance, instance,
} }
} }
async fn send(&self, data: TileData) -> Result<(), SendError<TileData>> { async fn send(&mut self, data: TileData) -> Result<(), SendError<TileData>> {
let mut sender = self.sender.write().await; self.sender.send(data).await
sender.send(data).await
} }
fn prettify_kib(kib: u64) -> Box<str> { fn prettify_kib(kib: u64) -> Box<str> {
@ -67,7 +65,7 @@ impl Memory {
.parse()?) .parse()?)
} }
async fn run(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { async fn run(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let mut timer = interval(Duration::from_secs(5)); let mut timer = interval(Duration::from_secs(5));
let mut raw = [0u8; 256]; let mut raw = [0u8; 256];
loop { loop {
@ -110,10 +108,9 @@ impl Memory {
} }
impl Tile for Memory { impl Tile for Memory {
fn spawn(self: Arc<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>> { fn spawn(mut self: Box<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>> {
tokio::spawn(async move { tokio::spawn(async move {
let instance = self; let result = self.run().await;
let result = instance.run().await;
eprintln!("Error in Memory: {:?}", result); eprintln!("Error in Memory: {:?}", result);
result result
}) })

View file

@ -4,14 +4,13 @@ use chrono::DateTime;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use tokio::sync::mpsc::{error::SendError, Sender}; use tokio::sync::mpsc::{error::SendError, Sender};
use tokio::sync::RwLock;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tokio::time::delay_for; use tokio::time::delay_for;
#[derive(Debug)] #[derive(Debug)]
pub struct Time { pub struct Time {
sender_id: usize, sender_id: usize,
sender: RwLock<Sender<TileData>>, sender: Sender<TileData>,
instance: Arc<str>, instance: Arc<str>,
format: Box<str>, format: Box<str>,
short_format: Box<str>, short_format: Box<str>,
@ -21,19 +20,18 @@ impl Time {
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Time { pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Time {
Time { Time {
sender_id, sender_id,
sender: RwLock::new(sender), sender,
instance, instance,
format: "%Y-%m-%d %H:%M:%S".into(), format: "%Y-%m-%d %H:%M:%S".into(),
short_format: "%H:%M:%S".into(), short_format: "%H:%M:%S".into(),
} }
} }
async fn send(&self, data: TileData) -> Result<(), SendError<TileData>> { async fn send(&mut self, data: TileData) -> Result<(), SendError<TileData>> {
let mut sender = self.sender.write().await; self.sender.send(data).await
sender.send(data).await
} }
async fn send_time(&self, time: DateTime<Local>) -> Result<(), SendError<TileData>> { async fn send_time(&mut self, time: DateTime<Local>) -> Result<(), SendError<TileData>> {
let block = Block { let block = Block {
full_text: time.format(&self.format).to_string().into(), full_text: time.format(&self.format).to_string().into(),
short_text: Some(time.format(&self.short_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 self.send(data).await
} }
async fn run(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { async fn run(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let mut time = Local::now(); let mut time = Local::now();
loop { loop {
self.send_time(time).await?; self.send_time(time).await?;
@ -61,10 +59,9 @@ impl Time {
} }
impl Tile for Time { impl Tile for Time {
fn spawn(self: Arc<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>> { fn spawn(mut self: Box<Self>) -> JoinHandle<Result<(), Box<dyn std::error::Error + Send + Sync>>> {
tokio::spawn(async move { tokio::spawn(async move {
let instance = self; self.run().await
instance.run().await
}) })
} }
} }