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;
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<dyn std::error::Error>> {
let (sender, receiver) = channel(1024);
let tiles: Vec<Arc<dyn Tile>> = vec![
Arc::new(tiles::Load::new(
let tiles: Vec<Box<dyn Tile>> = 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? {}
}

View file

@ -81,5 +81,5 @@ pub struct TileData {
}
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::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<TileData>>,
sender: Sender<TileData>,
instance: Arc<str>,
}
@ -17,17 +16,16 @@ impl Hostname {
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Hostname {
Hostname {
sender_id,
sender: RwLock::new(sender),
sender,
instance,
}
}
async fn send(&self, data: TileData) -> Result<(), SendError<TileData>> {
let mut sender = self.sender.write().await;
sender.send(data).await
async fn send(&mut self, data: TileData) -> Result<(), SendError<TileData>> {
self.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();
File::open("/proc/sys/kernel/hostname")
.await?
@ -50,10 +48,9 @@ impl 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 {
let instance = self;
instance.run().await
self.run().await
})
}
}

View file

@ -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<TileData>>,
sender: Sender<TileData>,
instance: Arc<str>,
}
@ -19,17 +18,16 @@ impl Load {
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Load {
Load {
sender_id,
sender: RwLock::new(sender),
sender,
instance,
}
}
async fn send(&self, data: TileData) -> Result<(), SendError<TileData>> {
let mut sender = self.sender.write().await;
sender.send(data).await
async fn send(&mut self, data: TileData) -> Result<(), SendError<TileData>> {
self.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));
loop {
timer.tick().await;
@ -55,10 +53,9 @@ impl 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 {
let instance = self;
instance.run().await
self.run().await
})
}
}

View file

@ -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<TileData>>,
sender: Sender<TileData>,
instance: Arc<str>,
}
@ -21,14 +20,13 @@ impl Memory {
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Memory {
Memory {
sender_id,
sender: RwLock::new(sender),
sender,
instance,
}
}
async fn send(&self, data: TileData) -> Result<(), SendError<TileData>> {
let mut sender = self.sender.write().await;
sender.send(data).await
async fn send(&mut self, data: TileData) -> Result<(), SendError<TileData>> {
self.sender.send(data).await
}
fn prettify_kib(kib: u64) -> Box<str> {
@ -67,7 +65,7 @@ impl Memory {
.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 raw = [0u8; 256];
loop {
@ -110,10 +108,9 @@ impl 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 {
let instance = self;
let result = instance.run().await;
let result = self.run().await;
eprintln!("Error in Memory: {:?}", result);
result
})

View file

@ -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<TileData>>,
sender: Sender<TileData>,
instance: Arc<str>,
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 {
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<TileData>> {
let mut sender = self.sender.write().await;
sender.send(data).await
async fn send(&mut self, data: TileData) -> Result<(), SendError<TileData>> {
self.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 {
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<dyn std::error::Error + Send + Sync>> {
async fn run(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
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<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 {
let instance = self;
instance.run().await
self.run().await
})
}
}