diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-11-18 14:12:11 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-11-18 14:12:11 -0700 |
| commit | ddc986ea05c5c17758e1028ce0d9f20dc133ec9d (patch) | |
| tree | 417111926032329d71ce8a292c151b04fffa7201 | |
| parent | 01f5b773ab89b809059e2212feb8547f8b073e91 (diff) | |
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
| -rw-r--r-- | Cargo.lock | 2 | ||||
| -rw-r--r-- | Cargo.toml | 8 | ||||
| -rw-r--r-- | src/facilities.rs | 74 | ||||
| -rw-r--r-- | src/facilities/envsys.rs | 28 | ||||
| -rw-r--r-- | src/main.rs | 12 | ||||
| -rw-r--r-- | src/modules.rs | 16 | ||||
| -rw-r--r-- | src/modules/time.rs | 18 |
7 files changed, 149 insertions, 9 deletions
diff --git a/Cargo.lock b/Cargo.lock index ed87140..a0f1f1b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -276,7 +276,9 @@ dependencies = [ "async-channel", "async-executor", "async-io", + "bitflags", "futures-lite", + "rustix", "time", "x11", "xcb", diff --git a/Cargo.toml b/Cargo.toml index 49c7fe1..a9d2188 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,14 @@ futures-lite = "2.0.0" version = "1.5.0" features = [ "static" ] +[dependencies.bitflags] +version = "2.13.2" +features = [ ] + +[dependencies.rustix] +version = "1.1.6" +features = [ ] + [dependencies.time] version = "0.3.55" features = [ "local-offset", "macros", "formatting" ] 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<Self> 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<FacilityType, Box<dyn Facility>>, +} + +impl FacilityMap { + pub fn new() -> FacilityMap { + FacilityMap { facilities: BTreeMap::new() } + } + + pub fn insert<F: Facility + TypeableFacility>(&mut self, facility: F) { + self.facilities.insert(F::FACILITY_TYPE, Box::new(facility)); + } + + pub fn get<F: Facility + TypeableFacility>(&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<Self> + 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<Rectangle<u16>>, font: Arc<Font>, baseline_position: i16, + facilities: FacilityMap, time_module: Arc<Mutex<TimeModule>>, } @@ -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<Arc<Mutex<Self>>>; + fn format(&mut self) -> String; + fn sleep_duration(&self) -> Option<Duration>; +} + 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<Mutex<TimeModule>> { +impl Module for TimeModule { + const REQUIRED_FACILITIES: FacilityType = FacilityType::empty(); + + fn new() -> Result<Arc<Mutex<TimeModule>>> { 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<Duration> { + fn sleep_duration(&self) -> Option<Duration> { if let Some(most_recent_report) = self.most_recent_report { let (_, _, _, millis) = most_recent_report.to_hms_milli(); |