1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
use crate::error::Result;
use std::any::Any;
use std::collections::BTreeMap;
use async_executor::StaticExecutor;
use bitflags::bitflags;
pub use envsys::EnvSys;
pub use i3::I3;
pub mod envsys;
pub mod i3;
pub trait Facility: Any {
/* 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 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;
/* 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
* 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.
*/
async fn new(executor: &'static StaticExecutor) -> 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(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct FacilityType: u32 {
const ENVSYS = 0x00000001;
const I3 = 0x00000002;
}
}
impl FacilityType {
pub async fn new(&self, executor: &'static StaticExecutor)
-> Result<Box<dyn Facility>>
{
match self {
&FacilityType::ENVSYS => Ok(EnvSys::new(executor).await?.as_box()),
&FacilityType::I3 => Ok(I3::new(executor).await?.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
* 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() }
}
/* 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: 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 }
}
#[allow(unused)]
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 }
}
}
|