From ddc986ea05c5c17758e1028ce0d9f20dc133ec9d Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Wed, 18 Nov 2026 14:12:11 -0700 Subject: add an abstraction called "facilities", which does nothing yet the type system work was a pain, so it's being checked in now Force-Push: yes Change-Id: If7ec15f0108afb030fa2e5a207c77a6cf9828c43 --- src/facilities.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ src/facilities/envsys.rs | 28 ++++++++++++++++++ src/main.rs | 12 ++++++-- src/modules.rs | 16 +++++++++++ src/modules/time.rs | 18 ++++++++---- 5 files changed, 139 insertions(+), 9 deletions(-) create mode 100644 src/facilities.rs create mode 100644 src/facilities/envsys.rs (limited to 'src') diff --git a/src/facilities.rs b/src/facilities.rs new file mode 100644 index 0000000..ab74dd1 --- /dev/null +++ b/src/facilities.rs @@ -0,0 +1,74 @@ +use crate::error::Result; + +use std::any::Any; +use std::borrow::BorrowMut; +use std::collections::BTreeMap; +use std::sync::Arc; +use std::sync::Mutex; + +use bitflags::bitflags; + +pub use envsys::EnvSys; + +pub mod envsys; + + +pub trait Facility: Any { + /* The where clause here is to keep things dyn-compatible. You can see how + * "new" can't be put in a vtable because it has no self parameter. Since dyn + * doesn't have Sized, this is a cute way to say new() won't be called + * through dyn and doesn't need to be in the vtable. + * + * As far as why the compiler can't reach that conclusion on its own, we + * have no specific information. + */ + fn new() -> Result where Self: Sized; + + /* Though it would be tidy to put this in TypeableFacility instead, we + * can't, because we need it as part of performing a downcast, and in the + * FacilityMap accessors we don't know that we have TypeableFacility until + * after the downcast succeeds. + */ + fn as_any_mut(&mut self) -> &mut dyn Any; +} + +/* In order to keep Facility dyn-compatible, the constant is here instead. */ +pub trait TypeableFacility: Facility { + const FACILITY_TYPE: FacilityType; +} + + +bitflags! { + #[derive(PartialEq, Eq, PartialOrd, Ord)] + pub struct FacilityType: u32 { + const ENVSYS = 0x00000001; + } +} + + +/* The wisdom of having this complexity is questionable, but it does work. + * Notice that we don't wrap the individual items in Mutex. That's necessary + * because our accessors need to do the downcast on their callers' behalf, and + * they wouldn't be able to return a reference to inside a lock. Instead, + * callers should wrap the entire FacilityMap in a coarse Mutex. + */ +pub struct FacilityMap { + facilities: BTreeMap>, +} + +impl FacilityMap { + pub fn new() -> FacilityMap { + FacilityMap { facilities: BTreeMap::new() } + } + + pub fn insert(&mut self, facility: F) { + self.facilities.insert(F::FACILITY_TYPE, Box::new(facility)); + } + + pub fn get(&mut self) -> Option<&mut F> { + if let Some(facility) = self.facilities.get_mut(&F::FACILITY_TYPE) { + (*facility).as_mut().as_any_mut().downcast_mut() + } else { None } + } +} + diff --git a/src/facilities/envsys.rs b/src/facilities/envsys.rs new file mode 100644 index 0000000..7b5fccc --- /dev/null +++ b/src/facilities/envsys.rs @@ -0,0 +1,28 @@ +use crate::error::{ Error, Result }; +use crate::facilities::{ Facility, FacilityType, TypeableFacility }; + +use std::any::Any; +use std::sync::Arc; +use std::sync::Mutex; + + +pub struct EnvSys { +} + +impl Facility for EnvSys { + fn new() -> Result + where Self: Sized + { + Ok(EnvSys { + }) + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } +} + +impl TypeableFacility for EnvSys { + const FACILITY_TYPE: FacilityType = FacilityType::ENVSYS; +} + diff --git a/src/main.rs b/src/main.rs index a397217..6292619 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![allow(linker_messages)] use crate::error::*; +use crate::facilities::*; use crate::geometry::*; use crate::modules::*; use crate::x::*; @@ -28,6 +29,7 @@ use xcb::x::{ }; mod error; +mod facilities; mod geometry; mod modules; mod x; @@ -54,6 +56,7 @@ struct Dock { bounds: Arc>, font: Arc, baseline_position: i16, + facilities: FacilityMap, time_module: Arc>, } @@ -106,8 +109,8 @@ async fn async_main(_shutdown_sender: Sender<()>) -> Result<()> { */ redraw_loop(dock, redraw_receiver, sleep_sender).await?; - event_task.cancel(); - sleep_task.cancel(); + event_task.cancel().await; + sleep_task.cancel().await; Ok(()) } @@ -170,7 +173,9 @@ impl Dock { + vertical_padding as i16 + configuration.baseline_offset as i16; - let time_module = TimeModule::new(); + let facilities = FacilityMap::new(); + + let time_module = TimeModule::new()?; /* We start is_visible at false. Since redraw events set it to true, this * won't make us miss any drawing. @@ -181,6 +186,7 @@ impl Dock { bounds: Arc::new(dock_bounds), font, baseline_position, + facilities, time_module, }; diff --git a/src/modules.rs b/src/modules.rs index 0aeda34..e28ac41 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -1,4 +1,20 @@ +use crate::error::Result; +use crate::facilities::FacilityType; + +use std::sync::{ Arc, Mutex }; +use std::time::Duration; + + pub use time::TimeModule; pub mod time; + +pub trait Module { + const REQUIRED_FACILITIES: FacilityType; + + fn new() -> Result>>; + fn format(&mut self) -> String; + fn sleep_duration(&self) -> Option; +} + diff --git a/src/modules/time.rs b/src/modules/time.rs index 25c5864..b7c2634 100644 --- a/src/modules/time.rs +++ b/src/modules/time.rs @@ -1,3 +1,7 @@ +use crate::error::Result; +use crate::facilities::FacilityType; +use crate::modules::Module; + use std::sync::{ Arc, Mutex }; use std::time::Duration; @@ -11,21 +15,23 @@ pub struct TimeModule { } -impl TimeModule { - pub fn new() -> Arc> { +impl Module for TimeModule { + const REQUIRED_FACILITIES: FacilityType = FacilityType::empty(); + + fn new() -> Result>> { let format_string = "[weekday repr:short] [year]-[month]-[day] ".to_owned() + "[hour repr:24]:[minute]:[second]"; let time_format = parse_owned::<3>(&format_string).unwrap(); - Arc::new(Mutex::new(TimeModule { + Ok(Arc::new(Mutex::new(TimeModule { time_format, most_recent_report: None, - })) + }))) } - pub fn format(&mut self) -> String { + fn format(&mut self) -> String { let time = OffsetDateTime::now_local().unwrap(); self.most_recent_report = Some(time); @@ -33,7 +39,7 @@ impl TimeModule { time.format(&self.time_format).unwrap() } - pub fn sleep_duration(&self) -> Option { + fn sleep_duration(&self) -> Option { if let Some(most_recent_report) = self.most_recent_report { let (_, _, _, millis) = most_recent_report.to_hms_milli(); -- cgit 1.4.1