diff options
Diffstat (limited to 'src/facility.rs')
| -rw-r--r-- | src/facility.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/facility.rs b/src/facility.rs index c156570..dcc252c 100644 --- a/src/facility.rs +++ b/src/facility.rs @@ -21,6 +21,11 @@ pub trait Facility: Any { */ #[allow(dead_code)] fn as_any_mut(&mut self) -> &mut dyn Any; + + /* Frustratingly, this can't be async because that wouldn't be + * dyn-compatible. + */ + fn update(&mut self) -> Result<()>; } /* In order to keep Facility dyn-compatible, we put everything that isn't in @@ -106,5 +111,48 @@ impl FacilityMap { (*facility).as_mut().as_any_mut().downcast_mut() } else { None } } + + pub fn iter(&self) -> impl Iterator<Item=&Box<dyn Facility>> { + let btree_iterator = self.facilities.iter(); + + Iter { btree_iterator } + } + + pub fn iter_mut(&mut self) -> impl Iterator<Item=&mut Box<dyn Facility>> { + let btree_iterator = self.facilities.iter_mut(); + + IterMut { btree_iterator } + } +} + + +struct Iter<'a> { + btree_iterator: std::collections::btree_map + ::Iter<'a, FacilityType, Box<dyn Facility>>, +} + +impl<'a> Iterator for Iter<'a> { + type Item = &'a Box<dyn Facility>; + + fn next(&mut self) -> Option<Self::Item> { + if let Some((_, value)) = self.btree_iterator.next() { + Some(value) + } else { None } + } +} + +struct IterMut<'a> { + btree_iterator: std::collections::btree_map + ::IterMut<'a, FacilityType, Box<dyn Facility>>, +} + +impl<'a> Iterator for IterMut<'a> { + type Item = &'a mut Box<dyn Facility>; + + fn next(&mut self) -> Option<Self::Item> { + if let Some((_, value)) = self.btree_iterator.next() { + Some(value) + } else { None } + } } |