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