use crate::{ errors::{DispatchError, InternalError}, request::{payload::Payload, AFPluginEventRequest, FromAFPluginRequest}, util::ready::{ready, Ready}, }; use std::{any::type_name, ops::Deref, sync::Arc}; pub struct AFPluginState(Arc); impl AFPluginState where T: Send + Sync, { pub fn new(data: T) -> Self { AFPluginState(Arc::new(data)) } pub fn get_ref(&self) -> &T { self.0.as_ref() } } impl Deref for AFPluginState where T: ?Sized + Send + Sync, { type Target = Arc; fn deref(&self) -> &Arc { &self.0 } } impl Clone for AFPluginState where T: ?Sized + Send + Sync, { fn clone(&self) -> AFPluginState { AFPluginState(self.0.clone()) } } impl From> for AFPluginState where T: ?Sized + Send + Sync, { fn from(arc: Arc) -> Self { AFPluginState(arc) } } impl FromAFPluginRequest for AFPluginState where T: ?Sized + Send + Sync + 'static, { type Error = DispatchError; type Future = Ready>; #[inline] fn from_request(req: &AFPluginEventRequest, _: &mut Payload) -> Self::Future { if let Some(state) = req.get_state::>() { ready(Ok(state.clone())) } else { let msg = format!("Failed to get the plugin state of type: {}", type_name::()); log::error!("{}", msg,); ready(Err(InternalError::Other(msg).into())) } } }