use std::cell::Cell;
use bson::Bson;
use crate::vm::operators::VmOperator;
pub(crate) struct SumOperator {
inner: Cell<i64>,
}
impl SumOperator {
pub(crate) fn compile(_v: &Bson) -> Box<dyn VmOperator> {
Box::new(SumOperator {
inner: Cell::new(0),
})
}
}
impl VmOperator for SumOperator {
fn initial_value(&self) -> Bson {
Bson::Int64(self.inner.get())
}
fn next(&self, _input: &Bson) -> Bson {
let next = self.inner.get() + 1;
self.inner.set(next);
Bson::Int64(next)
}
fn complete(&self) -> Bson {
Bson::Int64(self.inner.get())
}
}