measurements: store to disk *after* evaluating them

i'm hoping to simplify a lot of serialization code with this
This commit is contained in:
2022-07-28 21:43:48 -07:00
parent 32d1a70d15
commit 5c4b8d86f2
5 changed files with 53 additions and 39 deletions

View File

@@ -15,7 +15,8 @@ pub trait AbstractMeasurement: Send + Sync + DynClone {
}
dyn_clone::clone_trait_object!(AbstractMeasurement);
pub fn eval_multiple_kv(state: &dyn GenericSim, meas: &[Box<dyn AbstractMeasurement>]) -> IndexMap<String, String> {
/// create one IndexMap out of several measurements
pub fn eval_multiple_kv(state: &dyn GenericSim, meas: &[&dyn AbstractMeasurement]) -> IndexMap<String, String> {
let mut r = IndexMap::new();
for m in meas {
let other = m.key_value(state);
@@ -24,6 +25,12 @@ pub fn eval_multiple_kv(state: &dyn GenericSim, meas: &[Box<dyn AbstractMeasurem
r
}
pub fn eval_to_vec(state: &dyn GenericSim, meas: &[&dyn AbstractMeasurement]) -> Vec<Evaluated> {
eval_multiple_kv(state, meas).into_iter().map(|(k, v)| {
Evaluated::new(k, v)
}).collect()
}
#[derive(Clone, Serialize, Deserialize)]
pub struct Time;
@@ -58,23 +65,26 @@ impl AbstractMeasurement for Meta {
}
}
/// some measurement which has already been evaluated.
/// this is used particularly if we need to monomorphize a measurement (e.g. for serialization)
/// and know it won't be applied to a new/different state.
#[derive(Clone, Serialize, Deserialize)]
pub struct Label(pub String);
pub struct Evaluated(String, String);
impl Label {
pub fn new<S: Into<String>>(s: S) -> Self {
Self(s.into())
impl Evaluated {
pub fn new<S1: Into<String>, S2: Into<String>>(key: S1, value: S2) -> Self {
Self(key.into(), value.into())
}
}
#[typetag::serde]
impl AbstractMeasurement for Label {
impl AbstractMeasurement for Evaluated {
fn eval(&self, _state: &dyn GenericSim) -> String {
self.0.clone()
self.1.clone()
}
fn key_value(&self, _state: &dyn GenericSim) -> IndexMap<String, String> {
[
(self.0.clone(), self.0.clone()),
(self.0.clone(), self.1.clone()),
].into_iter().collect()
}
}