From 7d0c44bf5f449a4fcdc1f1bd590610e722ba5ad5 Mon Sep 17 00:00:00 2001 From: Skye Jensen Date: Fri, 7 Aug 2020 14:15:40 -0400 Subject: [PATCH] Create TileResult type alias to clean up signatures --- src/config.rs | 7 ++++--- src/tiles/battery.rs | 3 ++- src/tiles/hostname.rs | 5 ++--- src/tiles/load.rs | 4 ++-- src/tiles/memory.rs | 4 ++-- src/tiles/mod.rs | 5 +++++ src/tiles/time.rs | 8 +++----- 7 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/config.rs b/src/config.rs index 74da8a1..ecdb23c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,7 @@ use structopt::StructOpt; use tokio::fs::File; use tokio::prelude::*; use tokio::time::{self, Duration}; +use crate::tiles::TileResult; #[derive(Deserialize, Clone, Debug, Default)] #[serde(default)] @@ -116,7 +117,7 @@ pub async fn read_config() -> Result> { pub fn process_tile( tile: &TileConfig, connection: &Arc, -) -> BoxStream<'static, Result>> { +) -> BoxStream<'static, TileResult> { let five_secs = Duration::from_secs(5); match &tile.config_type { TileConfigType::Battery => wrap(tiles::battery_stream(), tile.update.or(Some(five_secs))), @@ -130,9 +131,9 @@ pub fn process_tile( fn wrap<'a, S>( stream: S, duration: Option, -) -> BoxStream<'a, Result>> +) -> BoxStream<'a, TileResult> where - S: Stream>> + Send + 'a, + S: Stream + Send + 'a, { match duration { Some(duration) => Box::pin(time::throttle(duration, stream)), diff --git a/src/tiles/battery.rs b/src/tiles/battery.rs index 77f21b6..5e78cd2 100644 --- a/src/tiles/battery.rs +++ b/src/tiles/battery.rs @@ -1,3 +1,4 @@ +use super::TileResult; use crate::tile::Block; use futures::{future::try_join3, stream, Stream}; use futures_util::StreamExt; @@ -5,7 +6,7 @@ use std::error::Error; use tokio::fs::File; use tokio::prelude::*; -pub fn battery_stream() -> impl Stream>> { +pub fn battery_stream() -> impl Stream { stream::repeat(()).then(|()| async { let charge_now = async { let mut raw = String::new(); diff --git a/src/tiles/hostname.rs b/src/tiles/hostname.rs index 2258c8d..0f3dcb8 100644 --- a/src/tiles/hostname.rs +++ b/src/tiles/hostname.rs @@ -1,13 +1,12 @@ use crate::tile::Block; +use crate::tiles::TileResult; use dbus::nonblock::stdintf::org_freedesktop_dbus::Properties; use dbus::nonblock::{Proxy, SyncConnection}; use futures::{FutureExt, Stream}; use std::sync::Arc; use std::time::Duration; -pub fn hostname_stream( - connection: Arc, -) -> impl Stream>> { +pub fn hostname_stream(connection: Arc) -> impl Stream { let proxy = Proxy::new( "org.freedesktop.hostname1", "/org/freedesktop/hostname1", diff --git a/src/tiles/load.rs b/src/tiles/load.rs index d6f5016..4f86aca 100644 --- a/src/tiles/load.rs +++ b/src/tiles/load.rs @@ -1,11 +1,11 @@ +use super::TileResult; use crate::tile::Block; use futures::stream::StreamExt; use tokio::fs::File; use tokio::prelude::*; use tokio::stream::Stream; -pub fn load_stream() -> impl Stream>> -{ +pub fn load_stream() -> impl Stream { futures::stream::repeat(()).then(|()| async { let mut raw = String::new(); File::open("/proc/loadavg") diff --git a/src/tiles/memory.rs b/src/tiles/memory.rs index 72dbf04..df407c1 100644 --- a/src/tiles/memory.rs +++ b/src/tiles/memory.rs @@ -1,3 +1,4 @@ +use super::TileResult; use crate::tile::Block; use futures::{stream, Stream, StreamExt}; use std::{io, str, u64}; @@ -40,8 +41,7 @@ fn extract_value(line: &str) -> Result impl Stream>> -{ +pub fn memory_stream() -> impl Stream { stream::repeat(()).then(|_| async { let mut raw = [0u8; 256]; File::open("/proc/meminfo") diff --git a/src/tiles/mod.rs b/src/tiles/mod.rs index 712cd85..f0b1350 100644 --- a/src/tiles/mod.rs +++ b/src/tiles/mod.rs @@ -8,3 +8,8 @@ pub use hostname::hostname_stream; pub use load::load_stream; pub use memory::memory_stream; pub use time::time_stream; + +use crate::tile::Block; +use std::error::Error; + +pub type TileResult = Result>; diff --git a/src/tiles/time.rs b/src/tiles/time.rs index 686aa25..6bdc13d 100644 --- a/src/tiles/time.rs +++ b/src/tiles/time.rs @@ -1,3 +1,4 @@ +use super::TileResult; use crate::config::TimeConfig; use crate::tile::Block; use chrono::prelude::*; @@ -6,14 +7,11 @@ use futures::future::Future; use futures::stream::Stream; use futures_util::ready; use pin_project::pin_project; -use std::error::Error; use std::pin::Pin; use std::task::{Context, Poll}; use tokio::time::{delay_for, delay_until, Delay, Instant}; -pub fn time_stream( - config: TimeConfig, -) -> impl Stream>> { +pub fn time_stream(config: TimeConfig) -> impl Stream { TimeStream { config, delay: delay_until(Instant::now()), @@ -46,7 +44,7 @@ impl TimeStream { } impl Stream for TimeStream { - type Item = Result>; + type Item = TileResult; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { let project = Pin::as_mut(&mut self).project(); ready!(Future::poll(project.delay, cx));