use crate::actor::*; use crate::error::Error; use preserves::value::Map; pub type DuringRetractionHandler = Box ActorResult>; pub struct During(Map>); pub type DuringResult = Result ActorResult>>, Error>; pub struct DuringEntity where E: 'static + Send, Fa: Send + FnMut(&mut E, &mut Activation, Assertion) -> DuringResult, { state: E, handler: Fa, during: During, } impl During { pub fn new() -> Self { During(Map::new()) } pub fn await_retraction ActorResult>( &mut self, h: Handle, f: F, ) -> ActorResult { self.0.insert(h, Box::new(f)); Ok(()) } pub fn retract(&mut self, h: Handle) -> DuringRetractionHandler { self.0.remove(&h).unwrap_or_else(|| Box::new(|_, _| Ok(()))) } } impl DuringEntity where E: 'static + Send, Fa: Send + FnMut(&mut E, &mut Activation, Assertion) -> DuringResult, { pub fn new(state: E, handler: Fa) -> Self { DuringEntity { state, handler, during: During::new(), } } } impl Entity for DuringEntity where E: 'static + Send, Fa: Send + FnMut(&mut E, &mut Activation, Assertion) -> DuringResult, { fn assert(&mut self, t: &mut Activation, a: Assertion, h: Handle) -> ActorResult { match (self.handler)(&mut self.state, t, a)? { Some(f) => self.during.await_retraction(h, f), None => Ok(()) } } fn retract(&mut self, t: &mut Activation, h: Handle) -> ActorResult { self.during.retract(h)(&mut self.state, t) } }