From 0bcfe7b468a83436a077824ca60e09c5fdd3f803 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Wed, 18 Nov 2026 14:15:25 -0700 Subject: plurality Force-Push: yes Change-Id: I6e5400ab4ce64a54fe84e5facb1da56fd26c4667 --- src/facilities.rs | 74 ------------------------------------------------ src/facilities/envsys.rs | 28 ------------------ src/facility.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ src/facility/envsys.rs | 28 ++++++++++++++++++ src/main.rs | 8 +++--- src/module.rs | 20 +++++++++++++ src/module/time.rs | 56 ++++++++++++++++++++++++++++++++++++ src/modules.rs | 20 ------------- src/modules/time.rs | 56 ------------------------------------ 9 files changed, 182 insertions(+), 182 deletions(-) delete mode 100644 src/facilities.rs delete mode 100644 src/facilities/envsys.rs create mode 100644 src/facility.rs create mode 100644 src/facility/envsys.rs create mode 100644 src/module.rs create mode 100644 src/module/time.rs delete mode 100644 src/modules.rs delete mode 100644 src/modules/time.rs (limited to 'src') diff --git a/src/facilities.rs b/src/facilities.rs deleted file mode 100644 index ab74dd1..0000000 --- a/src/facilities.rs +++ /dev/null @@ -1,74 +0,0 @@ -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 deleted file mode 100644 index 7b5fccc..0000000 --- a/src/facilities/envsys.rs +++ /dev/null @@ -1,28 +0,0 @@ -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/facility.rs b/src/facility.rs new file mode 100644 index 0000000..ab74dd1 --- /dev/null +++ b/src/facility.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/facility/envsys.rs b/src/facility/envsys.rs new file mode 100644 index 0000000..804f1d9 --- /dev/null +++ b/src/facility/envsys.rs @@ -0,0 +1,28 @@ +use crate::error::{ Error, Result }; +use crate::facility::{ 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 6292619..2d3c1d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ #![allow(linker_messages)] use crate::error::*; -use crate::facilities::*; +use crate::facility::*; use crate::geometry::*; -use crate::modules::*; +use crate::module::*; use crate::x::*; use std::num::NonZero; @@ -29,9 +29,9 @@ use xcb::x::{ }; mod error; -mod facilities; +mod facility; mod geometry; -mod modules; +mod module; mod x; diff --git a/src/module.rs b/src/module.rs new file mode 100644 index 0000000..c859bd3 --- /dev/null +++ b/src/module.rs @@ -0,0 +1,20 @@ +use crate::error::Result; +use crate::facility::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/module/time.rs b/src/module/time.rs new file mode 100644 index 0000000..f334fd0 --- /dev/null +++ b/src/module/time.rs @@ -0,0 +1,56 @@ +use crate::error::Result; +use crate::facility::FacilityType; +use crate::module::Module; + +use std::sync::{ Arc, Mutex }; +use std::time::Duration; + +use time::OffsetDateTime; +use time::format_description::{ FormatDescriptionV3, parse_owned }; + + +pub struct TimeModule { + time_format: FormatDescriptionV3<'static>, + most_recent_report: Option, +} + + +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(); + + Ok(Arc::new(Mutex::new(TimeModule { + time_format, + most_recent_report: None, + }))) + } + + fn format(&mut self) -> String { + let time = OffsetDateTime::now_local().unwrap(); + + self.most_recent_report = Some(time); + + time.format(&self.time_format).unwrap() + } + + fn sleep_duration(&self) -> Option { + if let Some(most_recent_report) = self.most_recent_report { + let (_, _, _, millis) = most_recent_report.to_hms_milli(); + + /* An easy way to check whether this behavior is correct is to open + * several copies of the dock atop each other, and check whether they're + * in sync. + */ + Some(Duration::from_millis(1001 - millis as u64)) + } else { + Some(Duration::from_millis(1000)) + } + } +} + diff --git a/src/modules.rs b/src/modules.rs deleted file mode 100644 index e28ac41..0000000 --- a/src/modules.rs +++ /dev/null @@ -1,20 +0,0 @@ -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 deleted file mode 100644 index b7c2634..0000000 --- a/src/modules/time.rs +++ /dev/null @@ -1,56 +0,0 @@ -use crate::error::Result; -use crate::facilities::FacilityType; -use crate::modules::Module; - -use std::sync::{ Arc, Mutex }; -use std::time::Duration; - -use time::OffsetDateTime; -use time::format_description::{ FormatDescriptionV3, parse_owned }; - - -pub struct TimeModule { - time_format: FormatDescriptionV3<'static>, - most_recent_report: Option, -} - - -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(); - - Ok(Arc::new(Mutex::new(TimeModule { - time_format, - most_recent_report: None, - }))) - } - - fn format(&mut self) -> String { - let time = OffsetDateTime::now_local().unwrap(); - - self.most_recent_report = Some(time); - - time.format(&self.time_format).unwrap() - } - - fn sleep_duration(&self) -> Option { - if let Some(most_recent_report) = self.most_recent_report { - let (_, _, _, millis) = most_recent_report.to_hms_milli(); - - /* An easy way to check whether this behavior is correct is to open - * several copies of the dock atop each other, and check whether they're - * in sync. - */ - Some(Duration::from_millis(1001 - millis as u64)) - } else { - Some(Duration::from_millis(1000)) - } - } -} - -- cgit 1.4.1