about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/facility.rs78
-rw-r--r--src/facility/envsys.rs23
-rw-r--r--src/main.rs10
-rw-r--r--src/module.rs3
-rw-r--r--src/module/battery.rs30
5 files changed, 108 insertions, 36 deletions
diff --git a/src/facility.rs b/src/facility.rs
index ab74dd1..824a864 100644
--- a/src/facility.rs
+++ b/src/facility.rs
@@ -1,10 +1,7 @@
 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;
 
@@ -14,37 +11,71 @@ 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
+  /*   Though it would be tidy to put this in CreatableFacility 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
+   * FacilityMap accessors we don't know that we have CreatableFacility until
    * after the downcast succeeds.
+   *
+   *   As of rustc 1.98.1 in edition 2024, the compiler generates a
+   * warning that this is unused, but it is used. Good fun.
    */
+  #[allow(dead_code)]
   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 {
+/*   In order to keep Facility dyn-compatible, we put everything that isn't in
+ * a separate trait. It turns out, after some contemplation, that there is a
+ * unifying principle behind what we need that breaks compatibility, and it's
+ * stuff concerned with creating instances in the first place. So, that's why
+ * we call it Creatable.
+ */
+pub trait CreatableFacility: Facility + Sized {
+  /*   Someday, associated constants will be dyn-compatible, but today they are
+   * not. Ah, Rust, where all the obvious features are directions for future
+   * research.
+   *
+   *   Also, as of rustc 1.98.1 in edition 2024, the compiler generates a
+   * warning that this is unused, but it is used. Good fun.
+   */
+  #[allow(dead_code)]
   const FACILITY_TYPE: FacilityType;
+
+  /*   You can see how "new" can't be put in a vtable because it has no self
+   * parameter. We could have it in the other trait if we wanted, by giving it
+   * a Sized constriant, but it feels like better organization to have it
+   * here.
+   *
+   *   As far as why the compiler can't reach that conclusion on its own and
+   * skip it, we have no specific information.
+   */
+  fn new() -> Result<Self>;
+
+  /* The only way to coerce to dyn is to return as it! */
+  fn as_box<'a>(self) -> Box<dyn Facility + 'a>
+    where Self: 'a
+  {
+    Box::new(self)
+  }
 }
 
 
+
 bitflags! {
-  #[derive(PartialEq, Eq, PartialOrd, Ord)]
+  #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
   pub struct FacilityType: u32 {
     const ENVSYS = 0x00000001;
   }
 }
 
+impl FacilityType {
+  pub fn new<'a>(&self) -> Result<Box<dyn Facility + 'a>> {
+    match self {
+      &FacilityType::ENVSYS => Ok(EnvSys::new()?.as_box()),
+      _ => todo!()
+    }
+  }
+}
+
 
 /*   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
@@ -61,11 +92,16 @@ impl FacilityMap {
     FacilityMap { facilities: BTreeMap::new() }
   }
 
-  pub fn insert<F: Facility + TypeableFacility>(&mut self, facility: F) {
-    self.facilities.insert(F::FACILITY_TYPE, Box::new(facility));
+  /*   It would be nice if we could infer the facility type, but the facility
+   * is already boxed at this point, so we can't use non-dyn stuff.
+   */
+  pub fn insert(&mut self, facility_type: FacilityType,
+                facility: Box<dyn Facility>)
+  {
+    self.facilities.insert(facility_type, facility);
   }
 
-  pub fn get<F: Facility + TypeableFacility>(&mut self) -> Option<&mut F> {
+  pub fn get<F: CreatableFacility>(&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
index 804f1d9..b2f632c 100644
--- a/src/facility/envsys.rs
+++ b/src/facility/envsys.rs
@@ -1,28 +1,27 @@
-use crate::error::{ Error, Result };
-use crate::facility::{ Facility, FacilityType, TypeableFacility };
+use crate::error::Result;
+use crate::facility::{ CreatableFacility, Facility, FacilityType };
 
 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 {
+impl CreatableFacility for EnvSys {
   const FACILITY_TYPE: FacilityType = FacilityType::ENVSYS;
+
+  fn new() -> Result<Self> {
+    println!("loading envsys");
+
+    Ok(EnvSys {
+    })
+  }
+
 }
 
diff --git a/src/main.rs b/src/main.rs
index 38a993f..cffb6bb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,7 +5,6 @@ use crate::geometry::*;
 use crate::module::*;
 use crate::x::*;
 
-use std::num::NonZero;
 use std::process::ExitCode;
 use std::sync::Arc;
 use std::sync::Mutex;
@@ -177,7 +176,14 @@ impl Dock {
                             + vertical_padding as i16
                             + configuration.baseline_offset as i16;
 
-    let facilities = FacilityMap::new();
+    let mut required_facilities = FacilityType::empty();
+    required_facilities |= BatteryModule::REQUIRED_FACILITIES;
+    required_facilities |= TimeModule::REQUIRED_FACILITIES;
+
+    let mut facilities = FacilityMap::new();
+    for facility_type in required_facilities.iter() {
+      facilities.insert(facility_type.clone(), facility_type.new()?);
+    }
 
     let mut modules = Vec::new();
     modules.push((Arc::new(Mutex::new(TimeModule::new()?.as_box())),
diff --git a/src/module.rs b/src/module.rs
index d10e436..9d4baa0 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -1,12 +1,13 @@
 use crate::error::Result;
 use crate::facility::FacilityType;
 
-use std::sync::{ Arc, Mutex };
 use std::time::Duration;
 
 
+pub use battery::BatteryModule;
 pub use time::TimeModule;
 
+pub mod battery;
 pub mod time;
 
 
diff --git a/src/module/battery.rs b/src/module/battery.rs
new file mode 100644
index 0000000..05f551f
--- /dev/null
+++ b/src/module/battery.rs
@@ -0,0 +1,30 @@
+use crate::error::Result;
+use crate::facility::FacilityType;
+use crate::module::{ CreatableModule, Module };
+
+use std::time::Duration;
+
+
+pub struct BatteryModule {
+}
+
+
+impl CreatableModule for BatteryModule {
+  const REQUIRED_FACILITIES: FacilityType = FacilityType::ENVSYS;
+
+  fn new() -> Result<BatteryModule> {
+    Ok(BatteryModule {
+    })
+  }
+}
+
+impl Module for BatteryModule {
+  fn format(&mut self) -> String {
+    "battery".to_string()
+  }
+
+  fn sleep_duration(&self) -> Option<Duration> {
+    None
+  }
+}
+